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. Socket和ObjectOutputStream问题

    用到Socket序列化对象网络传输时ObjectOutputStream一直刷新连接 用户代码 package com.jachs.ladflower.ladflower; import java.n ...

  2. django_filter,Search_Filter,Order_Filter,分页

    一.分页drf配置信息: 1.在Lib\site-packages\rest_framework\settings.py中查看: 2.简单分页在项目setting中配置:(所有get请求返回数据每页5 ...

  3. linux audit审计(2)--audit启动

    参考:https://access.redhat.com/documentation/en-us/red_hat_enterprise_linux/7/html/security_guide/sec- ...

  4. 初识Anrdiod SDK

    概念 SDK:(software development kit)软件开发工具包.被软件开发工程师用于为特定的软件包.软件框架.硬件平台.操作系统等建立应用软件的开发工具的集合. 因此,Android ...

  5. Express学习 ------模版引擎(handlebars)

    Handlebars一款js模版引擎,我们在做客户端开发的时候,也可能已经使用过.它语法比较简单,和我们平常写的html 一样,只不过html 中可以加入handlebars 表达式. handleb ...

  6. python中的split()方法的使用

    Python split()方法:通过指定分隔符对字符串进行分割并返回一个列表,默认分隔符为所有空字符,包括空格.换行(\n).制表符(\t)等. l  Str.split()默认以空格,换行\n,制 ...

  7. PHP 公共方法分享180628

    查看php 类的详情:方法.常量.属性( type(new \Illuminate\Http\Request());) /** * fixme 打印类详情 * @param $class object ...

  8. Android如何着色字符串的特定部分

    文章选自StackOverflow(简称:SOF)精选问答汇总系列文章之一,本系列文章将为读者分享国外最优质的精彩问与答,供读者学习和了解国外最新技术.本文探讨Android如何着色字符串的特定部分. ...

  9. 【LOJ#6074】子序列(动态规划)

    [LOJ#6074]子序列(动态规划) 题面 LOJ 题解 考虑一个暴力\(dp\). 设\(f[i][c]\)表示当前在第\(i\)位,并且以\(c\)结尾的子序列个数. 那么假设当前位为\(a\) ...

  10. [luogu2446][bzoj2037][SDOI2008]Sue的小球【区间DP】

    分析 简单区间DP, 定义状态f[i][j][0/1]为取完i-j的小球最后取i/j上的小球所能获得的最大价值. 排序转移. ac代码 #include <bits/stdc++.h> # ...