PAT 1118 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 (≤10^4 ) 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 (≤10^4 ) 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<iostream> //并查集
#include<math.h>
#include<vector>
using namespace std;
vector<int> birds(10010, 0), check(10010, 0);
int findfather(int b){
int t=b;
while(birds[b]!=b)
b=birds[b];
while(birds[t]!=b){
int temp=t;
t=birds[t];
birds[temp]=b;
}
return b;
}
void Union(int a, int b){
int m= findfather(a);
int n= findfather(b);
if(m!=n) birds[m]=n;
}
int main(){
int n, k, b, num=0, cnt=0, qn;
cin>>n;
vector<int> picture(n, 0);
for(int i=0; i<10010; i++)
birds[i]=i;
for(int i=0; i<n; i++){
cin>>k;
for(int j=0; j<k; j++){
cin>>b;
num=max(num, b);
if(picture[i]==0)
picture[i]=b;
Union(b, picture[i]);
}
}
for(int i=1; i<=num; i++){
b=findfather(i);
if(check[b]==0){
cnt++;
check[b]=1;
}
}
cout<<cnt<<" "<<num<<endl;
cin>>qn;
for(int i=0; i<qn; i++){
int a, b;
cin>>a>>b;
if(findfather(a)==findfather(b))
cout<<"Yes"<<endl;
else
cout<<"No"<<endl;
}
return 0;
}
PAT 1118 Birds in Forest的更多相关文章
- 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 分)
1118 Birds in Forest (25 分) Some scientists took pictures of thousands of birds in a forest. Assume ...
- [并查集] 1118. Birds in Forest (25)
1118. Birds in Forest (25) Some scientists took pictures of thousands of birds in a forest. Assume t ...
- 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 (25分)(并查集)
题意: 输入一个正整数N(<=10000),接着输入N行数字每行包括一个正整数K和K个正整数,表示这K只鸟是同一棵树上的.输出最多可能有几棵树以及一共有多少只鸟.接着输入一个正整数Q,接着输入Q ...
- PAT A1118 Birds in Forest (25 分)——并查集
Some scientists took pictures of thousands of birds in a forest. Assume that all the birds appear in ...
- 1118 Birds in Forest (25 分)
Some scientists took pictures of thousands of birds in a forest. Assume that all the birds appear in ...
随机推荐
- hbase查询_Phoenix及hbase repl命令行两种方式
一.Phoenix(jdbc)登陆 1.cd /home/mr/phoenix/bin(此路径每个环境里面有可能不一样)2../sqlline.py localhost 二.shell repl Hb ...
- android电池管理系统
原文:http://www.2cto.com/kf/201408/326462.html 1.概述 随着移动智能设备的快速发屏,电池的续航能力在很大情况下诱导了大众消费者的购买选择,android系统 ...
- 8.4 IP地址的划分及子网划分
都是比较灵活的一些计算题.只要掌握了其中的规则,还是比较容易解题的.在了解子网的划分如何进行之前呢,一定要弄清楚一个概念:子网掩码.这是弄清楚如何进行子网划分的一个关键. IP地址是四段二进制码拼合而 ...
- Sort List 典型链表
https://leetcode.com/problems/sort-list/ Sort a linked list in O(n log n) time using constant space ...
- E20170630-ts
displacement n. 取代,替代; 免职,停职; [船] 排水量; [化] 置换;
- B. Trees in a Row(cf)
B. Trees in a Row time limit per test 1 second memory limit per test 256 megabytes input standard in ...
- Kafka详解与总结(五)
Kafka持久化 1. 概述 Kafka大量依赖文件系统去存储和缓存消息.对于硬盘有个传统的观念是硬盘总是很慢,这使很多人怀疑基于文件系统的架构能否提供优异的性能.实际上硬盘的快慢完全取决于使用它的方 ...
- Django 内容回顾
模板 变量 {{ }} 标签 {% %} if elif else for empty forloop() with...as csrf_token 过滤器 default length add da ...
- 【洛谷2257/BZOJ2820】YY的GCD(数论/莫比乌斯函数)
题目: 洛谷2257 预备知识:莫比乌斯定理(懵逼乌斯定理) \(\mu*1=\epsilon\)(证bu明hui略zheng) 其中(我校学长把\(\epsilon(x)\)叫单位函数但是为什么我没 ...
- ACM_“IP地址”普及(进制转换)
“IP地址”普及 Time Limit: 2000/1000ms (Java/Others) Problem Description: 大家都知道最近广财大校园网提速,现在就跟大家普及一下简单的互联网 ...