PAT - 甲级 - 1118. Birds in Forest (25) (并查集)
2016-12-08 10:34
369 查看
Some scientists took pictures of thousands of birds in a forest. Assume that all the birds appear in the same picture belong to the same tree. You are supposed to help the scientists to count the maximum number of trees in the forest, and for any pair of birds,
tell if they are on the same tree.
Input Specification:
Each input file contains one test case. For each case, the first line contains a positive number N (<= 104) which is the number of pictures. Then N lines follow, each describes a picture in the format:
K B1 B2 ... BK
where K is the number of birds in this picture, and Bi's are the indices of birds. It is guaranteed that the birds in all the pictures are numbered continuously from 1 to some number that is no more than 104.
After the pictures there is a positive number Q (<= 104) which is the number of queries. Then Q lines follow, each contains the indices of two birds.
Output Specification:
For each test case, first output in a line the maximum possible number of trees and the number of birds. Then for each query, print in a line "Yes" if the two birds belong to the same tree, or "No" if not.
Sample Input:
Sample Output:
题目大意:
给了n张树的照片,还给了树上的鸟,让输出有几棵树,共几只鸟。并且给出程序,能够判断任意2只鸟是否在一棵树上。
思路:1:求几只鸟很简单:桶排思想,一个萝卜一个坑,用数据记录即可。
求几棵树:并且判断2鸟是否在一棵树上要用到并查集的知识。
2:求几棵树:即求出有多少个根节点。
3:求2鸟是否一棵树:求2鸟的根节点是否相同即可。
不会并查集的:点击查看 点连接看一下。
tell if they are on the same tree.
Input Specification:
Each input file contains one test case. For each case, the first line contains a positive number N (<= 104) which is the number of pictures. Then N lines follow, each describes a picture in the format:
K B1 B2 ... BK
where K is the number of birds in this picture, and Bi's are the indices of birds. It is guaranteed that the birds in all the pictures are numbered continuously from 1 to some number that is no more than 104.
After the pictures there is a positive number Q (<= 104) which is the number of queries. Then Q lines follow, each contains the indices of two birds.
Output Specification:
For each test case, first output in a line the maximum possible number of trees and the number of birds. Then for each query, print in a line "Yes" if the two birds belong to the same tree, or "No" if not.
Sample Input:
4 3 10 1 2 2 3 4 4 1 5 7 8 3 9 6 4 2 10 5 3 7
Sample Output:
2 10 Yes No
题目大意:
给了n张树的照片,还给了树上的鸟,让输出有几棵树,共几只鸟。并且给出程序,能够判断任意2只鸟是否在一棵树上。
思路:1:求几只鸟很简单:桶排思想,一个萝卜一个坑,用数据记录即可。
求几棵树:并且判断2鸟是否在一棵树上要用到并查集的知识。
2:求几棵树:即求出有多少个根节点。
3:求2鸟是否一棵树:求2鸟的根节点是否相同即可。
不会并查集的:点击查看 点连接看一下。
#include <cstdio> #include <algorithm> using namespace std; int p[10010]; int find(int x) { return p[x] == x ? x : p[x] = find(p[x]); } void join(int x, int y) { int fx = find(x); int fy = find(y); if(fx != fy) p[fx] = fy; } int n, k, a, b, cntBird, cntTree, book[10010]; int main() { fill(book, book+10010, 0); scanf("%d", &n); for(int i = 0; i < 10010; i++) { p[i] = i; } for (int i = 0; i < n; i++) { scanf("%d", &k); scanf("%d", &a); book[a] = 1; for (int j = 1; j < k; j++) { scanf("%d", &b); book[b] = 1; join(a, b); } } for (int i = 0; i < 10010; i++) { if (book[i] == 1) cntBird++; if (book[i] == 1 && p[i] == i) cntTree++; } printf("%d %d\n", cntTree, cntBird); scanf("%d", &k); for (int i = 0; i < k; i++) { scanf("%d %d", &a, &b); int fa = find(a); int fb = find(b); if(fa == fb) printf("Yes\n"); else printf("No\n"); } return 0; }
相关文章推荐
- 1118. Birds in Forest (25)-PAT甲级真题(并查集)
- PAT - 甲级 - 1114. Family Property (25) (并查集)
- 1114. Family Property (25)-PAT甲级真题(并查集)
- PAT甲级 1097. Deduplication on a Linked List (25)
- PAT(甲级)1028. List Sorting (25)
- 1079. Total Sales of Supply Chain (25)-PAT甲级真题(dfs,bfs,树的遍历)
- PAT甲级-1002. A+B for Polynomials (25)
- 1029. Median (25)-PAT甲级真题(two points)
- 1075. PAT Judge (25)-PAT甲级真题
- 1113. Integer Set Partition (25)-PAT甲级真题
- 1021. Deepest Root (25)-PAT甲级真题(图的遍历,dfs,连通分量的个数)
- *浙大PAT甲级 1107 并查集
- PAT(甲级) 1007. Maximum Subsequence Sum (25)
- PAT(甲级)1009. Product of Polynomials (25)
- 1067. Sort with Swap(0,*) (25)-PAT甲级真题(贪心算法)
- PAT甲级 1002. A+B for Polynomials (25)
- PAT 甲级 1012. The Best Rank (25)
- PAT(甲级) 1016. Phone Bills (25)
- PAT甲级 1137. Final Grading (25)
- PAT甲级1002. A+B for Polynomials (25)