畅通工程(并查集基本应用)
Description
Input
注意:两个城市之间可以有多条道路相通,也就是说
3 3
1 2
1 2
2 1
这种输入也是合法的
当N为0时,输入结束,该用例不被处理。
Output
Sample Input
Sample Output
1 #include<stdio.h> 2 int pre[1010]; 3 int findx(int x)///查找根节点 4 { 5 int a; 6 a=x; 7 while(pre[a]!=a) 8 { 9 a=pre[a]; 10 } 11 return a; 12 } 13 void combine(int root1,int root2)///合并路线 14 { 15 int x,y; 16 x=findx(root1); 17 y=findx(root2); 18 if(x!=y) 19 { 20 pre[x]=y; 21 } 22 } 23 int main() 24 { 25 int n,m,i,x,y,count; 26 while(scanf("%d%d",&n,&m)!=EOF) 27 { 28 if(n==0) 29 { 30 break; 31 } 32 count=0; 33 for(i=1; i<=n; i++) 34 { 35 pre[i]=i;///初始化 36 } 37 while(m--) 38 { 39 scanf("%d%d",&x,&y); 40 combine(x,y); 41 } 42 for(i=1;i<=n;i++) 43 { 44 if(pre[i]==i)///统计利用并查集将各个城镇分成的集合的个数 45 { 46 count++; 47 } 48 } 49 printf("%d\n",count-1);///若有n个集合那么便要新建n-1条通路 50 } 51 return 0; 52 }
Description
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
Output
Sample Input
Sample Output
1 #include<stdio.h> 2 int pre[1010]; 3 int findx(int x) 4 { 5 int a; 6 a=x; 7 while(pre[a]!=a) 8 { 9 a=pre[a]; 10 } 11 return a; 12 } 13 void combine(int root1,int root2) 14 { 15 int x,y; 16 x=findx(root1); 17 y=findx(root2); 18 if(x!=y) 19 { 20 pre[x]=y; 21 } 22 } 23 int main() 24 { 25 int n,m,i,x,y,count,t; 26 scanf("%d",&t); 27 while(t--) 28 { 29 scanf("%d%d",&n,&m); 30 count=0; 31 for(i=1; i<=n; i++) 32 { 33 pre[i]=i; 34 } 35 while(m--) 36 { 37 scanf("%d%d",&x,&y); 38 combine(x,y);///合并 39 } 40 for(i=1;i<=n;i++) 41 { 42 if(pre[i]==i) 43 { 44 count++; 45 } 46 } 47 printf("%d\n",count); 48 } 49 return 0; 50 }
The Suspects
Description
In the Not-Spreading-Your-Sickness University (NSYSU), there are many student groups. Students in the same group intercommunicate with each other frequently, and a student may join several groups. To prevent the possible transmissions of SARS, the NSYSU collects the member lists of all student groups, and makes the following rule in their standard operation procedure (SOP).
Once a member in a group is a suspect, all members in the group are suspects.
However, they find that it is not easy to identify all the suspects when a student is recognized as a suspect. Your job is to write a program which finds all the suspects.
Input
A case with n = 0 and m = 0 indicates the end of the input, and need not be processed.
Output
Sample Input
100 4 2 1 2 5 10 13 11 12 14 2 0 1 2 99 2
200 2 1 5 5 1 2 3 4 5 1 0 0 0
Sample Output
4 1 1
题目意思:
有很多组学生,在同一个组的学生经常会接触,也会有新的同学的加入。但是SARS是很容易传染的,只要在该组有一位同学怀疑感染SARS,那么该组的所有同学都被认为得了SARS。现在的任务是计算出有多少位学生感染SARS了。假定编号为0的同学是得了SARS的。
解题思路:并查集的基本应用,先利用并查集将各组同学按照关系分成不同的集合,统计含有0的集合中学生的个数即为被感染的人数。
1 #include <iostream> 2 #include <stdio.h> 3 using namespace std; 4 int pre[30010]; 5 int num[30010]; 6 int findx(int x) 7 { 8 int a; 9 a=x; 10 while(pre[a]!=a) 11 { 12 a=pre[a]; 13 } 14 return a; 15 } 16 void combine(int root1,int root2) 17 { 18 int x,y; 19 x=findx(root1); 20 y=findx(root2); 21 if(x!=y) 22 { 23 pre[x]=y; 24 num[y]=num[y]+num[x];///记录各个结点之下的个数 25 } 26 } 27 int main() 28 { 29 int m,n,t,i,j,a,b,k; 30 while(scanf("%d%d",&n,&m)!=EOF) 31 { 32 33 if(n==0&&m==0) 34 { 35 break; 36 } 37 for(i=0;i<n;i++) 38 { 39 pre[i]=i; 40 num[i]=1; 41 } 42 while(m--) 43 { 44 scanf("%d",&t); 45 scanf("%d",&a); 46 for(i=1;i<t;i++) 47 { 48 scanf("%d",&b); 49 combine(a,b); 50 } 51 } 52 k=findx(0); 53 printf("%d\n",num[k]); 54 } 55 return 0; 56 }
Ubiquitous Religions
There are so many different religions in the world today that it is difficult to keep track of them all. You are interested in finding out how many different religions students in your university believe in. You know that there are n students in your university (0 < n ≤ 50000). It is infeasible for you to ask every student their religious beliefs. Furthermore, many students are not comfortable expressing their beliefs. One way to avoid these problems is to ask m (0 ≤ m ≤ n(n−1)/2) pairs of students and ask them whether they believe in the same religion (e.g. they may know if they both attend the same church). From this data, you may not know what each person believes in, but you can get an idea of the upper bound of how many different religions can be possibly represented on campus. You may assume that each student subscribes to at most one religion.
Input
The input consists of a number of cases. Each case starts with a line specifying the integers n and m. The next m lines each consists of two integers i and j, specifying that students i and j believe in the same religion. The students are numbered 1 to n. The end of input is specified by a line in which n = m = 0.
Output
For each test case, print on a single line the case number (starting with 1) followed by the maximum number of different religions that the students in the university believe in.
Sample Input
10 9
1 2
1 3
1 4
1 5
1 6
1 7
1 8
1 9
1 10
10 4
2 3
4 5
4 8
5 8
0 0
Sample Output
Case 1: 1
Case 2: 7
题目意思:统计大学里大学生信仰的宗教的种类,给出了n个人和m组关系,再给出的关系中,两个同学信奉同一种宗教。
解题思路:并查集的基本应用,先利用并查集划分集合,再统计不同的种类个数。
1 #include <iostream> 2 #include <stdio.h> 3 using namespace std; 4 int pre[30010]; 5 int num[30010]; 6 int findx(int x) 7 { 8 int a; 9 a=x; 10 while(pre[a]!=a) 11 { 12 a=pre[a]; 13 } 14 return a; 15 } 16 void combine(int root1,int root2) 17 { 18 int x,y; 19 x=findx(root1); 20 y=findx(root2); 21 if(x!=y) 22 { 23 pre[x]=y; 24 num[y]=num[y]+num[x];///记录各个结点之下的个数 25 } 26 } 27 int main() 28 { 29 int m,n,t,i,j,a,b,k; 30 while(scanf("%d%d",&n,&m)!=EOF) 31 { 32 33 if(n==0&&m==0) 34 { 35 break; 36 } 37 for(i=0;i<n;i++) 38 { 39 pre[i]=i; 40 num[i]=1; 41 } 42 while(m--) 43 { 44 scanf("%d",&t); 45 scanf("%d",&a); 46 for(i=1;i<t;i++) 47 { 48 scanf("%d",&b); 49 combine(a,b); 50 } 51 } 52 k=findx(0); 53 printf("%d\n",num[k]); 54 } 55 return 0; 56 }