[并查集] 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 ...
随机推荐
- 一些适合初学者的C/C++语言开发环境(IDE)
对于很多初学C语言的人来说,第一个开发环境应该都是VC6.0(没办法的事,很多高校都用VC6),在以前VC6确实是比较适合用来进行C/C++的学习. 但现在VC6已经不适合当前的环境了,更不适合新手. ...
- 小程序犯错(一):“ReferenceError: 模拟服务器传来的数据 is not defined”
学习数据绑定,在onLoad中模拟服务器传数据时,报错:模拟服务器传来的数据 is not defined 我这里粗心的忘记注释说明了,如下: 把该行无关的错误数据注释或删除即可.这里提醒同学们,出现 ...
- Flume定时启动任务 防止挂掉
一,查看Flume条数:ps -ef|grep java|grep flume|wc -l ==>15 检查进程:给sh脚本添加权限,chmod 777 xx.sh #!/bin/s ...
- JavaWeb基础—JS学习小结
JavaScript是一种运行在浏览器中的解释型的编程语言 推荐:菜鸟教程一.简介js:javascript是基于对象[哪些基本对象呢]和和事件驱动[哪些主要事件呢]的语言,应用在客户端(注意与面向对 ...
- meterpreter lhost设置
如果要监听kali本地的话,设置 0.0.0.0
- java.util.Arrays.asList 的小问题
JDK 1.4对java.util.Arrays.asList的定义,函数参数是Object[].所以,在1.4中asList()并不支持基本类型的数组作参数. JDK 1.5中,java.util. ...
- JavaScript函数作用域与对象以及实用技巧
1. JS作用域 1.1 全局作用域和局部作用域 函数外面声明的就是 全局作用域 函数内是局部作用域 全局变量可以直接在函数内修改和使用 变量,使用var是声明,没有var是使用变量. 如果在函数内使 ...
- 分享一个DataTable转List强类型的类库
类库扩展自Datatable,可以直接用Datatable.ToList<T>()进行转换.为了方便把DataReader装入Datatable,开扩展了一个LoadForReader(t ...
- 微信小程序中的 hover-class
微信小程序中,可以用 hover-class 属性来指定元素的点击态效果.但是在在使用中要注意,大部分组件是不支持该属性的. 目前支持 hover-class 属性的组件有三个:view.button ...
- 利用VBS脚本实现Telnet自动连接
把以下代码保存为*.vbs文件,替换IP.用户名.密码. Dim objShell Set objShell = CreateObject("Wscript.Shell") obj ...