九度OJ-第5章-图论
二、并查集
1. 例题
题目1012:畅通工程
时间限制:1 秒
内存限制:32 兆
特殊判题:否
提交:10519
解决:4794
- 题目描述:
-
某省调查城镇交通状况,得到现有城镇道路统计表,表中列出了每条道路直接连通的城镇。省政府“畅通工程”的目标是使全省任何两个城镇间都可以实现交通(但不一定有直接的道路相连,只要互相间接通过道路可达即可)。问最少还需要建设多少条道路?
- 输入:
-
测试输入包含若干测试用例。每个测试用例的第1行给出两个正整数,分别是城镇数目N ( < 1000 )和道路数目M;随后的M行对应M条道路,每行给出一对正整数,分别是该条道路直接连通的两个城镇的编号。为简单起见,城镇从1到N编号。
注意:两个城市之间可以有多条道路相通,也就是说
3 3
1 2
1 2
2 1
这种输入也是合法的
当N为0时,输入结束,该用例不被处理。
- 输出:
-
对每个测试用例,在1行里输出最少还需要建设的道路数目。
- 样例输入:
-
4 2 1 3 4 3 3 3 1 2 1 3 2 3 5 2 1 2 3 5 999 0 0
- 样例输出:
-
1 0 2 998
1 #include <iostream> 2 #include <algorithm> 3 #include <stdio.h> 4 #include <string> 5 #include <ctype.h> 6 #include <stack> 7 8 using namespace std; 9 10 #define N 1000 11 int Tree [N]; 12 13 int findRoot(int x) { 14 if (Tree[x] == -1) return x; //相当于找到根结点了 15 else { 16 int tmp = findRoot(Tree[x]); //Tree[x] 存储着x的父节点,所以相当于向上走了一层 17 Tree[x] = tmp; // x既然不是根结点,就把它顺便直接挂到根结点下面,不用隔着很多层了,查找的时候比较快 18 return tmp; 19 } 20 } 21 22 23 int main() { 24 int n, m; 25 while(scanf("%d", &n) != EOF && n != 0) { 26 scanf("%d", &m); 27 for(int i = 1; i <= n; i++) Tree[i] = -1; // 初始化所有节点为孤立节点 28 while(m-- != 0) { // 读入边信息; 注意这里m--后于判断0执行,以执行1次为例 29 int a, b; 30 scanf("%d%d", &a, &b); 31 a = findRoot(a); 32 b = findRoot(b); 33 if(a != b) Tree[a] = b; // 如果a, b不是一个集合里的,则并在一起; 这里把a并在了b下面 34 } 35 int ans = 0; 36 for(int i = 1; i <= n; i++) { 37 if(Tree[i] == -1) ans++; 38 } 39 printf("%d\n", ans-1); // 目前有多少个集合,就再修ans-1条路使其联通 40 } 41 42 return 0; 43 44 45 46 return 0; 47 }
题目1444:More is better
时间限制:3 秒
内存限制:100 兆
特殊判题:否
提交:4491
解决:2037
- 题目描述:
-
Mr Wang wants some boys to help him with a project. Because the project is rather complex, the more boys come, the better it will be. Of course there are certain requirements.Mr Wang selected a room big enough to hold the boys. The boy who are not been chosen has to leave the room immediately. There are 10000000 boys in the room numbered from 1 to 10000000 at the very beginning. After Mr Wang's selection any two of them who are still in this room should be friends (direct or indirect), or there is only one boy left. Given all the direct friend-pairs, you should decide the best way.
- 输入:
-
The first line of the input contains an integer n (0 ≤ n ≤ 100 000) - the number of direct friend-pairs. The following n lines each contains a pair of numbers A and B separated by a single space that suggests A and B are direct friends. (A ≠ B, 1 ≤ A, B ≤ 10000000)
- 输出:
-
The output in one line contains exactly one integer equals to the maximum number of boys Mr Wang may keep.
- 样例输入:
-
4 1 2 3 4 5 6 1 6 4 1 2 3 4 5 6 7 8
- 样例输出:
-
4 2
1 #include <iostream> 2 #include <algorithm> 3 #include <stdio.h> 4 #include <string> 5 #include <ctype.h> 6 #include <stack> 7 8 using namespace std; 9 10 #define N 10000001 11 int Tree [N]; 12 13 int findRoot(int x) { 14 if (Tree[x] == -1) return x; //相当于找到根结点了 15 else { 16 int tmp = findRoot(Tree[x]); //Tree[x] 存储着x的父节点,所以相当于向上走了一层 17 Tree[x] = tmp; // x既然不是根结点,就把它顺便直接挂到根结点下面,不用隔着很多层了,查找的时候比较快 18 return tmp; 19 } 20 } 21 22 int sum[N]; 23 24 int main() { 25 int n; 26 while(scanf("%d", &n) != EOF) { 27 for(int i = 1; i < N; i++) { 28 Tree[i] = -1; 29 sum[i] = 1; 30 } 31 while(n-- != 0) { 32 int a, b; 33 scanf("%d%d", &a, &b); 34 a = findRoot(a); 35 b = findRoot(b); 36 if(a != b) { 37 Tree[a] = b; 38 sum[b] += sum[a]; 39 } 40 } 41 42 int ans = 1; 43 for(int i = 1; i < N; i++) { 44 if(Tree[i] == -1 && sum[i] > ans) ans = sum[i]; // 统计最大值 45 } 46 printf("%d\n", ans); 47 } 48 49 return 0; 50 }
2. 练习题
1109题:
题目:
题目1109:连通图 时间限制:1 秒内存限制:32 兆特殊判题:否提交:4707解决:2518 题目描述: 给定一个无向图和其中的所有边,判断这个图是否所有顶点都是连通的。 输入: 每组数据的第一行是两个整数 n 和 m(0<=n<=1000)。n 表示图的顶点数目,m 表示图中边的数目。如果 n 为 0 表示输入结束。随后有 m 行数据,每行有两个值 x 和 y(0<x, y <=n),表示顶点 x 和 y 相连,顶点的编号从 1 开始计算。输入不保证这些边是否重复。 输出: 对于每组输入数据,如果所有顶点都是连通的,输出"YES",否则输出"NO"。 样例输入: 4 3 1 2 2 3 3 2 3 2 1 2 2 3 0 0 样例输出: NO YES
解答:
1 #include <iostream> 2 #include <algorithm> 3 #include <stdio.h> 4 #include <string> 5 #include <ctype.h> 6 #include <stack> 7 8 using namespace std; 9 10 #define N 1002 11 12 int Tree[N]; 13 14 int findRoot(int x) { 15 if(Tree[x] == -1) return x; 16 else { 17 int tmp = findRoot(Tree[x]); 18 Tree[x] = tmp; 19 return tmp; 20 } 21 } 22 23 int main() { 24 int n, m; 25 while(scanf("%d%d", &n, &m) != EOF) { 26 if(n == 0 && m == 0) return 0; 27 28 for(int i = 1; i <= n; i++) Tree[i] = -1; 29 30 while(m-- != 0) { 31 int a, b; 32 scanf("%d%d", &a, &b); 33 a = findRoot(a); 34 b = findRoot(b); 35 if(a != b) Tree[a] = b; // 即, 使得a的爸爸是b 36 } 37 38 int ans = 0; 39 for(int i = 1; i <= n; i++) { 40 if(Tree[i] == -1) ans++; 41 } 42 if(ans == 1) printf("YES\n"); 43 else printf("NO\n"); 44 } 45 46 return 0; 47 }
1445题:
题目:
题目1445:How Many Tables 时间限制:1 秒内存限制:128 兆特殊判题:否提交:1857解决:1123 题目描述: 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. 输入: 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. 输出: For each test case, just output how many tables Ignatius needs at least. Do NOT print any blanks. 样例输入: 2 5 3 1 2 2 3 4 5 5 1 2 5 样例输出: 2 4
解答:
1 #include <iostream> 2 #include <algorithm> 3 #include <stdio.h> 4 #include <string> 5 #include <ctype.h> 6 #include <stack> 7 8 using namespace std; 9 10 int Tree[1001]; 11 12 int findRoot(int x) { 13 14 if(Tree[x] == -1) return x; 15 else{ 16 int tmp = findRoot(Tree[x]); //这里注意一定是Tree[x] 17 Tree[x] = tmp; 18 return tmp; 19 } 20 } 21 22 23 int main() { 24 int aa; 25 scanf("%d", &aa); 26 while(aa--) { 27 int n, m; 28 scanf("%d%d", &n, &m); 29 for(int i = 1; i <= n; i++) { 30 Tree[i] = -1; 31 } 32 while(m--) { 33 int a, b; 34 scanf("%d%d", &a, &b); 35 a = findRoot(a); 36 b = findRoot(b); 37 38 if(a != b) { 39 Tree[a] = b; 40 } 41 } 42 43 int count = 0; 44 for(int i = 1; i <= n; i++) { 45 if(Tree[i] == -1) count++; 46 } 47 48 // printf("%d\n", count); 49 cout << count << endl; 50 } 51 52 return 0; 53 }
1446题: