PAT题解-1118. Birds in Forest (25)-(并查集模板题)
如题。。。
#include <iostream>
#include <cstdio>
#include <algorithm>
#include <string.h>
using namespace std;
/*
并查集水题
*/
const int maxn=+; struct UF{
int father[maxn];
void init(){
for(int i=;i<maxn;i++)
father[i]=i;
}
int find_root(int x){
if(father[x]!=x){
father[x]=find_root(father[x]);
}
return father[x];
}
void Union(int x,int y){
int fx=find_root(x);
int fy=find_root(y);
if(fx!=fy){
father[fy]=fx;
}
}
}uf;
int main()
{
int n,q;
int k,a,b;
int vis[maxn];
uf.init();
memset(vis,,sizeof(vis));
scanf("%d",&n);
for(int i=;i<n;i++){
scanf("%d %d",&k,&a);
vis[a]=;
for(int j=;j<k;j++){
scanf("%d",&b);
vis[b]=;
uf.Union(a,b);
a=b;
}
}
int flag[maxn];
memset(flag,,sizeof(flag));
int cnt=;
for(int i=;i<maxn;i++){
if(vis[i]){
cnt++;
int fa=uf.find_root(i);
flag[fa]=;
}
}
int tree=;
for(int i=;i<maxn;i++){
tree+=flag[i];
}
printf("%d %d\n",tree,cnt);
scanf("%d",&q);
for(int i=;i<q;i++){
scanf("%d %d",&a,&b);
int fa=uf.find_root(a);
int fb=uf.find_root(b);
if(fa==fb)
printf("Yes\n");
else
printf("No\n");
}
return ;
}
PAT题解-1118. Birds in Forest (25)-(并查集模板题)的更多相关文章
- 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] ...
- [并查集] 1118. Birds in Forest (25)
1118. Birds in Forest (25) Some scientists took pictures of thousands of birds in a forest. Assume t ...
- 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甲题题解-1114. Family Property (25)-(并查集模板题)
题意:给出每个人的家庭成员信息和自己的房产个数与房产总面积,让你统计出每个家庭的人口数.人均房产个数和人均房产面积.第一行输出家庭个数,随后每行输出家庭成员的最小编号.家庭人口数.人均房产个数.人均房 ...
- PAT1118. Birds in Forest (并查集)
思路:并查集一套带走. AC代码 #include <stdio.h> #include <string.h> #include <algorithm> using ...
- 1118 Birds in Forest
题意: 思路:并查集模板题. 代码: #include <cstdio> #include <algorithm> using namespace std; ; int fat ...
- PAT甲级 并查集 相关题_C++题解
并查集 PAT (Advanced Level) Practice 并查集 相关题 <算法笔记> 重点摘要 1034 Head of a Gang (30) 1107 Social Clu ...
随机推荐
- 2.Redis集群环境搭建
转载请出自出处:http://www.cnblogs.com/hd3013779515/ 一.基本概念 1.redis集群是一个可以在多个节点之间进行数据共享的设施.redis集群提供了以下两个好处1 ...
- vue2.0学习笔记之组件
[易错点]: 1. 组件内html代码片段超过一个标签时必须套一个根元素,即template模板子元素只能是一个. 2. 组件名称采用横杠间隔命名时,第一个字母大写会报错. 写法一:全局 ...
- Redis系列六:redis相关功能
一. 慢查询原因分析 与mysql一样:当执行时间超过阀值,会将发生时间耗时的命令记录 redis命令生命周期:发送 排队 执行 返回慢查询只统计第3个执行步骤的时间 预设阀值:两种方式,默认为10毫 ...
- opengl渲染管线梳理
opengl渲染管线梳理 http://www.cnblogs.com/zhanglitong/p/3238989.html 坐标系变换和矩阵 http://www.cppblog.com/guoji ...
- Kubernetes1.91(K8s)安装部署过程(三)--创建高可用etcd集群
这里的etcd集群复用我们测试的3个节点,3个node都要安装并启动,注意修改配置文件 1.TLS认证文件分发:etcd集群认证用,除了本机有,分发到其他node节点 scp ca.pem kuber ...
- Python2.7-weakref
weakref 模块,允许创建对象的弱引用,被弱引用的对象其引用计数不变,对象的引用计数为0时就会被垃圾清理机制释放内存空间,此时对其的弱引用也会失效.在对象会被交叉引用,需要释放内存空间时常用. 模 ...
- POJ 3660 Cow Contest(Floyd求传递闭包(可达矩阵))
Cow Contest Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 16341 Accepted: 9146 Desc ...
- neo4j----创建索引
创建索引 create index on:Student(name) 删除索引 drop index on:Student(name) 创建唯一索引 create constraint on (s:T ...
- python在输出一段话中插入多个变量,每日作业补充
%s用来插入用户输入的值name=input('请输入您的姓名:')age=input('请输入您的年龄:')sex=input('你的性别:')print('-------您好!%s------\n ...
- pip安装python包出错:Could not find a version that satisfies the requirement skimage (from versions: )
今天用pip安装skimage时报错: 这是因为网络的问题,需要使用国内的镜像源来加速,比如豆瓣源 命令改为: pip install scikit-image -i http://pypi.doub ...