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 ...
随机推荐
- C# Note8: 设计模式全解
前言——资源说明 目前网上设计模式的介绍可谓非常之多(各种编程语言的版本),其中不乏精细之作,本文的目的在于搜集和整理C#或C++的设计模式,毕竟思想还是共通的! 设计模式的分类 创建型模式,共五种: ...
- dom 事件主要内容
一 . onclick(单击) 原图 单击btn1 在点击btn2 二 . onfocus 和 onblur onfocus(聚焦, 鼠标点击输入框) onblur(模糊, 鼠标点击输入框外的地方) ...
- linux上如何让other用户访问没有other权限的目录
目前遇到一个问题,一个other用户要访问一个目录,他需要在这个目录下创建文件,因此这个目录需要一个写权限,于是就给了这个目录777的权限,这样这个权限有点太大了,很容易出现安全问题,那我们应该怎么办 ...
- WPF中自定义MarkupExtension
在介绍这一篇文章之前,我们首先来回顾一下WPF中的一些基础的概念,首先当然是XAML了,XAML全称是Extensible Application Markup Language (可扩展应用程序标记 ...
- PHP的爬虫框架
Beanbun PHPSpider PHPQuery QueryList PHPCrawer Snoopy
- JMeter 连接 sql server
1.安装驱动 http://www.microsoft.com/zh-CN/download/details.aspx?id=11774 下载后解压后复制sqljdbc.jar到 “jmeter的安装 ...
- delphi中adoquery控件中某个字段Onvalidate事件的用法?
procedure TForm2.ADOQuery1TestFieldValidate(Sender: TField);begin// Sender就是当前字段,可以在这里对Sender字段进行各种操 ...
- Vue之computed计算属性
demo.html <!DOCTYPE html> <html lang="en" xmlns:v-bind="http://www.w3.org/19 ...
- 使用cmd命令行窗口操作SqlServer
本文主要介绍使用windows下的使用cmd命令行窗口操作Sqlserver, 首先我们可以运行 osql ?/ ,这样就把所有可以通过CMD命令行操作sqlserver的命令显示出来 (有图有 ...
- 前端传递给后端且通过cookie方式,尽量传递id
前端传递给后端且通过cookie方式,尽量传递id