1118. Birds in Forest (25)
2016-12-02 11:24
501 查看
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:
题解:考察并查集。还比较简单
ac代码:#include <iostream>
#include <map>
#include <cstdio>
#include <vector>
#include <string>
#include <algorithm>
#include <set>
using namespace std;
int father[10005];
set<int> seta;
int findfather(int x){
int a;
if(x!=father[x]){
a=findfather(father[x]);
int tmp;
while(x!=father[x]){
tmp=father[x];
father[x]=a;
x=tmp;
}
return a;
}
else return x;
}
void uniona(int a,int b){
int x=findfather(a);
int y=findfather(b);
if(x!=y) father[y]=x;
}
int vis[10005];
int main(){
//freopen("E:input.txt","r",stdin);
int n,q,k,num;
cin>>n;
for(int i=0;i<=10000;i++) father[i]=i;
int a,b,maxb=0;
for(int i=0;i<n;i++){
cin>>k;
if(k==0) continue;
int pre,a;
cin>>pre; if(pre>maxb) maxb=pre;
for(int j=1;j<k;j++){
cin>>a; if(a>maxb) maxb=a;
uniona(a,pre);
pre=a;
}
}
//for(int i=1;i<=maxb;i++) printf("%d %d\n",i,father[i]);
//cout<<endl;
int cnt=0;
for(int i=1;i<=maxb;i++){
if(vis[findfather(i)]==false){
++cnt;
vis[father[i]]=true;
}
}
printf("%d %d\n",cnt,maxb);
cin>>q;
for(int i=0;i<q;i++){
cin>>a>>b;
if(father[a]==father[b]) printf("Yes\n");
else printf("No\n");
}
//cout<<endl;
freopen("CON","r",stdin);
system("pause");
}
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 YesNo
题解:考察并查集。还比较简单
ac代码:#include <iostream>
#include <map>
#include <cstdio>
#include <vector>
#include <string>
#include <algorithm>
#include <set>
using namespace std;
int father[10005];
set<int> seta;
int findfather(int x){
int a;
if(x!=father[x]){
a=findfather(father[x]);
int tmp;
while(x!=father[x]){
tmp=father[x];
father[x]=a;
x=tmp;
}
return a;
}
else return x;
}
void uniona(int a,int b){
int x=findfather(a);
int y=findfather(b);
if(x!=y) father[y]=x;
}
int vis[10005];
int main(){
//freopen("E:input.txt","r",stdin);
int n,q,k,num;
cin>>n;
for(int i=0;i<=10000;i++) father[i]=i;
int a,b,maxb=0;
for(int i=0;i<n;i++){
cin>>k;
if(k==0) continue;
int pre,a;
cin>>pre; if(pre>maxb) maxb=pre;
for(int j=1;j<k;j++){
cin>>a; if(a>maxb) maxb=a;
uniona(a,pre);
pre=a;
}
}
//for(int i=1;i<=maxb;i++) printf("%d %d\n",i,father[i]);
//cout<<endl;
int cnt=0;
for(int i=1;i<=maxb;i++){
if(vis[findfather(i)]==false){
++cnt;
vis[father[i]]=true;
}
}
printf("%d %d\n",cnt,maxb);
cin>>q;
for(int i=0;i<q;i++){
cin>>a>>b;
if(father[a]==father[b]) printf("Yes\n");
else printf("No\n");
}
//cout<<endl;
freopen("CON","r",stdin);
system("pause");
}
相关文章推荐
- 1118. Birds in Forest (25)[并查集]
- 1118. Birds in Forest (25)-PAT甲级真题(并查集)
- 1118. Birds in Forest (25) PAT 甲级
- 1118. Birds in Forest (25)
- 1118. Birds in Forest (25)
- PAT (Advanced Level) Practise 1118 Birds in Forest (25)
- PAT (Advanced Level) Practise 1118 Birds in Forest (25)
- 1118. Birds in Forest (25)
- 1118. Birds in Forest (25)
- 1118. Birds in Forest (25)
- 1118. Birds in Forest (25) 并查集
- 1118. Birds in Forest (25)
- 1118. Birds in Forest (25)
- 1118. Birds in Forest (25)
- 1118. Birds in Forest (25)
- PAT (Advanced Level)1118. Birds in Forest (25)&&1114. Family Property (25)并查集找爸爸
- 1118. Birds in Forest
- 1118. Birds in Forest
- 1118. Birds in Forest
- PAT A1118 birds in forest (25)