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. WebService实例-CRM系统提供WebService实现用户注册功能

    <—start—> 编写crm的webservice接口,实现客户信息保存操作.在CustomerService接口中新增一个服务接口,用于添加客户注册的信息. @Path("/ ...

  2. jQuery操作复选框checkbox技巧总结 ---- 设置选中、取消选中、获取被选中的值、判断是否选中等

    转载:https://blog.csdn.net/chenchunlin526/article/details/77448168 jQuery操作复选框checkbox技巧总结 --- 设置选中.取消 ...

  3. WPF 如何创建自己的WPF自定义控件库

    在我们平时的项目中,我们经常需要一套自己的自定义控件库,这个特别是在Prism这种框架下面进行开发的时候,每个人都使用一套统一的控件,这样才不会每个人由于界面不统一而造成的整个软件系统千差万别,所以我 ...

  4. 莫烦keras学习自修第五天【CNN卷积神经网络】

    1.代码实战 #!/usr/bin/env python #! _*_ coding:UTF-8 _*_ import numpy as np np.random.seed(1337) # for r ...

  5. 不幸,我的Ryzen 7 1700X中招了,也有segfault

    在历经了I7-5775C,I7-5820K之后,决定尝鲜用一下为AMD漂亮翻身的Ryzen 7,海淘了一颗Ryzen 7 1700X 最近听说在极重负载的情况下,CPU会出错,于是从网上找来Kill- ...

  6. bmi

    <!DOCTYPE html> <html> <head> <meta charset="UTF-8"> <title> ...

  7. hdu-3746(kmp)

    题意:给你一个字符串,问你至少增添几个字符可以把这个字符串变成一个循环字符串(ababa的循环节是ab,不是aba): 解题思路:利用kmp中的next数组,首先在这样求next的数组的代码里: vo ...

  8. python 模块之-json

    python 模块json import json x="[null,true,false,1]" print(json.loads(x))   #---------------- ...

  9. eclipse中无法查看引用的jar包源码

    本文来源:http://blog.csdn.NET/zljjava/article/details/7545270(这篇博客也是转载的,向最原始的作者致敬) 1.下载JAD jad官方地址的官方下载地 ...

  10. BZOJ3876[Ahoi2014&Jsoi2014]支线剧情——有上下界的最小费用最大流

    题目描述 [故事背景] 宅男JYY非常喜欢玩RPG游戏,比如仙剑,轩辕剑等等.不过JYY喜欢的并不是战斗场景,而是类似电视剧一般的充满恩怨情仇的剧情.这些游戏往往 都有很多的支线剧情,现在JYY想花费 ...