如题。。。

#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. Audit log report

  2. leaflet学习一 入门

    1从官网https://leafletjs.com/下载的Leaflet包含文件: leaflet.js - 简化版的 Leaflet JavaScript代码 leaflet-src.js - 这是 ...

  3. codeforces 671D Roads in Yusland & hdu 5293 Tree chain problem

    dp dp优化 dfs序 线段树 算是一个套路.可以处理在树上取链的问题.

  4. Beta阶段第一次冲刺

    Beta阶段第一次冲刺 以后严格按照Git标准来,组员有上传Git的才有贡献分没有的为0 代码签入图 1.part1 -站立式会议照片 2.part2 -项目燃尽图 3.part3 -项目进展 1.正 ...

  5. 三、git管理修改

    一.修改提交 如下图,Git分工作区和版本库(.git隐藏目录中). 在每次修改后 git add "file name" 其实是把修改内容提交到本地版本库的 暂存区(stage) ...

  6. Android学习之——自己搭建Http框架(1)

    一.前言         近期学习http框架.         眼下写的这个框架临时仅仅适用于学习之用,实际用于项目之中还须要不断的优化.         要从server或者网络获取数据.显示到U ...

  7. BZOJ4810:[YNOI2017]由乃的玉米田(莫队,bitset)

    Description 由乃在自己的农田边散步,她突然发现田里的一排玉米非常的不美.这排玉米一共有N株,它们的高度参差不齐. 由乃认为玉米田不美,所以她决定出个数据结构题   这个题是这样的: 给你一 ...

  8. WorldWind源码剖析系列:可渲染对象列表类RenderableObjectList

    可渲染对象列表类RenderableObjectList用来存储与一个星球体相关联的全部的可渲染对象,继承自可渲染对象类RenderableObject.RenderableObjectList的类图 ...

  9. 简单的表格json控件

    简单的表格json控件 由于最近做的项目一直有表格的形式展示数据,所以想写个简单的关于表格方面的控件出来,想用JSON数据直接渲染出来,因为开发给到我们前端的字段可能会叫不同的名字,所以我们前端渲染页 ...

  10. 404 Note Found队 Beta答辩总结

    目录 所有成员 项目宣传视频链接 贡献比例 工作流程 组员分工 本组 Beta 冲刺站立会议博客链接汇总 燃尽图 原计划.达成情况及原因分析 组员:胡绪佩 组员:周政演 组员:庄卉 组员:何家伟 组员 ...