33,008
社区成员
发帖
与我相关
我的任务
分享
#include <stdio.h>
int a[30001];
int b[30001];
int c[30001];
int w[30001];
int n;
void move(int i,int j){
int x=i,y=j;
n=0;
while(x!=a[x]){
w[n]=x;
n++;
x=a[x];
}
while(y!=a[y]) y=a[y];
if(x==y) return;
a[x]=y;
b[x]=c[y];
c[y]+=c[x];
for(i=n-1;i>=0;i--){
b[w[i]]+=b[a[w[i]]];
a[w[i]]=y;
}
}
void count(int i){
int x=i,y=i;
n=0;
while(a[x]!=a[a[x]]){
w[n]=x;
n++;
x=a[x];
}
for(i=n-1;i>=0;i--){
b[w[i]]+=b[a[w[i]]];
a[w[i]]=a[x];
}
printf("%d\n",b[y]);
}
int main(){
int i,n;
char o;
int x,y;
for(i=1;i<=30000;i++){
a[i]=i;b[i]=0;c[i]=1;
}
scanf("%d",&n);
while(n--){
scanf("\n%c",&o);
if(o=='M'){
scanf("%d %d",&x,&y);
move(x,y);
}else if(o=='C'){
scanf("%d",&x);
count(x);
}
}
return 0;
}
5868361 linren 1988 Accepted 512K 250MS C 823B 2009-09-17 13:02:30
5866649 linren 1988 Time Limit Exceeded C 591B 2009-09-16 23:03:12
5866644 linren 1988 Wrong Answer C 678B 2009-09-16 23:02:08
5866597 linren 1988 Wrong Answer C 627B 2009-09-16 22:52:44
5866394 linren 1988 Time Limit Exceeded C 572B 2009-09-16 22:16:21
5866369 linren 1988 Wrong Answer G++ 562B 2009-09-16 22:11:53
5866315 linren 1988 Wrong Answer GCC 567B 2009-09-16 22:03:05
5866284 linren 1988 Wrong Answer C 567B 2009-09-16 21:58:02
5864004 linren 1988 Time Limit Exceeded C++ 600B 2009-09-16 16:21:21
5863997 linren 1988 Time Limit Exceeded GCC 600B 2009-09-16 16:20:29
5863900 linren 1988 Time Limit Exceeded GCC 552B 2009-09-16 16:00:29
5863605 linren 1988 Time Limit Exceeded GCC 416B 2009-09-16 14:53:01
5863578 linren 1988 Time Limit Exceeded GCC 464B 2009-09-16 14:47:46
5863443 linren 1988 Time Limit Exceeded C 542B 2009-09-16 14:21:32
5863352 linren 1988 Time Limit Exceeded C++ 534B 2009-09-16 14:00:40
5863337 linren 1988 Time Limit Exceeded GCC 588B 2009-09-16 13:58:23
5863286 linren 1988 Time Limit Exceeded GCC 878B 2009-09-16 13:51:36
5863272 linren 1988 Runtime Error C 877B 2009-09-16 13:49:56
5863240 linren 1988 Runtime Error C 865B 2009-09-16 13:42:16
#include <stdio.h>
#include <stdlib.h>
void init(int **a,int **b,int n){
int i;
*a=(int*)malloc(sizeof(int)*n);
*b=(int*)malloc(sizeof(int)*n);
for(i=1;i<n;i++) (*a)[i]=-1;//记录集合每个立方体上面的立方体
for(i=1;i<n;i++) (*b)[i]=-1;//记录集合每个立方体下面的立方体
}
void move(int *a,int *b,int x,int y){
int px=x,py=y,pz=x;
while(b[px]!=-1) px=b[px];//查找含有x的最下面的立方体=>px
while(a[py]!=-1) py=a[py];//查找含有y的最上面的立方体=>py
while(a[pz]!=-1) pz=a[pz];//查找含有x的最上面的立方体=>pz
if(py==pz) return;//如果在同一个集合不进行操作
b[px]=py;
a[py]=px;
}
void count(int *b,int x){
int px=x;
int c=0;
while(b[px]!=-1){
px=b[px];c++;
}
printf("%d cubes under x(%d)\n",c,x);
}
void del(int **a,int **b){
free(*a);free(*b);
}
int main(){
int *a,*b;
int n=30000;
init(&a,&b,n);
/**执行指令******************/
move(a,b,1,2);
move(a,b,3,4);
move(a,b,3,2);
count(b,1);
count(b,2);
count(b,3);
count(b,4);
/******************执行指令**/
del(&a,&b);
return 0;
}
1 cubes under x(1)
0 cubes under x(2)
3 cubes under x(3)
2 cubes under x(4)
Press any key to continue
#include <iostream>
using namespace std;
struct {
int parent;
int CubeInStack;
int CubeBelow;
}cube[30010];
void MakeSet(int SizeOfSet)
{
for (int i = 1;i <= SizeOfSet;i++)
{
cube[i].parent = i;
cube[i].CubeInStack = 1;
cube[i].CubeBelow = 0;
}
}
void Union(int RootOfX,int RootOfY,int NodeX,int NodeY)
{
//将X所在集合依附于Y所在集合,更新Y集合根节点的CubeInStack,更新X集合根节点的CubeBelow
cube[RootOfX].parent = RootOfY;
cube[RootOfX].CubeBelow += cube[RootOfY].CubeInStack;
cube[RootOfY].CubeInStack += cube[RootOfX].CubeInStack;
}
int Find(int NodeToFind)
{
if(cube[NodeToFind].parent==NodeToFind)
return NodeToFind;
int temp = cube[NodeToFind].parent;
cube[NodeToFind].parent = Find(cube[NodeToFind].parent);
cube[NodeToFind].CubeBelow += cube[temp].CubeBelow;
return cube[NodeToFind].parent;
}
int main()
{
MakeSet(30005);
int NumOfOrders;
cin>>NumOfOrders;
char order;
int x,y;
while(NumOfOrders--)
{
//scanf("%c %d %d",&order,&x,&y);
scanf("\n%c",&order);
//cin>>order;
if (order=='M')
{
scanf(" %d %d",&x,&y);
//cin>>x>>y;
int rootx = Find(x);
int rooty = Find(y);
Union(rootx,rooty,x,y);
}
else
{
scanf(" %d",&x);
Find(x);
//cin>>x;
cout<<cube[x].CubeBelow<<endl;
}
//getchar();
}
}