【PAT甲级】1118 Birds in Forest (25分)(并查集)
题意:
输入一个正整数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分)(并查集)的更多相关文章
- PAT A 1118. Birds in Forest (25)【并查集】
并查集合并 #include<iostream> using namespace std; const int MAX = 10010; int father[MAX],root[MAX] ...
- PAT题解-1118. Birds in Forest (25)-(并查集模板题)
如题... #include <iostream> #include <cstdio> #include <algorithm> #include <stri ...
- PAT甲级——1118 Birds in Forest (并查集)
此文章 同步发布在CSDN:https://blog.csdn.net/weixin_44385565/article/details/89819984 1118 Birds in Forest ...
- [并查集] 1118. Birds in Forest (25)
1118. Birds in Forest (25) Some scientists took pictures of thousands of birds in a forest. Assume t ...
- PAT 甲级 1040 Longest Symmetric String (25 分)(字符串最长对称字串,遍历)
1040 Longest Symmetric String (25 分) Given a string, you are supposed to output the length of the ...
- PAT 甲级 1083 List Grades (25 分)
1083 List Grades (25 分) Given a list of N student records with name, ID and grade. You are supposed ...
- PAT甲级——1130 Infix Expression (25 分)
1130 Infix Expression (25 分)(找规律.中序遍历) 我是先在CSDN上面发表的这篇文章https://blog.csdn.net/weixin_44385565/articl ...
- 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 ...
- PAT 甲级 1086 Tree Traversals Again (25分)(先序中序链表建树,求后序)***重点复习
1086 Tree Traversals Again (25分) An inorder binary tree traversal can be implemented in a non-recu ...
随机推荐
- springboot之swagger快速启动(新的ui)
springboot之swagger快速启动(新的ui) 功能点: 集成swagger前端接口文档 Swagger 整合 zuul 智能列表 无缝集成 knife4j 前端文档 支持 v0.1.2RE ...
- 小程序封装request请求
//request.js var host = 'https://www.xxx.com';//请求域名 module.exports = function (type, params, method ...
- 字段类型(uniqueidentifier)问题
环境:SQL 2016: 语句 select * from A where PID=JoID 上述查询语句中的Where PID=JoID条件中PID的字段类型为varchar(50)而JoID的字段 ...
- Angular文件基本结构
main.ts(应用程序主入口) → app.module.ts app.module.ts //这个根模块会告诉Angular如何组装该应用 //引入模块 import { BrowserModul ...
- php压缩文件zip格式并打包(单个或多个文件压缩)
最近接到一个需求,就是选择多个文件下载时,不要一个一个下载,直接把多个文件打包成一个文件进行下载.我们项目是前后端分离,所以我写了个接口,让前端传参数,后台下载. 废话不多说,直接上代码: 先是压缩单 ...
- 本地服务器热更新 插件 live-server
本地服务器热更新 插件 live-server 超级好用 强烈种草一波 无需安装到项目中 使用方法如下: 1.先全局安装live-server: npm i http-server -g 2.在需要热 ...
- 【spring boot】SpringBoot初学(8)– 简单整合redis
前言 到目前为止,把项目中需要用到的:properties读取.数据源配置.整合mybatis/JdbcTemplate.AOP.WebService.redis.filter.interceptor ...
- Docker Compose搭建ELK
Elasticsearch默认使用mmapfs目录来存储索引.操作系统默认的mmap计数太低可能导致内存不足,我们可以使用下面这条命令来增加内存: sysctl -w vm.max_map_count ...
- 如何开启音乐二倍速?不下载其他软件【win10】
使用windows自带的windows media player开启N倍速 绪言 额……暑假将终,我想起了件事:貌似我忘记帮你们开好二倍速再走了. 可能我回(六班)来的机会也比较少,废话不多说,直接看 ...
- 一个margin就可以让块状元素响应居中,很实用
之前总结过水平居中的很多方法,但今天在<css世界>这本书里看到margin的一个特性,一行代码就搞定很实用,分享一下 margin: auto能在块级元素设定宽高之后自动填充剩余宽高.m ...