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;
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

lingchen0522

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值