【带权并查集】【HDOJ】
http://acm.hdu.edu.cn/showproblem.php?pid=3047
Zjnu Stadium
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 4799 Accepted Submission(s): 1849
Problem Description
In 12th Zhejiang College Students Games 2007, there was a new stadium built in Zhejiang Normal University. It was a modern stadium which could hold thousands of people. The audience Seats made a circle. The total number of columns were 300 numbered 1--300, counted clockwise, we assume the number of rows were infinite.
These days, Busoniya want to hold a large-scale theatrical performance in this stadium. There will be N people go there numbered 1--N. Busoniya has Reserved several seats. To make it funny, he makes M requests for these seats: A B X, which means people numbered B must seat clockwise X distance from people numbered A. For example: A is in column 4th and X is 2, then B must in column 6th (6=4+2).
Now your task is to judge weather the request is correct or not. The rule of your judgement is easy: when a new request has conflicts against the foregoing ones then we define it as incorrect, otherwise it is correct. Please find out all the incorrect requests and count them as R.
These days, Busoniya want to hold a large-scale theatrical performance in this stadium. There will be N people go there numbered 1--N. Busoniya has Reserved several seats. To make it funny, he makes M requests for these seats: A B X, which means people numbered B must seat clockwise X distance from people numbered A. For example: A is in column 4th and X is 2, then B must in column 6th (6=4+2).
Now your task is to judge weather the request is correct or not. The rule of your judgement is easy: when a new request has conflicts against the foregoing ones then we define it as incorrect, otherwise it is correct. Please find out all the incorrect requests and count them as R.
Input
There are many test cases:
For every case:
The first line has two integer N(1<=N<=50,000), M(0<=M<=100,000),separated by a space.
Then M lines follow, each line has 3 integer A(1<=A<=N), B(1<=B<=N), X(0<=X<300) (A!=B), separated by a space.
For every case:
The first line has two integer N(1<=N<=50,000), M(0<=M<=100,000),separated by a space.
Then M lines follow, each line has 3 integer A(1<=A<=N), B(1<=B<=N), X(0<=X<300) (A!=B), separated by a space.
Output
For every case:
Output R, represents the number of incorrect request.
Output R, represents the number of incorrect request.
Sample Input
10 10
1 2 150
3 4 200
1 5 270
2 6 200
6 5 80
4 7 150
8 9 100
4 8 50
1 7 100
9 2 100
Sample Output
2
#include<iostream> #include<cstdio> #include<cstring> #include<algorithm> using namespace std; struct pot{ int id; int fa; int len; }POT[50005]; int find(int x) { if(POT[x].fa==x)return x; int tt=POT[x].fa; POT[x].fa=find(POT[x].fa); POT[x].len+=POT[tt].len; return POT[x].fa; } int cnt ; void Union(int x,int y,int z) { int tx=find(x); int ty=find(y); if(tx==ty) { if(((POT[x].len)%300)!=(POT[y].len+z)%300) cnt++; } else { if((POT[y].len+z-POT[x].len)>=0) { POT[tx].fa=ty; POT[tx].len=(POT[y].len+z-POT[x].len)%300; } else { POT[ty].fa=tx; POT[ty].len=(-(POT[y].len+z-POT[x].len))%300; } } } int main() { int n,m; while(scanf("%d%d",&n,&m)==2) { cnt = 0; int a,b,c; for(int i = 1 ; i <= n ;i++) { // POT[i].id=-1; POT[i].fa=i; POT[i].len=0; } while(m--) { int a,b,c; scanf("%d%d%d",&a,&b,&c); Union(a,b,c); } cout << cnt<<endl; } return 0; }
http://acm.hdu.edu.cn/showproblem.php?pid=3635
Dragon Balls
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 7617 Accepted Submission(s): 2820
Problem Description
Five hundred years later, the number of dragon balls will increase unexpectedly, so it's too difficult for Monkey King(WuKong) to gather all of the dragon balls together.
His country has N cities and there are exactly N dragon balls in the world. At first, for the ith dragon ball, the sacred dragon will puts it in the ith city. Through long years, some cities' dragon ball(s) would be transported to other cities. To save physical strength WuKong plans to take Flying Nimbus Cloud, a magical flying cloud to gather dragon balls.
Every time WuKong will collect the information of one dragon ball, he will ask you the information of that ball. You must tell him which city the ball is located and how many dragon balls are there in that city, you also need to tell him how many times the ball has been transported so far.
His country has N cities and there are exactly N dragon balls in the world. At first, for the ith dragon ball, the sacred dragon will puts it in the ith city. Through long years, some cities' dragon ball(s) would be transported to other cities. To save physical strength WuKong plans to take Flying Nimbus Cloud, a magical flying cloud to gather dragon balls.
Every time WuKong will collect the information of one dragon ball, he will ask you the information of that ball. You must tell him which city the ball is located and how many dragon balls are there in that city, you also need to tell him how many times the ball has been transported so far.
Input
The first line of the input is a single positive integer T(0 < T <= 100).
For each case, the first line contains two integers: N and Q (2 < N <= 10000 , 2 < Q <= 10000).
Each of the following Q lines contains either a fact or a question as the follow format:
T A B : All the dragon balls which are in the same city with A have been transported to the city the Bth ball in. You can assume that the two cities are different.
Q A : WuKong want to know X (the id of the city Ath ball is in), Y (the count of balls in Xth city) and Z (the tranporting times of the Ath ball). (1 <= A, B <= N)
For each case, the first line contains two integers: N and Q (2 < N <= 10000 , 2 < Q <= 10000).
Each of the following Q lines contains either a fact or a question as the follow format:
T A B : All the dragon balls which are in the same city with A have been transported to the city the Bth ball in. You can assume that the two cities are different.
Q A : WuKong want to know X (the id of the city Ath ball is in), Y (the count of balls in Xth city) and Z (the tranporting times of the Ath ball). (1 <= A, B <= N)
Output
For each test case, output the test case number formated as sample output. Then for each query, output a line with three integers X Y Z saparated by a blank space.
Sample Input
2
3 3
T 1 2
T 3 2
Q 2
3 4
T 1 2
Q 1
T 1 3
Q 1
Sample Output
Case 1:
2 3 0
Case 2:
2 2 1
3 3 2
1 #include<iostream> 2 #include<cstdio> 3 #include<cstring> 4 using namespace std; 5 int fa[10005],moves[10005],nums[10005]; 6 int find(int x) 7 { 8 if(x==fa[x])return x; 9 int t=fa[x]; 10 fa[x]=find(fa[x]); 11 moves[x]+=moves[t]; 12 //int xx=x; 13 14 // cout << x << fa[x]<<endl; 15 return fa[x]; 16 } 17 void Union(int x,int y) 18 { 19 int tt=find(x); 20 int t2=find(y); 21 if(tt==t2)return; 22 nums[t2]+=nums[tt]; 23 moves[tt]=1; 24 fa[tt]=t2; 25 // cout << tt<<y<<endl; 26 } 27 int main() 28 { 29 int t; 30 scanf("%d",&t); 31 int case1=1; 32 while(t--) 33 { 34 printf("Case %d:\n",case1++); 35 int n,q; 36 scanf("%d%d",&n,&q); 37 for(int i = 1 ; i <= n ;i++) 38 { 39 fa[i]=i; 40 moves[i]=0; 41 nums[i]=1; 42 } 43 while(q--) 44 { 45 char ss[50]; 46 scanf("%s",ss); 47 if(ss[0]=='T') 48 { 49 int a,b; 50 scanf("%d%d",&a,&b); 51 Union(a,b); 52 } 53 else 54 { 55 int c; 56 scanf("%d",&c); 57 int tt=find(c); 58 printf("%d %d %d\n",tt,nums[tt],moves[c]); 59 } 60 } 61 62 } 63 return 0; 64 }
小结:用结构体存与父节点的关系以及父节点序号,然后在路径压缩过程进行与祖先关系的转化即可