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 (<= 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 <iostream>
#include <cstdio>
#include <cmath>
#include <cstring>
#include <algorithm>
#include <vector>
using namespace std; const int maxn=1e4+; int father[maxn];
int num[maxn]={};
int flag[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 uf(int a,int b)
{
int fa=findFather(a);
int fb=findFather(b);
if(fa!=fb)
{
father[fa]=fb;
num[fa]+=num[fb];
}
} int max_num=;
int ans=; int main()
{
for(int i=;i<maxn;i++) father[i]=i;
fill(num,num+maxn,);
int n;
scanf("%d",&n);
for(int i=;i<n;i++)
{
int k,first;
scanf("%d %d",&k,&first);
flag[first]=;
max_num=first>max_num?first:max_num;
for(int j=;j<k;j++)
{
int a;
scanf("%d",&a);
flag[a]=;
max_num=max_num>a?max_num:a;
uf(a,first);
}
}
int cnt=;
for(int i=;i<=max_num;i++)
{
//printf(" %d",father[i]);
if(father[i]==i) cnt++;
}
for(int i=;i<maxn;i++) ans+=flag[i];
printf("%d %d\n",cnt,ans);
int q;
scanf("%d",&q);
for(int i=;i<q;i++)
{
int u,v;
scanf("%d%d",&u,&v);
if(findFather(u)==findFather(v))
{
printf("Yes\n");
}
else
{
printf("No\n");
}
}
return ;
}

[并查集] 1118. Birds in Forest (25)的更多相关文章

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

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

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

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

  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甲级】1118 Birds in Forest (25分)(并查集)

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

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

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

  6. 1118 Birds in Forest (25 分)

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

  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

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

  9. 1118 Birds in Forest

    题意: 思路:并查集模板题. 代码: #include <cstdio> #include <algorithm> using namespace std; ; int fat ...

随机推荐

  1. mssqlserver的md5函数

    参考:https://www.cnblogs.com/JuneZhang/p/6396896.html?utm_source=itdadao&utm_medium=referral 简单说明: ...

  2. Java面向对象六大原则

    引用自百度知道: ——根据首字母快速记忆SOLID(固体,坚固的),具体请参考这里 1) Open-Close Principle(OCP),开-闭原则, 讲的是设计要对扩展有好的支持,而对修改要严格 ...

  3. 使用 Django WebSocket Redis 搭建在线即时通讯工具

    话不多说先上效果图演示 项目:http://112.74.164.107:9990/ 1.安装组建 redis: yum install redis/apt install redis 2.创建虚拟化 ...

  4. UWP 卡片视图 Card View

    上一篇 提到了 UWP 轨道视图Orbit View,这次就说一下卡片视图,毕竟两个差不多. 卡片视图,效果如其名,卡片一样,左右滑动,当然能翻牌最好了. 嗯,我这个可以的额(⊙﹏⊙)... 看下效果 ...

  5. TCP/IP 协议簇 端口 三次握手 四次挥手 11种状态集

    第1章 概念介绍 1.1 VLAN 1.1.1 什么是VLAN VLAN(Virtual LAN),翻译成中文是“虚拟局域网”.LAN可以是由少数几台家用计算机构成的网络,也可以是数以百计的计算机构成 ...

  6. 大数据中HBase集群搭建与配置

    hbase是分布式列式存储数据库,前提条件是需要搭建hadoop集群,需要Zookeeper集群提供znode锁机制,hadoop集群已经搭建,参考 Hadoop集群搭建 ,该文主要介绍Zookeep ...

  7. k8s常用命令记录

    目录 kubectl常用命令 kubectl get pod -n dev 查看日志 查看pod详情 删除pod 删除job 进入pod里面 查看namespace 创建namespace 删除nam ...

  8. oracle存储过程 关于update的动态SQL-工作心得

    本随笔文章,由个人博客(鸟不拉屎)转移至博客园 发布时间: 2018 年 12 月 20 日 原地址:https://niaobulashi.com/archives/oracle-procedure ...

  9. 解决java读取大文件内存溢出问题

    1. 传统方式:在内存中读取文件内容 读取文件行的标准方式是在内存中读取,Guava 和Apache Commons IO都提供了如下所示快速读取文件行的方法: Files.readLines(new ...

  10. 4. 为HelloWorld添加日志

    回顾 通过上篇内容,我们已经使用flask编写了我们的第一个接口或者说是html页面.我们可以看到,使用flask来编写接口/页面是十分简单的.那么接下来,我们丰富一下上面的例子. 需求 现在的需求来 ...