PAT (Advanced Level) Practice 1118 Birds in Forest (25 分) 凌宸1642

2021/8/14 23:10:23

本文主要是介绍PAT (Advanced Level) Practice 1118 Birds in Forest (25 分) 凌宸1642,对大家解决编程问题具有一定的参考价值,需要的程序猿们随着小编来一起学习吧!

PAT (Advanced Level) Practice 1118 Birds in Forest (25 分) 凌宸1642

题目描述:

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:

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.

译:每个输入文件包含一个测试用例。 对于每种情况,第一行包含一个正数 N(≤104),它是图片的数量。 然后是 N 行,每行描述一张图片的格式:

K B1 B2 ... BK

其中 K 是这张图片中鸟类的数量,Bi 是鸟类的索引。 保证所有图片中的鸟类从1到不超过104的某个数字连续编号。

图片后有一个正数Q(≤104),即查询次数。 然后是 Q 行,每行包含两只鸟的索引。


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.

译:对于每个测试用例,首先在一行中输出最大可能的树木数量和鸟类数量。 然后对于每个查询,如果两只鸟属于同一棵树,则在一行中打印 Yes,否则打印 No


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

The Idea:

  • 题目意思很明显的使用并查集

The Codes:

#include <bits/stdc++.h>
using namespace std ;
int n , k , id , q , t , a , b ;
const int maxn = 10010;
int fa[maxn] = {0}, cnt[maxn] = {0}; 
bool exist[maxn] ; // 记录编号为 i 的鸟是否存在
int findFather(int x){
	int a = x ;
	while(x != fa[x]) x = fa[x] ;
	while(a != fa[a]){
		int z = a ;
		a = fa[a] ;
		fa[z] = x ;
	}
	return x ;
}
void Union(int a , int b){
	int faA = findFather(a) ;
	int faB = findFather(b) ;
	if(faA != faB) fa[faA] = fa[faB] ;
}
int main() {
    cin >> n ;
    for(int i = 0 ; i < maxn ; i ++){
    	fa[i] = i ;
	}
	for(int i = 0 ; i < n ; i ++){
		cin >> k >> id ;
		exist[id] = true ;
		for(int j = 1 ; j < k ; j ++){
			cin >> t ;
			Union(id , t) ;
			exist[t] = true ;
		}
	}
	for(int i = 0  ; i < maxn ; i ++){
		if(exist[i]) cnt[findFather(i)] ++ ;
	}
	int trees = 0 , birds = 0 ;
	for(int i = 0 ; i < maxn ; i ++){
		if(exist[i] && cnt[i] != 0){
			trees ++ ;
			birds += cnt[i] ;
		}
	}
	cout << trees << ' ' << birds << endl ;
	cin >> q ;
	for(int i = 0 ; i < q ; i ++){
		cin >> a >> b ;
		printf("%s\n" , (findFather(a) == findFather(b)?"Yes":"No")) ;
	}
    return 0;
}


这篇关于PAT (Advanced Level) Practice 1118 Birds in Forest (25 分) 凌宸1642的文章就介绍到这儿,希望我们推荐的文章对大家有所帮助,也希望大家多多支持为之网!


扫一扫关注最新编程教程