题意:

输入一个正整数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. Cesium案例解析(三)——Camera相机

    目录 1. 概述 2. 实例 2.1. Camera.html 2.2. Camera.js 2.2.1. 飞行至某一点 2.2.2. 飞行至某区域 2.2.3. 两地之间飞行 2.2.4. 设置视图 ...

  2. MySQL保存微信昵称中的特殊符号造成:(Incorrect string value: "xxxx'for column ‘name’ at row 1)异常

    今天有业务员反应,编辑某个用户的信息的时候出现了异常,异常信息如下: Incorrect string value: "xFOx9Fx92x9D vxE6..'f or column 'na ...

  3. 原创:mysql5 还原至mysql 8.0.11数据库链接配置提示错误(修改内容有三处

    原创:mysql5 还原至mysql 8.0.11数据库链接配置提示错误改有三: a) mysql 连接jar包版修改 b)类路径修改 c)配置连接池地址修改 因版本升级,首先要修改 1:mysql- ...

  4. C#中 ref 关键字的认识和理解

    之前接手老项目的时候有遇到一些的方法参数中使用了ref关键字加在传参的参数前面的情况.对于新手,这里介绍和讲解一下ref的用法和实际效果. CLR中默认所有方法的参数传递方式都是传值,也就是说不管你传 ...

  5. Treap总结

    \(Treap = Tree + Heap\) 树堆(Treap),在数据结构中也称Treap,是指有一个随机附加域满足堆的性质的二叉搜索树,其结构相当于以随机数据插入的二叉搜索树.其基本操作的期望时 ...

  6. 【Java】Swing实现一个简单的计算器

    import javax.swing.*; import java.awt.*; /** * 计算器 * @author paul * 2019.11.25 21:43 * */ public cla ...

  7. 使用elementui树形控件写项目小结

    使用tree pagination serch table 实现功能 项目难点主要解析后台传递的代码,线性转树形,这儿加上一个大神的解析 https://blog.csdn.net/dandanzmc ...

  8. css3基础-文本与字体+转换+过渡+动画+案例

    Css3文本与字体   文本阴影 h1 { text-shadow: 5px 5px 5px red; } word-break换行: h1:nth-child(1) { word-break: no ...

  9. Oracle12c传统数据库模式 OGG

    OGG12C 配置 环境配置: 安装数据库Oracle12c 安装源端OGG:oggs  PORT:7809 安装目标端OGG:oggt   PORT:7909 源端和目标端地址:127.0.0.1 ...

  10. shiro权限认证Realm的四大用法

    一.SimpleAccountRealm public class AuthenticationTest {          SimpleAccountRealm sar=new SimpleAcc ...