【PAT】1118. Birds in Forest (25)
来源:互联网 发布:淘宝商家贷款 编辑:程序博客网 时间:2024/04/28 02:54
1118. Birds in Forest (25)
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:43 10 1 22 3 44 1 5 7 83 9 6 4210 53 7Sample Output:
2 10YesNo
分析:并查集。
#include <iostream>#include <string>#include <cstdio>#include <cmath>#include <queue>#include <vector>#include <functional>#define rep(i,j,k) for(int i=j;i<=k;++i)const int Max=10001;int a[Max];using namespace std;int Find(int x){if (a[x]>0) return Find(a[x]);else return x;}void Union(int x, int y){if (a[x]==0) a[x]=-1;if (a[y]==0) a[y]=-1;x=Find(x);y=Find(y);if (x==y) return;if (a[x]<a[y]) {a[x]+=a[y];a[y]=x;}else{a[y]+=a[x];a[x]=y;}}bool isUnion(int x, int y){if (Find(x)==Find(y)) return true;else return false;}int main(){//freopen("test.txt","r",stdin);rep(i,1,Max-1) a[i]=0;int N;cin>>N;rep(i,1,N){int k;cin>>k;int x;if (k>0) {cin>>x;if (a[x]==0) a[x]=-1;}rep(j,1,k-1){int y;cin>>y;Union(x,y);}}int sum1=0,sum2=0;rep(i,1,Max-1){if (a[i]<0){++sum1;sum2-=a[i];}}cout<<sum1<<' '<<sum2<<endl;int m;cin>>m;rep(i,1,m){int x,y;cin>>x>>y;if (isUnion(x,y)) cout<<"Yes\n";else cout<<"No\n";} return 0;}
- PAT(A) - 1118. Birds in Forest (25)
- 【PAT】1118. Birds in Forest (25)
- 1118. Birds in Forest (25) PAT 甲级
- PAT-1118. Birds in Forest (25)
- PAT 甲级 1118. Birds in Forest (25)
- 【PAT】1118. Birds in Forest
- pat 1118. Birds in Forest
- PAT - 甲级 - 1118. Birds in Forest (25) (并查集)
- pat 1118. Birds in Forest (25) 并查集
- PAT甲级练习1118. Birds in Forest (25)
- PAT 1118. Birds in Forest (25) 并查集
- 【PAT】【Advanced Level】1118. Birds in Forest (25)
- PAT A 1118. Birds in Forest
- PAT 1118 Birds in Forest (25)
- PAT A1118 birds in forest (25)
- PAT A1118. Birds in Forest (25)
- 1118. Birds in Forest (25)
- 1118. Birds in Forest (25)
- 11 Passing Date Into Our Components
- 十六进制转八进制
- this和$(this)的区别
- python技巧(6)
- 657UVa掷骰子(图)
- 【PAT】1118. Birds in Forest (25)
- mysql-遇到的问题
- 从一个字符数组中读出相应的整数、实数
- 【u229】独木桥
- 如果Apache-SVN发生报错Permission denied
- 图像矫正与车牌识别资料整理
- Randomized quickSort
- 102. Binary Tree Level Order Traversal
- 模式识别hw3-------常见模式识别算法用于人脸图片性别识别