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 (≤10^​4​​ ) which is the number of pictures. Then N lines follow, each describes a picture in the format:

K B​1 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 (≤10^4 ) 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<math.h>
#include<vector>
using namespace std;
vector<int> birds(10010, 0), check(10010, 0);
int findfather(int b){
int t=b;
while(birds[b]!=b)
b=birds[b];
while(birds[t]!=b){
int temp=t;
t=birds[t];
birds[temp]=b;
}
return b;
}
void Union(int a, int b){
int m= findfather(a);
int n= findfather(b);
if(m!=n) birds[m]=n;
}
int main(){
int n, k, b, num=0, cnt=0, qn;
cin>>n;
vector<int> picture(n, 0);
for(int i=0; i<10010; i++)
birds[i]=i;
for(int i=0; i<n; i++){
cin>>k;
for(int j=0; j<k; j++){
cin>>b;
num=max(num, b);
if(picture[i]==0)
picture[i]=b;
Union(b, picture[i]);
}
}
for(int i=1; i<=num; i++){
b=findfather(i);
if(check[b]==0){
cnt++;
check[b]=1;
}
}
cout<<cnt<<" "<<num<<endl;
cin>>qn;
for(int i=0; i<qn; i++){
int a, b;
cin>>a>>b;
if(findfather(a)==findfather(b))
cout<<"Yes"<<endl;
else
cout<<"No"<<endl;
}
return 0;
}

PAT 1118 Birds in Forest的更多相关文章

  1. PAT 1118 Birds in Forest [一般]

    1118 Birds in Forest (25 分) Some scientists took pictures of thousands of birds in a forest. Assume ...

  2. PAT甲级——1118 Birds in Forest (并查集)

    此文章 同步发布在CSDN:https://blog.csdn.net/weixin_44385565/article/details/89819984   1118 Birds in Forest  ...

  3. 1118 Birds in Forest (25 分)

    1118 Birds in Forest (25 分) Some scientists took pictures of thousands of birds in a forest. Assume ...

  4. [并查集] 1118. Birds in Forest (25)

    1118. Birds in Forest (25) Some scientists took pictures of thousands of birds in a forest. Assume t ...

  5. PAT A 1118. Birds in Forest (25)【并查集】

    并查集合并 #include<iostream> using namespace std; const int MAX = 10010; int father[MAX],root[MAX] ...

  6. PAT题解-1118. Birds in Forest (25)-(并查集模板题)

    如题... #include <iostream> #include <cstdio> #include <algorithm> #include <stri ...

  7. 【PAT甲级】1118 Birds in Forest (25分)(并查集)

    题意: 输入一个正整数N(<=10000),接着输入N行数字每行包括一个正整数K和K个正整数,表示这K只鸟是同一棵树上的.输出最多可能有几棵树以及一共有多少只鸟.接着输入一个正整数Q,接着输入Q ...

  8. PAT A1118 Birds in Forest (25 分)——并查集

    Some scientists took pictures of thousands of birds in a forest. Assume that all the birds appear in ...

  9. 1118 Birds in Forest (25 分)

    Some scientists took pictures of thousands of birds in a forest. Assume that all the birds appear in ...

随机推荐

  1. Codeforces Round #327 (Div. 2)C. Median Smoothing 构造

    C. Median Smoothing   A schoolboy named Vasya loves reading books on programming and mathematics. He ...

  2. 500万url的es 批删除

    bash  循环 算术计算 读写文件 [root@hadoop2 ~]# sh looh.sh1234LIZ1 2 3 4 0 1 2 3 4 5 6 7 8 9 10 0games:x:12:100 ...

  3. ALSA声卡驱动中的DAPM详解之三:如何定义各种widget

    上一节中,介绍了DAPM框架中几个重要的数据结构:snd_soc_dapm_widget,snd_soc_dapm_path,snd_soc_dapm_route.其中snd_soc_dapm_pat ...

  4. 使用EL表达式正确情况下报错:javax.servlet.jsp cannot be resolved to a type

    这个错误可能是服务器自带的servlet库未导入的原因.右键项目属性,转到Targeted Runtimes,选择一个服务器,例如Tomcat,单击应用,可能就可以解决.

  5. java—容器学习笔记

    一:迭代器 刚开始学容器,做了个简单的练习题.. import java.util.ArrayList; import java.util.Collection; import java.util.I ...

  6. C. Searching for Graph(cf)

    C. Searching for Graph time limit per test 1 second memory limit per test 256 megabytes input standa ...

  7. [Swift通天遁地]四、网络和线程-(2)通过BlockOperation实现线程的队列

    ★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★➤微信公众号:山青咏芝(shanqingyongzhi)➤博客园地址:山青咏芝(https://www.cnblogs. ...

  8. [Swift通天遁地]九、拔剑吧-(5)创建Tab图标具有多种样式的Tab动画

    ★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★➤微信公众号:山青咏芝(shanqingyongzhi)➤博客园地址:山青咏芝(https://www.cnblogs. ...

  9. ACM_情人节

    情人节 Time Limit: 2000/1000ms (Java/Others) Problem Description: 某发每天都在各大群水啊水,然后认识了很多崇拜他的妹子,毕竟是数学专业.这不 ...

  10. C#最实用的快捷键

    Ctrl+J(Alt+→):智能提示. Ctrl+X:删除整行. Shift+Alt+Enter:全屏切换 F12:跳转到定义. Ctrl+-.Ctrl+Shift+-:上一步.下一步(仅限于使用过上 ...