题意:

输入一个正整数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. codewars--js--Hamming Numbers

    问题描述: A Hamming number is a positive integer of the form 2i3j5k, for some non-negative integers i, j ...

  2. 一篇文章带你搞定 ElasticSearch 术语

    这篇文章主要介绍 ElasticSearch 的基本概念,学习文档.索引.集群.节点.分片等概念,同时会将 ElasticSearch 和关系型数据库做简单的类比,还会简单介绍 REST API 的使 ...

  3. 面型对象和UML类图

    面向对象 why? 1.程序执行:顺序,判断,循环,----结构化 2.面向对象----数据结构化 3.面向计算机,结构化的才是最简单的 4.变成应该 简单&抽象 一个基本的类 class P ...

  4. 【React Native】某个页面禁用物理返回键

    1.引入组件 import { BackHandler, } from 'react-native'; 2.添加监听 componentDidMount(): void { BackHandler.a ...

  5. Linux 简介、目录结构

    Linux是类 Unix 操作系统. 根据原生程度可分为: 内核版本 发行版本:一些公司.组织在内核版的基础上进行二次开发 根据市场需求可分为: 服务器版:没有好看的界面,在终端操作,类似于dos 桌 ...

  6. 轮播组件/瀑布流/组合搜索/KindEditor插件

    一.企业官网 ### 瀑布流 ​ Models.Student.objects.all() #获取所有学员信息 ​ 通过div进行循环图片和字幕 ​ 1.以template模板方法实现瀑布流以列为单位 ...

  7. Kubernetes CI/CD(2)

    本章节通过在Jenkins创建一个kubernetes云环境,动态的在kubernetes集群中创建pod完成pipeline的构建流程,关于直接在宿主机上搭建Jenkins集群的可参照Kuberne ...

  8. npm常用命令和脚手架使用

    最近前端同学使用最多的莫过于vue,angualr,react等热门前端框架了.那么就避免不掉的使用npm命令,本人就经常因为这三个脚手架的使用而不得不百度相应的npm命令,不胜其烦,,,因此就整理一 ...

  9. C#画图之饼图

    public JsonResult DrawPie() { // 预置颜色 List<Color> colors = new List<Color>() { Color.Fro ...

  10. python变量加逗号,的含义

    逗号,用于生成一个长度为1的元组 >>> (1) 1 >>> (1,) (1,) >>> 1, (1,) 因此需要将长度为1的元组中元素提取出来可 ...