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

K B​1​​ B​2​​ ... B​K​​

where K is the number of birds in this picture, and B​i​​'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 10​4​​.

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

分析:并查集,水题。
 /**
 * Copyright(c)
 * All rights reserved.
 * Author : Mered1th
 * Date : 2019-02-27-20.04.59
 * Description : A1118
 */
 #include<cstdio>
 #include<cstring>
 #include<iostream>
 #include<cmath>
 #include<algorithm>
 #include<string>
 #include<unordered_set>
 #include<map>
 #include<vector>
 #include<set>
 using namespace std;
 ;
 int father[maxn];
 int findFather(int x){
     int a=x;
     while(x!=father[x]){
         x=father[x];
     }
     while(a!=father[a]){
         int z=a;
         a=father[a];
         father[z]=x;
     }
     return x;
 }

 void Union(int a,int b){
     int faA=findFather(a);
     int faB=findFather(b);
     if(faA!=faB){
         father[faB]=faA;
     }
 }

 int main(){
 #ifdef ONLINE_JUDGE
 #else
     freopen("1.txt", "r", stdin);
 #endif
     ;
     scanf("%d",&n);
     ;i<maxn;i++) father[i]=i;
     ;i<n;i++){
         scanf("%d%d",&k,&t);
         maxin=max(maxin,t);
         ;j<k-;j++){
             scanf("%d",&t1);
             maxin=max(maxin,t1);
             Union(t,t1);
         }
     }
     set<int> tree;
     ;i<=maxin;i++){
         tree.insert(findFather(i));
     }
     printf("%d %d\n",tree.size(),maxin);
     int query,u,v;
     scanf("%d",&query);
     ;i<query;i++){
         scanf("%d%d",&u,&v);
         if(findFather(u)==findFather(v)) printf("Yes\n");
         else printf("No\n");
     }
     ;
 }
 

1118 Birds in Forest (25 分)的更多相关文章

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

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

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

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

  3. 1118. Birds in Forest (25)

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

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

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

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

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

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

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

  7. PAT 1118 Birds in Forest [一般]

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

  8. 1118 Birds in Forest (25 分)

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

  9. PAT 1118 Birds in Forest

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

随机推荐

  1. js中数组的去重

    第一种方式: var ss=['小红','小花','小兰','小花'] var uu=[] for(var i=0;i<ss.length;i++){ if(uu.indexOf(ss[i])= ...

  2. 《DSP using MATLAB》Problem 5.7

    代码: %% ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ %% Output In ...

  3. mysql给数据库授权与收回权限--------dcl

    用户授权语法 grant 权限1,权限2... on 数据库名.* to 用户名 @IP地址或% 打开新创建的名为“test”的数据库后 用 show databases;  的命令 看内部的数据结果 ...

  4. 【mysql】创建索引

    一.联合唯一索引 项目中需要用到联合唯一索引: 例如:有以下需求:每个人每一天只有可能产生一条记录:处了程序约定之外,数据库本身也可以设定: 例如:t_aa 表中有aa,bb两个字段,如果不希望有2条 ...

  5. linux 之sed

    sed 用法 sed [-nefr] [action] -i 直接修改文件内容,而不是像其他命令那样只是输出到终端 a新增c取代d删除i插入p列印常与sed -n 使用s取代 有一点需要注意的是:如果 ...

  6. Object 及toString() 方法的重写

    Object: 是所有的类的父类  ,Object中所有的方法 , 子类都能使用  ,   接口不是Object子类. Person: /*将父类的equals方法 重写 * 不改变父类的源代码 eq ...

  7. pnpm 快速节省磁盘工具的包管理工具

    nodejs 相关的包管理工具有很多,我们常用的有 npm cnpm(我基本已经不用了),yarn... pnpm 是另外一个不错的包管理工具,包含以下特性 快速 节省空间,一个版本的包只会在磁盘中存 ...

  8. lch 儿童围棋课堂 初级篇2 (李昌镐 著)

    第1章 吃子 第2章 死活:实战演练 第3章 劫争 第4章 布局:定式篇 第5章 布局:三线,四线和大场 第6章 布局:布局类型与形势判断 第7章 中盘:分断 第8章 官子:价值的计算 第9章 对杀技 ...

  9. java中的ArrayList 、List、LinkedList、Collection

    原文地址: http://www.cnblogs.com/liqiu/p/3302607.html 一.基础介绍(Set.List.Map) Set(集):集合中的元素不按特定方式排序,并且没有重复对 ...

  10. 排序算法<No.4>【基数排序】

    由于春节,以及项目要上线的原因,导致这期的算法博文跟进的有点慢,内疚! 今天要介绍的是排序算法中的基数排序(Radix Sort),这类排序也是一个分而治之的排序,是对桶排序的一个升级和改造,也是稳定 ...