畅通工程(并查集基本应用)

 

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
 
 
解题思路:并查集的基本应用,将各个城镇分成n个集合,要使全部的城镇都有连接,那么还需要n-1条通路。
 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 }

 

 
How Many Tables
 

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
 
题目意思:在T组输入输出的情况下,有n个人参加生日聚会,有m种关系,问最后需要多少个桌子。
 
解题思路:并查集的基本应用,在这里是将参加聚会的n个人按照关系分为几种集合,有几种集合就需要几个桌子。
 
 
 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

Severe acute respiratory syndrome (SARS), an atypical pneumonia of unknown aetiology, was recognized as a global threat in mid-March 2003. To minimize transmission to others, the best strategy is to separate the suspects from others. 
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

The input file contains several cases. Each test case begins with two integers n and m in a line, where n is the number of students, and m is the number of groups. You may assume that 0 < n <= 30000 and 0 <= m <= 500. Every student is numbered by a unique integer between 0 and n−1, and initially student 0 is recognized as a suspect in all the cases. This line is followed by m member lists of the groups, one line per group. Each line begins with an integer k by itself representing the number of members in the group. Following the number of members, there are k integers representing the students in this group. All the integers in a line are separated by at least one space. 
A case with n = 0 and m = 0 indicates the end of the input, and need not be processed.

Output

For each case, output the number of suspects in one line.

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 }

 




posted @ 2018-06-02 14:31  王陸  阅读(483)  评论(0编辑  收藏  举报