如题。。。

#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. 内网arp攻击

    内网arp攻击 环境:一台kali虚拟机(攻击者),一台win7虚拟机(用户) 网络:NAT模式 网段:192.168.41.0/24 网关:192.168.41.2/24 win7的IP地址:192 ...

  2. Office Web app server 2013 目前无法和windows server 2012 R2兼容。

    另外旧版的office文档和PDF格式不支持预览功能.

  3. CGJ02、BD09、西安80、北京54、CGCS2000常用坐标系详解

    一.万能地图下载器中的常用坐标系 水经注万能地图下载器中的常用的坐标系主要包括WGS84经纬度投影.WGS84 Web 墨卡托投影.WGS84 UTM 投影.北京54高斯投影.西安80高斯投影.CGC ...

  4. Actor模式初步入门

    Actor模型概念 Actor模型为并行而生,简单说是未解决高并发的一种编程思路.在Actor模型中,主角是Actor,类似一种worker,Actor彼此之间直接发送消息,不需要经过什么中介,消息是 ...

  5. 异常处理(try...catch...final 和 throw , throws)

    1.传统(弱语言)处理异常方式 原理:利用判断来控制异常出现 publicclass Test01 { publicstaticvoid main(String[] args) { Scanner s ...

  6. JS动态生成表格后 合并单元格

    JS动态生成表格后 合并单元格 最近做项目碰到表格中的单元格合并的问题,需求是这样的,首先发ajax请求 请求回来后的数据 动态生成表格数据,但是生成后如果编号或者(根据其他的内容)有相同时,要合并单 ...

  7. JAVA框架:hibernate(二)

    一.事务操作. 代码: @Test public void tr_Up(){ Session session=hibernateUtils.getSession(); Transaction tran ...

  8. (转)win7批量创建用户

    @echo off::zhangsan 改为你需要建立的用户名,123456改为你的密码,后面的两个参数的意思是“用户::不可以更改密码”和“密码永不过期”net user xiaowei 12345 ...

  9. 创世纪 BZOJ3037 & [Poi2004]SZP BZOJ2068

    分析: 树形DP中的一种,基环树DP 针对每一个环跑DP,f[i],g[i]分别表示选或者不选,之后我们注意每次遍历的时候,不要重复遍历. 附上代码: #include <cstdio> ...

  10. VB6 CHECK is run as admin privilege

    vb6 code: Private Declare Function IsUserAnAdmin Lib "Shell32" Alias "#680" () A ...