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. python----函数与函数式编程

    一. 函数与函数式编程 1. 面向对象编程 (类)class 2.面向过程编程 (过程) def 3.函数式编程 (函数) def (1)  函数的特点:          代码重用:         ...

  2. react 简书开发笔记

    详见文章<React简书开发实战课程笔记>

  3. docker下搭建fastfds集群版

    搭建过程参考 作者 https://me.csdn.net/feng_qi_1984 的课程视频 声明:集群版是在我之前写的单机版基础之上进行搭建的,我将安装了fastfds单机版的docker打包成 ...

  4. <------------------字节流--------------------->

    字节流: 输入和输出:1.参照物都是java程序来惨遭 2.Input输入,持久化上的数据---->内存 3.Output输出,内存--->硬盘 字节输出流: OutputStream: ...

  5. haproxy prometheus 监控docker-compose 运行试用

    haproxy prometheus 的监控metrics 使用的是exporter ,因为haproxy 对于状态统计报告处理的 比较好,我们可以了stats 同时支持一个csv的api 接口,所以 ...

  6. redis使用问题总结

    1.redis使用过多内存导致其他进程无法正常运行情况:      解决方案:限制redis的最大使用内存,修改redis.conf中的maxmemory(一般不要超过空闲内存的3/5,如果不设置ma ...

  7. 项目构建之maven篇:5.仓库及nexus创建私服-2

    下载安装 下载地址 改动默认端口: home\conf\nexus.properties # Sonatype Nexus # ============== # This is the most ba ...

  8. C# 结构和类

    不同点: 1.结构是值类型,而类是引用类型:2.结构不支持继承,而类支持继承:3.结构不能定义构造函数,编译器会定义. 适用场合: 结构:分配内存快,作用域结束即被删除,不需要垃圾回收,适用于小型数据 ...

  9. hadoop需要哪些技术支持

    hadoop是一个开源软件框架,可安装在一个商用机器集群中,使机器可彼此通信并协同工作,以高度分布式的方式共同存储和处理大量数据.最初,Hadoop 包含以下两个主要组件:Hadoop Distrib ...

  10. shopnc-setNcCookie-后台验证码

    function setNcCookie($name, $value, $expire='3600', $path='', $domain='.a.cn', $secure=false){ if (e ...