首页 > 代码库 > 并查集

并查集

How Many Tables

Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)
Total Submission(s): 29548    Accepted Submission(s): 14610


Problem Description
Today is Ignatius‘ birthday. He invites a lot of friends. Now it‘s dinner time. Ignatius wants to know how many tables he needs at least. You have to notice that not all the friends know each other, and all the friends do not want to stay with strangers.

One important rule for this problem is that if I tell you A knows B, and B knows C, that means A, B, C know each other, so they can stay in one table.

For example: If I tell you A knows B, B knows C, and D knows E, so A, B, C can stay in one table, and D, E have to stay in the other one. So Ignatius needs 2 tables at least.
 

 

Input
The input starts with an integer T(1<=T<=25) which indicate the number of test cases. Then T test cases follow. Each test case starts with two integers N and M(1<=N,M<=1000). N indicates the number of friends, the friends are marked from 1 to N. Then M lines follow. Each line consists of two integers A and B(A!=B), that means friend A and friend B know each other. There will be a blank line between two cases.
 

 

Output
For each test case, just output how many tables Ignatius needs at least. Do NOT print any blanks.
 

 

Sample Input
2
5 3
1 2
2 3
4 5
 
5 1
2 5
 

 

Sample Output
2
4
 代码实现
 1 #include <stdio.h>
 2 int p[1005];
 3 int find(int x)     //这个的作用就是下面的查找。
 4 { 
 5     if(x!=p[x])
 6         p[x]=find(p[x]);
 7     return p[x];
 8 }
 9 int hebing(int x,int y)  //这个的作用就是用来合并的。
10 {
11     return  p[x]=y;     //假设a=2,b=3,此时应该有p[2]=p[3]=3。即2和3到同一张桌子了。
12 }
13 int main()
14 {
15     int t,i,a,b;
16     scanf("%d",&t);
17     while(t--)
18     {
19         int n,m,ans=0;
20         scanf("%d%d",&n,&m);
21         for(i=1;i<=n;i++)
22             p[i]=i;   //初始化它,让编号为一的值为1,编号为2的值为2.以此类推。
23         for(i=1;i<=m;i++)
24         {
25             scanf("%d%d",&a,&b);
26             a=find(a);   
27             b=find(b);   //假设a=2,b=3,我认为经过这个查找之后p[2]就等于p[3]了。
28             if(a!=b)
29                 hebing(a,b);  //合并为一个值。
30         }
31         for(i=1;i<=n;i++)
32         {
33             if(p[i]==i)   //经过M次合并之后,如果是朋友,或者间接朋友的,他们对应的值都为同一个。所以桌子就减少了。
34                 ans++;     //如果值还是对应的,那么就不是朋友关系,增加一张桌子。
35         }
36         printf("%d\n",ans);//输出注意格式,只有一个空行。
37     }
38     return 0;
39 }

畅通工程

Time Limit: 4000/2000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)
Total Submission(s): 52520    Accepted Submission(s): 27993


Problem Description
某省调查城镇交通状况,得到现有城镇道路统计表,表中列出了每条道路直接连通的城镇。省政府“畅通工程”的目标是使全省任何两个城镇间都可以实现交通(但不一定有直接的道路相连,只要互相间接通过道路可达即可)。问最少还需要建设多少条道路?
 

 

Input
测试输入包含若干测试用例。每个测试用例的第1行给出两个正整数,分别是城镇数目N ( < 1000 )和道路数目M;随后的M行对应M条道路,每行给出一对正整数,分别是该条道路直接连通的两个城镇的编号。为简单起见,城镇从1到N编号。
注意:两个城市之间可以有多条道路相通,也就是说
3 3
1 2
1 2
2 1
这种输入也是合法的
当N为0时,输入结束,该用例不被处理。
 

 

Output
对每个测试用例,在1行里输出最少还需要建设的道路数目。
 

 

Sample Input
4 2
1 3
4 3
3 3
1 2
1 3
2 3
5 2
1 2
3 5
999 0
0
 

 

Sample Output
1
0
2
998
 
代码实现
 1 #include<iostream>
 2 using namespace std;
 3 int p[1000];
 4 int fun1(int x)
 5 {
 6     if(x!=p[x])
 7         p[x]=fun1(p[x]);
 8     return p[x];
 9 }
10 int fun2(int x,int y)
11 {
12     return p[x]=y;
13 }
14 int main()
15 {
16     int n,m,a,b,ans;
17     while(scanf("%d%d",&n,&m)!=EOF&&n!=0)
18     {
19         ans=0;
20         for(int i=1;i<=n;i++)
21             p[i]=i;
22         for(int i=1;i<=m;i++)
23         {
24             scanf("%d%d",&a,&b);
25             a=fun1(a);
26             b=fun1(b);
27             if(a!=b)
28                 fun2(a,b);
29                 
30         }
31         for(int i=1;i<=n;i++)
32         {
33             if(p[i]==i)
34                 ans++;
35         }
36         printf("%d\n",ans-1);
37     }
38     return 0;
39  } 

总结:此类题的关键是对各个有关数对建立联系,组成数集,然后找出数集个数。

并查集