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. this关键字的使用8/22

    实质就是:this代表当前对象目录: 1.this(name) 调用同一个类中参数为 public Person(String name)这个构造方法 2.this.say() 同一个类中,某一个方法 ...

  2. linux运维注意事项

    防火墙和selinux一定要注意 服务器或应用出现错误是一定要查看服务器运行状态是否正确,及其相关运行日志 不管什么时候,能不要特殊字符就不要用特殊字符,避免不必要的错误 在看部署文档的时候一定要认真 ...

  3. s3c2410串口笔记

  4. day 60 Bootstrip学习

    图标地址 http://fontawesome.io/icons/ 图标用法地址 http://fontawesome.io/examples/ 实现代码 <!DOCTYPE html> ...

  5. 使用loki+ mtail + grafana + prometheus server分析应用问题

    loki 是一个方便的类似prometheus 的log 系统,mtail 是一个方便的日志提取工具, 可以暴露为http 服务——支持导出prometheus metrics 环境准备 docker ...

  6. 07机器学习实战k-means

    K-Means原理初探 K-Means算法的思想很简单,对于给定的样本集,按照样本之间的距离大小,将样本集划分为K个簇.让簇内的点尽量紧密的连在一起,而让簇间的距离尽量的大. 如果我们想直接求上式的最 ...

  7. AspNetCore+Swagger 生成Model描述

    AspNetCore+Swagger 生成Model 描述 前言: 本篇文章实现是基于上一篇文章,进下补充:多余的就不多说了,只是为了实现Model的描述生成:有兴趣的可以结合上一篇的进行实现:如有更 ...

  8. java_main

    Java中用户向系统传递参数的三种基本方式 main方法 在Java中,main()方法是Java应用程序的入口方法,也就是说,程序在运行的时候,第一个执行的方法就是main()方法,这个方法和其他的 ...

  9. DocFetcher 本机文件搜索工具

    优点: 支持的文件类型多 全文搜索 可以随时update索引

  10. VARCHAR(N)类型,utf8编码,则N最大值为多少,n表示什么?

    4.0版本以下,varchar(20),指的是20字节,如果存放UTF8汉字时,只能存6个(每个汉字3字节) 5.0版本以上,varchar(20),指的是20字符,无论存放的是数字.字母还是UTF8 ...