PAT 1118. Birds in Forest (25) 并查集
2017-09-11 20:18
417 查看
1118. Birds in Forest (25)
时间限制150 ms
内存限制
65536 kB
代码长度限制
16000 B
判题程序
Standard
作者
CHEN, Yue
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:
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<iostream> #include<algorithm> #include<math.h> #include<string.h> #include<string> #include<stdio.h> #include<queue> #include<set> #include<deque> using namespace std; int father[10005]; int cnt[10005]={0}; bool cmp(int x,int y) { return x>y; } int findyou(int x) { if(x==father[x]) return x; else { int zz=findyou(father[x]); father[x]=zz; return zz; } } int main() { int n; cin>>n; //memset(father,-1,sizeof(father)); for(int i=0;i<10005;i++) father[i]=i; int maxx=-1; for(int i=0;i<n;i++) { int zz; cin>>zz; int hh; cin>>hh; findyou(hh); if(hh>maxx) maxx=hh; for(int j=1;j<zz;j++) { int ppp; cin>>ppp; if(ppp>maxx) maxx=ppp; if(father[ppp]!=father[hh]) father[fa 906b ther[ppp]]=hh; findyou(ppp); } } for(int i=0;i<10001;i++) findyou(i); for(int i=1;i<=maxx;i++) cnt[father[i]]++; sort(cnt,cnt+10001,cmp); int p=0; while(cnt[p]!=0) p++; cout<<p<<' '<<maxx<<endl; int m; cin>>m; for(int i=0;i<m;i++) { int x,y; cin>>x>>y; if(father[x]==father[y]) cout<<"Yes"<<endl; else cout<<"No"<<endl; } return 0; }
相关文章推荐
- PAT (Advanced Level)1118. Birds in Forest (25)&&1114. Family Property (25)并查集找爸爸
- 1114. Family Property (25)(PAT 并查集)
- 1114. Family Property (25)-PAT甲级真题(并查集)
- PAT - 甲级 - 1118. Birds in Forest (25) (并查集)
- 1118. Birds in Forest (25)-PAT甲级真题(并查集)
- PAT 1013. Battle Over Cities (25)(并查集||DFS)
- PAT - 甲级 - 1114. Family Property (25) (并查集)
- PAT甲题题解-1114. Family Property (25)-(并查集模板题)
- PAT 04-2. File Transfer (25) (并查集)
- 【并查集】PAT 家庭房产(25 分)
- 1021. Deepest Root (25)【并查集+深搜】——PAT (Advanced Level) Practise
- PAT (Advanced Level) 1013. Battle Over Cities (25) 并查集
- pat 1118. Birds in Forest (25) 并查集
- PAT题解-1118. Birds in Forest (25)-(并查集模板题)
- PAT-A 1021 Deepest Root (25)
- 【PAT】1071. Speech Patterns (25)
- PAT-乙级-1035. *插入与归并(25)
- 【PAT Advanced Level】1032. Sharing (25)
- 1039. Course List for Student (25)PAT甲级
- 1021. Deepest Root (25) -并查集判树 -BFS求深度