PAT A1118. Birds in Forest (25)(并查集)
2017-03-05 10:59
134 查看
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:
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
#include<cstdio> #include<algorithm> #include<cstring> const int Max = 11110; int n,m; int S[Max]={0}; int T[Max]={0}; int F[Max]={0}; bool Visit[Max]; int findf(int x) { int a = x; while(x!=S[x]) { x=S[x]; } while(a!=S[a]) { int f=S[a]; S[a]=x; a=f; } return a; } void Union (int a,int b) { int fa=findf(a); int fb=findf(b); if(fa!=fb) S[fb]=fa; } int main() { memset(Visit,false,sizeof(Visit)); for(int i=1;i<=Max;i++) { S[i]=i; } scanf("%d",&n); int mm=0,kk=0; for(int i =0 ; i<n;i++) { int temp; scanf("%d",&temp); for(int j=0;j<temp;j++) { int ans ; scanf("%d",&ans); if(Visit[ans]==false) { kk++; Visit[ans]=true; } if(ans>mm) mm=ans; if(T[i]==0) { T[i]=ans; } Union(ans,T[i]); } } for(int i=1;i<=mm;i++) { if(Visit[i]==true) { int a=findf(i); F[a]++;} } int ff=0; for(int i=1;i<=mm;i++) { if(F[i]!=0) ff++; } printf("%d %d\n",ff,kk); int k; scanf("%d",&k); for(int i=0;i<k;i++) { int temp;int ans; scanf("%d%d",&temp,&ans); if(S[temp]==S[ans]) { printf("Yes\n"); } else printf("No\n"); } system("pause"); return 0; }
相关文章推荐
- CF-25D - Roads not only in Berland(并查集或者搜索)
- Roads not only in Berland CodeForces 25D 并查集
- PAT (Advanced Level) 1013. Battle Over Cities (25) 并查集
- 1114. Family Property (25) 并查集
- PAT A 1118. Birds in Forest (25)【并查集】
- 1063. Set Similarity (25) 并查集
- CF-25D - Roads not only in Berland(并查集或者搜索)
- Codeforces 25D-Roads not only in Berland(并查集)
- Codeforces 25D-Roads not only in Berland 并查集
- 1114. Family Property (25)-PAT甲级真题(并查集)
- 7-6 红色警报(25 分)(并查集)
- 1079. Total Sales of Supply Chain (25)<BFS,DFS,并查集>
- 1021. Deepest Root (25) -- 并查集 dfs
- 1021. Deepest Root (25) -并查集判树 -BFS求深度
- 7-22 朋友圈(25 分) (并查集)
- 1118. Birds in Forest (25) 并查集
- 1114. Family Property (25)(PAT 并查集)
- 1114. Family Property (25) <并查集,set>
- Codeforces 25 D.Roads not only in Berland(并查集)
- CodeForces 25D - Roads not only in Berland(并查集题目)