[并查集] 1118. Birds in Forest (25)
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)的更多相关文章
- PAT A 1118. Birds in Forest (25)【并查集】
并查集合并 #include<iostream> using namespace std; const int MAX = 10010; int father[MAX],root[MAX] ...
- PAT题解-1118. Birds in Forest (25)-(并查集模板题)
如题... #include <iostream> #include <cstdio> #include <algorithm> #include <stri ...
- 1118. Birds in Forest (25)
Some scientists took pictures of thousands of birds in a forest. Assume that all the birds appear in ...
- 【PAT甲级】1118 Birds in Forest (25分)(并查集)
题意: 输入一个正整数N(<=10000),接着输入N行数字每行包括一个正整数K和K个正整数,表示这K只鸟是同一棵树上的.输出最多可能有几棵树以及一共有多少只鸟.接着输入一个正整数Q,接着输入Q ...
- PAT甲级——1118 Birds in Forest (并查集)
此文章 同步发布在CSDN:https://blog.csdn.net/weixin_44385565/article/details/89819984 1118 Birds in Forest ...
- 1118 Birds in Forest (25 分)
1118 Birds in Forest (25 分) Some scientists took pictures of thousands of birds in a forest. Assume ...
- PAT 1118 Birds in Forest [一般]
1118 Birds in Forest (25 分) Some scientists took pictures of thousands of birds in a forest. Assume ...
- PAT 1118 Birds in Forest
Some scientists took pictures of thousands of birds in a forest. Assume that all the birds appear in ...
- 1118 Birds in Forest
题意: 思路:并查集模板题. 代码: #include <cstdio> #include <algorithm> using namespace std; ; int fat ...
随机推荐
- 【转载】Spark学习 & 机器学习
然后看的是机器学习这一块,因为偏理论,可以先看完.其他的实践,再看. http://www.cnblogs.com/shishanyuan/p/4747761.html “机器学习是用数据或以往的经验 ...
- 【python】Selenium隐藏控制台解决办法
一.起因: 使用selenium 驱动浏览器的时候,如果使用headless模式,会有dos窗口弹出,输出监听信息,有时不想看到,很是麻烦. 二.解决办法: 修改源码:Lib \ site-packa ...
- jQuery学习-访问设置元素内容
<!DOCTYPE html> <html> <head> <meta charset="UTF-8"> <title> ...
- 【转载】MFC的Main函数跑哪去了
原文:http://blog.csdn.net/weiwenhp/article/details/8455471 习惯的思维 用习惯了C的人要看一个程序时首先会想到找到那个main函数在哪,然后再顺着 ...
- VS2013在Windows7 64位上变慢的解决方法
重装了windows7系统,又重装了vs2013,发现在打开vs2013.编译工程及调试的时候,vs2013都会变的比较慢,参考网上资料,这里列出几种可能的解决方法: 1. 打开工具--&g ...
- 【LG3249】[HNOI2016]矿区
[LG3249][HNOI2016]矿区 题面 洛谷 题解 先平面图转对偶图, 建好了对偶图之后随意拿出一个生成树,以无边界的范围为根. 无边界的范围很好求,用叉积算出有向面积时,算出来是负数的就是无 ...
- Kubernetes学习之路(一)之概念和架构解析和证书创建和分发
1.Kubernetes的重要概念 转自:CloudMan老师公众号<每天5分钟玩转Kubernetes>https://item.jd.com/26225745440.html Clus ...
- 解决 spring-test 出现 Failed to load ApplicationContext 的异常
在使用spring-test的时候,在启动@Test的方法时,spring-test会去加载spring的配置文件,这个时候如果配置文件没有在 @ContextConfiguration 中写全,就会 ...
- ATmega8仿真——键盘扫描的学习
1.按键的使用特点 按键的应用主要是在按键闭合时改变电路的电平,但是一般情况下按键的开关都是机械弹性触点开关,即利用触点的接触和分离来实现电路的通断,所以在按键按下和释放时往往会产生抖动干扰. 消除抖 ...
- java单元测试的用法及原因
1.ctrl+n 生成 Junit Test Case 2.选择文件夹 3.superClass 继承BaseUnitTest 4.next后 打勾选择需要单元测试的方法. 5.在生成的test ...