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. C语言数组截取常见问题

    这是一段数组截取的代码,函数功能是检查后面的'*'的数量,若多于指定输入数,则把多余的'*'删除 void fun( char *a,int n ) { char *p = a; while(*p) ...

  2. Bluebox Security最新提报Android漏洞的初步探讨(转)

    Bluebox Security在7月3号的时候,在官网上发布了一个据称99%  Android机器都有的一个漏洞.国内最早在4号开始有媒体报道,并持续升温.该漏洞可使攻击者在不更改Android应用 ...

  3. 如何查看PostgreSQL的checkpoint 活动

    磨砺技术珠矶,践行数据之道,追求卓越价值 回到上一级页面:PostgreSQL基础知识与基本操作索引页    回到顶级页面:PostgreSQL索引页 作者:高健@博客园 luckyjackgao@g ...

  4. Git 解决添加到.gitignore的忽略项不生效的问题

    今天又在.gitignore添加了一些忽略项,但是后来发现一些东西命名配置了忽略项却还是没起作用,so,分析原因,可能是在我添加忽略项之前,因为这些文件就早已经被提交了,所有他们已经在版本控制中,导致 ...

  5. springboot之websocket,STOMP协议

    一.WebSocket 是 HTML5 开始提供的一种在单个 TCP 连接上进行全双工通讯的协议. WebSocket 使得客户端和服务器之间的数据交换变得更加简单,允许服务端主动向客户端推送数据.在 ...

  6. ORM框架学习之EF

    首先推荐一篇很好的EF文章翻译,可以系统的学习一遍. <Entity Framework 6 Recipes>中文翻译系列 EF使用体会 优点: 可以省去Ado.net复杂的管道连接代码. ...

  7. WebGL——osg框架学习三

    今天继续来Draw绘制的osg模块的学习,昨天我们学习的是StateBin渲染状态树节点类,今天我们来继续学习下一个Draw的基础类DrawableEntity渲染对象实体类.这个类和Drawable ...

  8. vue mock(模拟后台数据) 最简单实例(一)——适合小白

    开发是前后端分离,不需要等待后台开发.前端自己模拟数据,经本人测试成功. 我们在根目录新建存放数据的json文件,存放我们的数据data.json //data.json{ "status& ...

  9. NIO基本概念

    1.  IO和NIO的区别 IO     面向流(stream oriented)  阻塞(blocking io) 无                  NIO  面向缓冲区(buffer orie ...

  10. Tomcat java zabbix 监控

    排除汤姆猫错误的步骤 ps-ef | grep java或jps –lvm 查看java pid进程 netstat –lntup | grep java 查看java 端口有没有启动 查看 tomc ...