如题。。。

#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)-(并查集模板题)的更多相关文章

  1. PAT甲级——1118 Birds in Forest (并查集)

    此文章 同步发布在CSDN:https://blog.csdn.net/weixin_44385565/article/details/89819984   1118 Birds in Forest  ...

  2. PAT A 1118. Birds in Forest (25)【并查集】

    并查集合并 #include<iostream> using namespace std; const int MAX = 10010; int father[MAX],root[MAX] ...

  3. [并查集] 1118. Birds in Forest (25)

    1118. Birds in Forest (25) Some scientists took pictures of thousands of birds in a forest. Assume t ...

  4. 1118. Birds in Forest (25)

    Some scientists took pictures of thousands of birds in a forest. Assume that all the birds appear in ...

  5. 【PAT甲级】1118 Birds in Forest (25分)(并查集)

    题意: 输入一个正整数N(<=10000),接着输入N行数字每行包括一个正整数K和K个正整数,表示这K只鸟是同一棵树上的.输出最多可能有几棵树以及一共有多少只鸟.接着输入一个正整数Q,接着输入Q ...

  6. PAT甲题题解-1114. Family Property (25)-(并查集模板题)

    题意:给出每个人的家庭成员信息和自己的房产个数与房产总面积,让你统计出每个家庭的人口数.人均房产个数和人均房产面积.第一行输出家庭个数,随后每行输出家庭成员的最小编号.家庭人口数.人均房产个数.人均房 ...

  7. PAT1118. Birds in Forest (并查集)

    思路:并查集一套带走. AC代码 #include <stdio.h> #include <string.h> #include <algorithm> using ...

  8. 1118 Birds in Forest

    题意: 思路:并查集模板题. 代码: #include <cstdio> #include <algorithm> using namespace std; ; int fat ...

  9. PAT甲级 并查集 相关题_C++题解

    并查集 PAT (Advanced Level) Practice 并查集 相关题 <算法笔记> 重点摘要 1034 Head of a Gang (30) 1107 Social Clu ...

随机推荐

  1. SASS对css的管理

    一.SASS简介 SASS是一种CSS的开发工具,提供了许多便利的写法,大大节省了设计者的时间,使得CSS的开发,变得简单和可维护. 本文总结了SASS的主要用法.我的目标是,有了这篇文章,日常的一般 ...

  2. ES6中变量解构的用途—遍历Map结构

  3. 【剑指offer】数值的整数次方

    版权声明:本文为博主原创文章,未经博主同意不得转载. https://blog.csdn.net/mmc_maodun/article/details/25506085 转载请注明出处:http:// ...

  4. java字符串面试题

    public static void main(String[] args) { String s1 = "abcd"; String s2 = new String(" ...

  5. 8.UDP协议

    传输层协议:TCP UDP TCP和UDP有什么区别? TCP是面向连接的 UDP是面向无连接.在互通之前,面向连接的协议会先建立连接,如TCP会三次握手.所谓的建立连接,是为了在客户端和服务端维护连 ...

  6. Ubuntu安装Chromium浏览器

    今天介绍一下谷歌浏览器在ubuntu 系统环境下的安装步骤,1.在终端的窗口上输入: sudo wget http://www.linuxidc.com/files/repo/google-chrom ...

  7. c语言gets()函数与它的替代者fgets()函数

    在c语言中读取字符串有多种方法,比如scanf() 配合%s使用,但是这种方法只能获取一个单词,即遇到空格等空字符就会返回.如果要读取一行字符串,比如: I love BIT 这种情况,scanf() ...

  8. 20155202张旭《网络对抗技术》 week1 PC平台逆向破解及Bof基础实践

    20155202张旭<网络对抗技术> week1 PC平台逆向破解及Bof基础实践 1.实践目标: 实践对象:一个名为pwn1的linux可执行文件. 该程序正常执行流程是: main调用 ...

  9. 20155204 2016-2017-2 《Java程序设计》第1周学习总结

    20155204 2016-2017-2 <Java程序设计>第1周学习总结 一.学习考核方式,理解成绩构成 首先是100分的构成,主要分为周考的总计60,实验的15分,团队项目(博客报告 ...

  10. EZ 2018 05 06 NOIP2018 慈溪中学集训队互测(五)

    享受爆零的快感 老叶本来是让初三的打的,然后我SB的去凑热闹了 TM的T2写炸了(去你妹的优化),T1连-1的分都忘记判了,T3理所当然的不会 光荣革命啊! T1 思维图论题,CHJ dalao给出了 ...