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连通,则在同一个树上,因 ...
随机推荐
- 解决Flink输出日志中时间比当前时间晚8个小时的问题
Flink安装在CentOS7上,默认时间是UTC时间,查看Flink日志,发现输出时间比当前时间晚8个小时. 通过如下命令,调整成北京时间 cp /usr/share/zoneinfo/Asia/S ...
- Sql Server的艺术(六) SQL 子查询,创建使用返回多行的子查询,子查询创建视图
子查询或内部查询或嵌套查询在另一个SQL查询的查询和嵌入式WHERE子句中. 子查询用于返回将被用于在主查询作为条件的数据,以进一步限制要检索的数据. 子查询可以在SELECT,INSERT,UPDA ...
- 使用CefSharp开发一个12306“安心刷票弹窗通知”工具
有需求就要改进 最近两年没有在春节回家过年了,主要是票太难买,虽然之前写了一个12306“无声购票弹窗”工具,解决了抢票问题,但是全家老小一起回去还是很累,干脆就在北京过年了.这两天突然有一个朋友问我 ...
- 什么是<!DOCTYPE html>
什么是<!DOCTYPE html>? 在HTML文档初,往往会有这么一句话<!DOCTYPE html>,那么它的意义是什么呢?它是html5标准网页声明,全称为Docume ...
- centos7添加图像化桌面并设置中文
我前面是使用的centos6.最近才最小化安装了一个centos7.4(最小化安装有很多命令都没有,所以不建议这样干).完了装了图形化界面和设置中文,感觉和centos6有些区别,所以记录一下过程. ...
- asp.net 发布程序到iis后无法连接到oralce数据库问题
在应用程序池里面,选中你的站点所使用的应用程序池->高级设置->启用32位应用程序->true
- spring jdbc踩坑日记,new JdbcTemplate 为null导致UserDao一直为null
private DataSource datasource; private JdbcTemplate jdbcTemplateObject; //设置注入 public void setdataso ...
- 使用JavaScript实现机器学习和神经学网络
欢迎大家前往云+社区,获取更多腾讯海量技术实践干货哦~ 下载heaton-javascript-ml.zip - 45.1 KB 基本介绍 在本文中,你会对如何使用JavaScript实现机器学习这个 ...
- xBIM IFC 墙壁案例
目录 xBIM 应用与学习 (一) xBIM 应用与学习 (二) xBIM 基本的模型操作 xBIM 日志操作 XBIM 3D 墙壁案例 xBIM 格式之间转换 xBIM 使用Linq 来优化查询 x ...
- MessagePack简析
一.MessagePack是什么 先看官方的定义:MessagePack是一种高效的二进制序列化格式.它允许您像JSON一样在多个语言之间交换数据.但是,它更快并且更小. 从官方定义中,可以有如下的结 ...