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. pci枚举初始化部分(1)

    基于linux-4.20-rc3源码分析 1 .扫描所有PCI设备并检测,填充设备结构体 static struct pci_dev *pci_scan_device(struct pci_bus * ...

  2. Get與Post的區別--總結隨筆

    關於Get與Post的區別的文章,在網上太多了:有優點有缺點,今天我給各位大哥做一個總結性的隨筆,還請多多包涵~ 首先是W3School上的答案,請查收: GET在浏览器回退时是无害的,而POST会再 ...

  3. go 交叉编译,部署

    go web 部署 交叉编译 go 语言有个强大的地方就是 交叉编译 windows --cmd 设置环境变量-mac SET CGO_ENABLED=0 SET GOOS=darwin SET GO ...

  4. 使用MATLAB工具deploytool打包不成功的解决办法

    下面解决方法是来自百度贴吧一抹伊静的解决办法,很好用,非常感谢,现记录下来以便后续查看: 这个问题是2014a 需要破解:下载一个破解文件,原因是matlab未完全破解,2014a的破解办法:http ...

  5. springboot快速入门(一)——HelloWorld搭建

    一.起步 1.先导 凡技术必登其官网的原则,官网走一波:https://projects.spring.io/spring-boot/#quick-start 极力推荐一个springboot教程:h ...

  6. 20155236范晨歌 Exp7 网络欺诈技术防范

    20155236范晨歌 Exp7 网络欺诈技术防范 虚拟机经常崩= =,所以基本上做一个实验换一个... 遇到了一个简单的小问题老师帮忙解决了:虚拟机没联网... 一.基础问题回答 通常在什么场景下容 ...

  7. 补交课下测试(ch12并发编程) 08.第八周

    有关线程图,下面说法正确的是() A .图的原点表示没有任何线程完成一条指令的初始状态 B . 向右向上是合法的转换 C .向左向下是合法的转换 D .对角线是合法的转换 E .一个程序执行的历史被模 ...

  8. 洛咕 P2480 [SDOI2010]古代猪文

    洛咕 P2480 [SDOI2010]古代猪文 题目是要求\(G^{\sum_{d|n}C^d_n}\). 用费马小定理\(G^{\sum_{d|n}C^d_n\text{mod 999911658} ...

  9. IT程序员每天的困扰:这TM为啥不可以?这TM也行?

    如果有对 Python 感兴趣的程序员,可以加我们小助手的QQ:979950755 会免费送 Python 的视频教程噢! 随着IT互联网对社会的影响越来越重要,关乎人类的未来发展进程.所以现在很多媒 ...

  10. SSD固态硬盘的GC与Trim

    操作系统:其实并没有删除数据: 事实上,它只是在硬盘前的索引区里标记这块文件占用的区域为无效的, 所以等该区域被擦除后,下次数据将要再次写入的时候,可以写入这块被标记的区域. 这也就是为啥那 些所谓的 ...