A1118. Birds in Forest
Some scientists took pictures of thousands of birds in a forest. Assume that all the birds appear in the same picture belong to the same tree. You are supposed to help the scientists to count the maximum number of trees in the forest, and for any pair of birds, tell if they are on the same tree.
Input Specification:
Each input file contains one test case. For each case, the first line contains a positive number N (<= 104) which is the number of pictures. Then N lines follow, each describes a picture in the format:
K B1 B2 ... BK
where K is the number of birds in this picture, and Bi's are the indices of birds. It is guaranteed that the birds in all the pictures are numbered continuously from 1 to some number that is no more than 104.
After the pictures there is a positive number Q (<= 104) which is the number of queries. Then Q lines follow, each contains the indices of two birds.
Output Specification:
For each test case, first output in a line the maximum possible number of trees and the number of birds. Then for each query, print in a line "Yes" if the two birds belong to the same tree, or "No" if not.
Sample Input:
4
3 10 1 2
2 3 4
4 1 5 7 8
3 9 6 4
2
10 5
3 7
Sample Output:
2 10
Yes
No
#include<cstdio>
#include<iostream>
#include<algorithm>
using namespace std;
int father[];
int findRoot(int x){
int temp = x;
while(x != father[x]){
x = father[x];
}
int temp2;
while(temp != x){
temp2 = father[temp];
father[temp] = x;
temp = temp2;
}
return x;
}
void union_(int a, int b){
int fa = findRoot(a);
int fb = findRoot(b);
if(fa == fb)
return;
father[fb] = fa;
}
int main(){
int N, K, cnt = -;
for(int i = ; i < ; i++){
father[i] = i;
}
scanf("%d", &N);
for(int i = ; i < N; i++){
int b1, b2, maxb;
scanf("%d%d", &K, &b1);
cnt = max(b1, cnt);
for(int j = ; j < K; j++){
scanf("%d", &b2);
union_(b1, b2);
maxb = max(b1, b2);
cnt = max(maxb, cnt);
}
}
int tree = ;
for(int i = ; i <= cnt; i++){
if(father[i] == i)
tree++;
}
printf("%d %d\n", tree, cnt);
int Q;
scanf("%d", &Q);
for(int i = ; i < Q; i++){
int q1, q2;
scanf("%d%d", &q1, &q2);
if(findRoot(q1) == findRoot(q2))
printf("Yes\n");
else printf("No\n");
}
cin >> N;
return ;
}
1、因为birds并不是连续出现的,需要用set来存储bird的编号。
A1118. Birds in Forest的更多相关文章
- PAT A1118 Birds in Forest (25 分)——并查集
Some scientists took pictures of thousands of birds in a forest. Assume that all the birds appear in ...
- PAT甲级——A1118 Birds in Forest【25】
Some scientists took pictures of thousands of birds in a forest. Assume that all the birds appear in ...
- PAT_A1118#Birds in Forest
Source: PAT A1118 Birds in Forest (25 分) Description: Some scientists took pictures of thousands of ...
- 1118 Birds in Forest (25 分)
1118 Birds in Forest (25 分) Some scientists took pictures of thousands of birds in a forest. Assume ...
- PAT1118:Birds in Forest
1118. Birds in Forest (25) 时间限制 150 ms 内存限制 65536 kB 代码长度限制 16000 B 判题程序 Standard 作者 CHEN, Yue Some ...
- [并查集] 1118. Birds in Forest (25)
1118. Birds in Forest (25) Some scientists took pictures of thousands of birds in a forest. Assume t ...
- PAT 1118 Birds in Forest [一般]
1118 Birds in Forest (25 分) Some scientists took pictures of thousands of birds in a forest. Assume ...
- PAT甲级——1118 Birds in Forest (并查集)
此文章 同步发布在CSDN:https://blog.csdn.net/weixin_44385565/article/details/89819984 1118 Birds in Forest ...
- 1118 Birds in Forest (25 分)
Some scientists took pictures of thousands of birds in a forest. Assume that all the birds appear in ...
随机推荐
- 使用withCount后再使用select设置查询的字段。就找不到withCount的数据了
https://laravelacademy.org/index.php/discussion/1021 如:Article::withCount(['comments'])->select(' ...
- Python __slots__ 作用
参考:https://blog.csdn.net/u010733398/article/details/52803643 https://blog.csdn.net/sxingming/artic ...
- Python图形用户界面
1.使用Tkinter创建图形用户界面的步骤 (1)导入Tkinter模块,使用import Tkinter或from Tkinter import * (2)创建顶层窗口对象容器,使用top = T ...
- 使用PHP对二维索引数组进行排序
本例中 data 数组中的每个单元表示一个表中的一行.这是典型的数据库记录的数据集合. 例子中的数据如下: volume | edition -------+-------- 67 | 2 86 | ...
- Yii2的使用
yii2的下载安装 使用下载好的文件配置高级模板,在配置文件配置好数据库和gii 在common模板配置db: 在backend模板配置gii: 配置nginx服务器访问backend和fronten ...
- 阿里云ECS服务器,CentOS 7.4配置jdk+tomcat+mysql
参考博客: https://mp.weixin.qq.com/s?__biz=MzIxMzk3Mjg5MQ==&mid=2247484020&idx=1&sn=6e0aa07f ...
- LODOP打印控件进行批量打印
Lodop打印控件批量打印的方式:1.批量打印每页内容相同的:(1)批量打印相同内容的很多纸张,可以设置打印份数,把该内容打印出多份.2.批量打印每页不同内容的:(1)通过在一个任务中分页,循环添加页 ...
- cookie中的小错误
今天在练习 cookie时意外的报了这个错. 这句话的意思是一个不识别的字符[32]出现在了cookie当中由于tomcat的版本比较高,所以在addCookie时是不能使用空格的 而在ASCII码中 ...
- Js 中一系列宽度和高度的学习
在学习元素一系列宽度和高度之前,我们先来看一个平时开发中几乎不会遇到的问题,那就是html文档声明<!DOCTYPE html> 确实会对元素的宽高产生影响.几乎不会遇到,是因为我们在写h ...
- 删除本地git的远程分支和远程删除git服务器的分支【转】
转- 删除本地git的远程分支和远程删除git服务器的分支 在项目中使用git管理代码后,有些时候会创建很多不同名称的分支,以此区分各个分支代码功能. 而随着代码的合并,以前的分支就可能不再需要保存了 ...