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连通,则在同一个树上,因 ...
随机推荐
- navicat的简单使用
navicat的简单使用: 连接: 输入ip地址,端口,用户名,密码 新建数据库: 数据库名,字符编码一定要选择utf-8 新建表: 字段,约束条件 双击表名,自己打开表,点击空列,添加数据,ctl+ ...
- Python初识 - day5
一.装饰器(decorator) 1.定义:本质是函数(装饰其它函数),就是为了其它函数添加附加功能. 2.原则:一是不能修改被装饰函数的源代码:二是不能修改被装饰函数的调用方式. 3.装饰器包含的知 ...
- Python初识-day1
1.第一个python程序(在pycharm中运行的,感觉还不错) 注释: 当行注释:#被注释内容 多行注释:''' 被注释内容 ''' 2.变量 (1) 自己理解的定义(仅供参考): 为了存储数据 ...
- MySQL--pt-osc工具学习
##=====================================================##pt-osc之工作流程:1.检查更改表是否有主键或唯一索引,是否有触发器2.检查修改表 ...
- Python之Django rest_Framework
Django Rest Framework 一.rest api a.api就是接口 如: - http://www.oldboyedu.com/get_user/ ...
- 缓存之ehcache
1.EhCache缓存框架简介 EhCache 是一个纯Java的进程内缓存框架,具有快速.精干等特点,是Hibernate中默认的CacheProvider. 我们使用EhCache缓存框架主要是为 ...
- Laravel 5.4.36 session 没有保存成功问题
session使用注意点 工作中使用的是session默认的文件缓存 在使用过发现 session()->put("key","values") 发 ...
- java线程池原理及实现方式
线程池的定义 线程池是一种多线程处理形式,处理过程中将任务添加到队列,然后在创建线程后自动启动这些任务.线程池线程都是后台线程 为什么要使用线程池 1.减少在创建和销毁线程上所花的时间以及系统资源的开 ...
- CF781D Axel and Marston in Bitland [倍增 矩阵乘法 bitset]
Axel and Marston in Bitland 好开心第一次补$F$题虽然是$Div.2$ 题意: 一个有向图,每条边是$0$或$1$,要求按如下规则构造一个序列然后走: 第一个是$0$,每次 ...
- Codeforces Round #403 (Div. 2, based on Technocup 2017 Finals)
Codeforces Round #403 (Div. 2, based on Technocup 2017 Finals) 说一点东西: 昨天晚上$9:05$开始太不好了,我在学校学校$9:40$放 ...