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<cstdio>
#include<iostream>
#include<algorithm>
using namespace std;
int father[];
int findRoot(int x){
int temp = x;
while(x != father[x]){
x = father[x];
}
int temp2;
while(temp != x){
temp2 = father[temp];
father[temp] = x;
temp = temp2;
}
return x;
}
void union_(int a, int b){
int fa = findRoot(a);
int fb = findRoot(b);
if(fa == fb)
return;
father[fb] = fa;
}
int main(){
int N, K, cnt = -;
for(int i = ; i < ; i++){
father[i] = i;
}
scanf("%d", &N);
for(int i = ; i < N; i++){
int b1, b2, maxb;
scanf("%d%d", &K, &b1);
cnt = max(b1, cnt);
for(int j = ; j < K; j++){
scanf("%d", &b2);
union_(b1, b2);
maxb = max(b1, b2);
cnt = max(maxb, cnt);
}
}
int tree = ;
for(int i = ; i <= cnt; i++){
if(father[i] == i)
tree++;
}
printf("%d %d\n", tree, cnt);
int Q;
scanf("%d", &Q);
for(int i = ; i < Q; i++){
int q1, q2;
scanf("%d%d", &q1, &q2);
if(findRoot(q1) == findRoot(q2))
printf("Yes\n");
else printf("No\n");
}
cin >> N;
return ;
}

1、因为birds并不是连续出现的,需要用set来存储bird的编号。

A1118. Birds in Forest的更多相关文章

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

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

  2. PAT甲级——A1118 Birds in Forest【25】

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

  3. PAT_A1118#Birds in Forest

    Source: PAT A1118 Birds in Forest (25 分) Description: Some scientists took pictures of thousands of ...

  4. 1118 Birds in Forest (25 分)

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

  5. PAT1118:Birds in Forest

    1118. Birds in Forest (25) 时间限制 150 ms 内存限制 65536 kB 代码长度限制 16000 B 判题程序 Standard 作者 CHEN, Yue Some ...

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

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

  7. PAT 1118 Birds in Forest [一般]

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

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

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

  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. Windows 10 & change DNS

    Windows 10 & change DNS https://www.windowscentral.com/how-change-your-pcs-dns-settings-windows- ...

  2. JAVA不可变类(immutable)机制与String的不可变性--非常好.

    JAVA不可变类(immutable)机制与String的不可变性 https://www.cnblogs.com/jaylon/p/5721571.html

  3. 深度学习最全优化方法总结比较(SGD,Adagrad,Adadelta,Adam,Adamax,Nadam)(转)

    转自: https://zhuanlan.zhihu.com/p/22252270    ycszen 另可参考: https://blog.csdn.net/llx1990rl/article/de ...

  4. 获取网络图片并显示在picturbox上,byte[]数组转换成Image:

    private void getWebPicture_Click(object sender, EventArgs e) { WebRequest request = WebRequest.Creat ...

  5. web.xml中三种通配符及匹配规则

    一.url-pattern的三种写法 1.精确匹配.以”/”开头,加上servlet名称:    /ad  ; 2.路径匹配.以”/”开头,加上通配符”*” :    /*  ; 3.扩展名匹配.以通 ...

  6. C#,单元测试

    C#,单元测试入门(以下内容可能来自网络) 一.什么叫单元测试(unit testing)? 是指对软件中的最小可测试单元进行检查和验证.对于单元测试中单元的含义,一般来说,要根据实际情况去判定其具体 ...

  7. 学习 Spring (十七) Spring 对 AspectJ 的支持 (完结)

    Spring入门篇 学习笔记 @AspectJ 的风格类似纯 java 注解的普通 java 类 Spring 可以使用 AspectJ 来做切入点解析 AOP 的运行时仍旧是纯的 Spring AO ...

  8. Upload Files In ASP.NET Core 1.0 (Form POST And JQuery Ajax)

    Uploading files is a common requirement in web applications. In ASP.NET Core 1.0 uploading files and ...

  9. Web API2 使用默认Identity

    当您选择个人账户在Web API项目模板,项目包含一个令牌授权服务器验证用户凭证和问题.下面的图显示了相同的凭证流的Web API组件. 发送一个未经授权的请求 首先,运行应用程序并单击按钮调用的AP ...

  10. BZOJ3223文艺平衡树——非旋转treap

    此为平衡树系列第二道:文艺平衡树您需要写一种数据结构,来维护一个有序数列,其中需要提供以下操作: 翻转一个区间,例如原有序序列是5 4 3 2 1,翻转区间是[2,4]的话,结果是5 2 3 4 1 ...