1118 Birds in Forest (25 分)
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 <bits/stdc++.h>
using namespace std;
int p[];
int visit[];
int a[];
int n,m,k;
int found(int a)
{
if(a==p[a]){
return a;
}
return p[a]=found(p[a]);
}
void unite(int a,int b)
{
int x = found(a);
int y = found(b);
if(x!=y){
p[x] = y;
}
return ;
}
bool istrue(int a,int b){
return found(a) == found(b);
}
int main()
{
int x,y;
set<int> s; for(int i=;i<=;i++){
p[i] = i;
}
scanf("%d",&n);
for(int i=;i<=n;i++){
scanf("%d",&m);
for(int j=;j<=m;j++)
{
scanf("%d",&a[j]);
s.insert(a[j]);
}
for(int j=;j<=m;j++)
{
unite(a[j-],a[j]);
}
}
int sum = s.size();
set<int> ss;
for(int i=;i<=sum;i++)
{
ss.insert(found(i));
}
cout<<ss.size()<<" "<<sum<<endl;
scanf("%d",&k);
while(k--)
{
scanf("%d%d",&x,&y);
if(istrue(x,y)){
cout<<"Yes"<<endl;
}else{
cout<<"No"<<endl;
}
}
return ;
}
1118 Birds in Forest (25 分)的更多相关文章
- 【PAT甲级】1118 Birds in Forest (25分)(并查集)
题意: 输入一个正整数N(<=10000),接着输入N行数字每行包括一个正整数K和K个正整数,表示这K只鸟是同一棵树上的.输出最多可能有几棵树以及一共有多少只鸟.接着输入一个正整数Q,接着输入Q ...
- [并查集] 1118. Birds in Forest (25)
1118. Birds in Forest (25) Some scientists took pictures of thousands of birds in a forest. Assume t ...
- 1118. Birds in Forest (25)
Some scientists took pictures of thousands of birds in a forest. Assume that all the birds appear in ...
- 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 ...
- 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
Some scientists took pictures of thousands of birds in a forest. Assume that all the birds appear in ...
随机推荐
- Dash 使用
花了 160 买了这个软件,至少看一遍它的 user guide,钱不能白花. https://kapeli.com/guide/guide.html 设置全局快捷键 Preference -> ...
- HTML5即将迎来黄金时代 轻应用再成行业焦点
2015-01-23 11:03:09 来源:快鲤鱼 大众能看到的H5效果拜“微信”所赐,几乎每天都有H5页面的推广以及H5小游戏在微信上传播.其实,H5的大热与百度不无关系,2012年开始, ...
- 在苹果iOS平台中获取当前程序进程的进程名等信息
本文由EasyDarwin开源团队成员Penggy供稿: Objective-C 提供 NSProcessInfo 这个类来获取当前 APP 进程信息, 然而我们的静态库是 pure C++ 工程. ...
- 无感知的用同步的代码编写方式达到异步IO的效果和性能,避免了传统异步回调所带来的离散的代码逻辑和陷入多层回调中导致代码无法维护
golang/goroutine 和 swoole/coroutine 协程性能测试对比 - Go语言中文网 - Golang中文社区 https://studygolang.com/articles ...
- 在DuiLib中使用MFC类
比如,想在DuiLib里使用MFC中的CInternetSession 首先,将Project Properties->General->Use of MFC设置为Use MFC in a ...
- HDOJ 4704 Sum 规律 欧拉定理
规律 欧拉定理: 找规律 2^n-1 ,n 非常大用欧拉定理 Sum Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 131072/13 ...
- contentprovider 实例
Provider端 public class PersonProvider extends ContentProvider { //用来存放所有合法的Uri的容器 private static Uri ...
- 2018年东北农业大学春季校赛 D wyh的迷宫 【BFS】
题目链接 https://www.nowcoder.com/acm/contest/93/D 思路 BFS模板题 AC代码 #include <cstdio> #include <c ...
- SSL协议、HTTP和HTTPS和区别
SSL协议 SLL协议的握手过程 开始加密通信之前,客户端和服务器首先必须建立连接和交换参数,这个过程叫做握手(handshake). 第一步,客户端给出协议版本号.一个客户端生成的随机数(Clien ...
- vue中的 v-if VS v-show
相同点:都是动态显示DOM元素. 不同点:1.v-if是动态的向DOM树内添加或者删除DOM元素:v-show是通过设置DOM元素的display样式属性控制显隐: 2.v-if切换有一个局部编译/卸 ...