题意:

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

AAAAAccepted code:

 #define HAVE_STRUCT_TIMESPEC
#include<bits/stdc++.h>
using namespace std;
int fa[];
int vis[];
int find_(int x){
if(x==fa[x])
return x;
return fa[x]=find_(fa[x]);
}
int main(){
ios::sync_with_stdio(false);
cin.tie(NULL);
cout.tie(NULL);
int n;
cin>>n;
for(int i=;i<=;++i)
fa[i]=i;
for(int i=;i<=n;++i){
int k;
cin>>k;
int temp=;
for(int j=;j<=k;++j){
int x;
cin>>x;
vis[x]=;
int f=find_(x);
if(j==)
temp=f;
fa[f]=temp;
}
}
int cnt=;
int sum=;
for(int i=;i<=;++i)
if(fa[i]==i&&vis[i])
++cnt;
else if(vis[i])
++sum;
cout<<cnt<<" "<<cnt+sum<<"\n";
int q;
cin>>q;
while(q--){
int a,b;
cin>>a>>b;
if(find_(a)==find_(b))
cout<<"Yes\n";
else
cout<<"No\n";
}
return ;
}

【PAT甲级】1118 Birds in Forest (25分)(并查集)的更多相关文章

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

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

  2. PAT题解-1118. Birds in Forest (25)-(并查集模板题)

    如题... #include <iostream> #include <cstdio> #include <algorithm> #include <stri ...

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

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

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

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

  5. PAT 甲级 1040 Longest Symmetric String (25 分)(字符串最长对称字串,遍历)

    1040 Longest Symmetric String (25 分)   Given a string, you are supposed to output the length of the ...

  6. PAT 甲级 1083 List Grades (25 分)

    1083 List Grades (25 分) Given a list of N student records with name, ID and grade. You are supposed ...

  7. PAT甲级——1130 Infix Expression (25 分)

    1130 Infix Expression (25 分)(找规律.中序遍历) 我是先在CSDN上面发表的这篇文章https://blog.csdn.net/weixin_44385565/articl ...

  8. PAT 甲级 1074 Reversing Linked List (25 分)(链表部分逆置,结合使用双端队列和栈,其实使用vector更简单呐)

    1074 Reversing Linked List (25 分)   Given a constant K and a singly linked list L, you are supposed ...

  9. PAT 甲级 1086 Tree Traversals Again (25分)(先序中序链表建树,求后序)***重点复习

    1086 Tree Traversals Again (25分)   An inorder binary tree traversal can be implemented in a non-recu ...

随机推荐

  1. [MongoDB] 使用PHP根据_id字段查询数据

    mongo中的_id是一个objectid对象类型,不管是查询时作为条件,还是列表时展示内容,都需要进行一下抓换 查询时要转为objectid对象 列表时要把对象转成字符串覆盖回_id字段 $filt ...

  2. vue循环语句

    循环使用 v-for 指令. v-for 指令需要以 site in sites 形式的特殊语法, sites 是源数据数组并且 site 是数组元素迭代的别名. v-for 可以绑定数据到数组来渲染 ...

  3. mysql 使用 limit ,从指定条数读取完,-1失效

    , 原因很简单这种写法本身就是错的,虽然它可以在之前的版本中运行(低优先级),新版本的mysql对此做出了修复,现在的替代方法是第二个参数用一个较大的正数代替 比如你写个 , 感觉这方法很蠢,然鹅我并 ...

  4. leetcode面试题 02.06. 回文链表,解题心路

    目录 leetcode面试题 02.06. 回文链表,解题心路 1.题目描述 2.java语言题解一 3.java语言题解二 4.C语言题解一 leetcode面试题 02.06. 回文链表,解题心路 ...

  5. 测试.NET core MiddleWare 运行逻辑

    话不多说,直接开整. 首先创建一个.NET CORE web 工程,创建完成之后,会自动创建相关文件如图(本示例基于.NET CORE 3.0): 打开Startup.cs可以看到在Configure ...

  6. 3.Python运算符详解

    1.算数运算符 符号:+   -   *   /   %(取余.取模)   //(取整)   **(开方) 2.比较运算符 符号:>   >=   <   <=   ==(全等 ...

  7. 高精度模板 支持各种运算 c++

    绪言 自从有了高精度模板,妈妈再也不用怕我不会打高精度了! 代码 代码长度与日俱增啊~~~ #include<iostream> #include<cstring> #incl ...

  8. HTML速查

    HTML 基本文档 <!DOCTYPE html> <html> <head> <title>文档标题</title> </head& ...

  9. 快速建立一个Django项目

    快速建立一个Django项目 版本说明 一定要先明确好使用的Python版本和所使用包的版本,避免耽误不要的时间 Python==3.6.4 Django==1.11.9 djangoresframe ...

  10. 《Adaptive Density Map Generation for Crowd Counting》密集人群检测论文笔记

    背景 密度图\(D_g\)的生成对于最终网络预测结果\(D_e\)至关重要,但是密度图\(D_g\)生成的过程中,高斯核的大小常常是手动设定的,并且对于不同的数据集,核大小和形状通常不一样.这些手动选 ...