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 ...
随机推荐
- 顺序容器vector,deque,list的选用规则
前言 常见的顺序容器主要有三种 - vector,deque,list.它们实现的功能相差不大,那么实际开发中该如何进行选择呢?本文将为你解答这个问题. 分析 由于这三种容器实现的数据结构原型不同(v ...
- C#用Infragistics 导入导出Excel
最近项目中有数据的导入导出Excel的需求,这里做简单整理. 公司用的是Infragistics的产品,付费,不需要本地安装Office. 有需要的朋友可以下载 Infragistics.2013.2 ...
- 基于live555实现的跨平台高性能RTSPServer流媒体服务器EasyIPCamera
本文转自EasyDarwin团队成员kim的博客:http://blog.csdn.net/jinlong0603/article/details/52366412 简介 EasyIPCamera是由 ...
- EasyDarwin流媒体服务器实现关键帧推送功能
-本篇由团队成员Fantasy供稿! 功能背景 随着社会进步,人们对产品体验要求越来越高.EasyDarwin也不例外.为了能满足用户对链接服 后看到画面时间(也就是我们经常看到的起播时间)短的要求, ...
- Winform拖拽改变无边框窗体大小
大家在进行Winform开发过程中,很容易就可以完成一个窗口的布局工作,但现在的软件界面美化效果一个比一个好,很多软件都是无边框的,于是乎,你是不是也感叹:winform的带边框的窗体如此丑陋,我一定 ...
- Quick UDP Internet Connections
https://blog.chromium.org/2013/06/experimenting-with-quic.html user datagram protocol transport laye ...
- 2.alert() 函数
①alert() 函数在 JavaScript 中并不常用,但它对于代码测试非常方便. <!DOCTYPE html><html><body> <h1> ...
- 在iOS平台使用ffmpeg解码h264视频流(转)
在iOS平台使用ffmpeg解码h264视频流,有需要的朋友可以参考下. 对于视频文件和rtsp之类的主流视频传输协议,ffmpeg提供avformat_open_input接口,直接将文件路径或UR ...
- Alsa中PCM参数设置⭐⭐
1) PCM设备的句柄.2) 指定同时可供回放或截获的PCM流的方向3) 提供一些关于我们想要使用的设置选项的信息,比如缓冲区大小,采样率,PCM数据格式等4) 检查硬件是否支持设置选项. 4.1 ...
- 解决Android Studio Fetching Android SDK component information失败问题【转】
本文转载自:http://blog.csdn.net/isesar/article/details/41908089 Android Studio 安装完成后,如果直接启动,Android Studi ...