PAT1118. Birds in Forest (并查集)
思路:并查集一套带走。
AC代码
#include <stdio.h>
#include <string.h>
#include <algorithm>
using namespace std;
const int maxn = 10000+5;
int par[maxn], vis[maxn];
int findRoot(int x) {
return x == par[x] ? x : par[x] = findRoot(par[x]);
}
void unionSet(int x, int y) {
x = findRoot(x);
y = findRoot(y);
if(x != y) {
par[x] = y;
}
}
void init(int n) {
memset(vis, 0, sizeof(vis));
for(int i = 0; i <= n; i++) {
par[i] = i;
}
}
int main() {
int n, k, q, len;
len = -1;
scanf("%d", &n);
init(maxn);
for(int i = 0; i < n; i++) {
int x, y;
scanf("%d%d", &k, &x);
len = max(len, x);
for(int j = 1; j < k; j++) {
scanf("%d", &y);
len = max(len, y);
unionSet(x, y);
x = y;
}
}
int tol = 0;
for(int i = 1; i <= len; i++) {
int root = findRoot(i);
if(!vis[root]) {
vis[root] = 1;
tol++;
}
}
printf("%d %d\n", tol, len);
scanf("%d", &q);
int x, y;
for(int i = 0; i < q; i++) {
scanf("%d%d", &x, &y);
x = findRoot(x);
y = findRoot(y);
if(x == y) {
printf("Yes\n");
} else {
printf("No\n");
}
}
return 0;
}
如有不当之处欢迎指出!
PAT1118. Birds in Forest (并查集)的更多相关文章
- PAT1118:Birds in Forest
1118. Birds in Forest (25) 时间限制 150 ms 内存限制 65536 kB 代码长度限制 16000 B 判题程序 Standard 作者 CHEN, Yue Some ...
- CodeForces 755C PolandBall and Forest (并查集)
题意:给定每一点离他最远的点,问是这个森林里有多少棵树. 析:并查集,最后统计不同根结点的数目即可. 代码如下: #pragma comment(linker, "/STACK:102400 ...
- PAT A1118 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 (并查集)
此文章 同步发布在CSDN:https://blog.csdn.net/weixin_44385565/article/details/89819984 1118 Birds in Forest ...
- 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)
1118. Birds in Forest (25) Some scientists took pictures of thousands of birds in a forest. Assume t ...
- Codeforces 755C:PolandBall and Forest(并查集)
http://codeforces.com/problemset/problem/755/C 题意:该图是类似于树,给出n个点,接下来p[i]表示在树上离 i 距离最远的 id 是p[i],如果距离相 ...
- CodeForces - 755C PolandBall and Forest (并查集)
题意:给定n个数,Ai的下标为1~n.对于每一个i,Ai与i在同一个树上,且是与i最远的点中id最小的点(这个条件变相的说明i与Ai连通).求森林中树的个数. 分析:若i与Ai连通,则在同一个树上,因 ...
随机推荐
- python_如何实现可迭代对象和迭代器对象?
什么是可迭代对象? 列表.字符串 for循环的本质? for循环要确保in后面的对象为可迭代对象,如何确保? iter() 方法得到一个迭代器对象 不停.__next__() 方法对迭代器对象进行迭代 ...
- 三十天学不会TCP,UDP/IP网络编程 - UDP的实践--DHCP
在经历了一顿忙碌加出去玩了玩之后,我又开始重新更新了~这是最新的一篇~完整版可以去gitbook(https://www.gitbook.com/@rogerzhu/)看到,在gitbook的后台流量 ...
- Intel DPDK 全面解读
高性能网络技术 随着云计算产业的异军突起,网络技术的不断创新,越来越多的网络设备基础架构逐步向基于通用处理器平台的架构方向融合,从传统的物理网络到虚拟网络,从扁平化的网络结构到基于 SDN 分层的网络 ...
- libev学习笔记
转 libev的使用--结合Socket编程 作者:cxy450019566 之前自己学过一些libev编程的基础,这次写压测刚好用上了,才算真正动手写了些东西,在这里做一些总结.写这篇文章是为了用浅 ...
- awk中引用shell变量执行替换的脚本
遇到问题: 现在有两个脚本,我想 将file1中的内容按file2来匹配 [root@154 home]# cat file1 3-1-1 POTV=1,POTA=0,POTP=2 1-4-76 PO ...
- iometer测试工具
简介 Iometer 为计算机I/O子系统所作的工作就如同测力计为引擎所作的工作一样:它测定在可控制的负荷下系统的性能.Iometer 以前被称为"伽利略". Iometer 既是 ...
- 15_Python函数名本质
函数名的本质 函数名实质上就是函数的内存地址 def wrapper(): pass print(wrapper) 1.引用是什么? 当我们定义a=1的时候,系统会开辟一块内存空间来保存1,然后用a变 ...
- My Calendar III
class MyCalendarThree(object): """ Implement a MyCalendarThree class to store your ev ...
- Django的ModelForm
基于django.forms.ModelForm:与模型类绑定的Form 先定义一个ModelForm类,继承ModelForm类 from django.forms import ModelForm ...
- 排序算法之NB三人组
快速排序 思路: 例如:一个列表[5,7,4,6,3,1,2,9,8], 1.首先取第一个元素5,以某种方式使元素5归位,此时列表被分为两个部分,左边的部分都比5小,右边的部分都比5大,这时列表变成了 ...