ICode9

精准搜索请尝试: 精确搜索
首页 > 其他分享> 文章详细

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

2021-08-14 21:32:31  阅读:152  来源: 互联网

标签:25 PAT Level int ++ number fa maxn birds


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;
}

标签:25,PAT,Level,int,++,number,fa,maxn,birds
来源: https://www.cnblogs.com/lingchen1642/p/15141903.html

本站声明: 1. iCode9 技术分享网(下文简称本站)提供的所有内容,仅供技术学习、探讨和分享;
2. 关于本站的所有留言、评论、转载及引用,纯属内容发起人的个人观点,与本站观点和立场无关;
3. 关于本站的所有言论和文字,纯属内容发起人的个人观点,与本站观点和立场无关;
4. 本站文章均是网友提供,不完全保证技术分享内容的完整性、准确性、时效性、风险性和版权归属;如您发现该文章侵犯了您的权益,可联系我们第一时间进行删除;
5. 本站为非盈利性的个人网站,所有内容不会用来进行牟利,也不会利用任何形式的广告来间接获益,纯粹是为了广大技术爱好者提供技术内容和技术思想的分享性交流网站。

专注分享技术,共同学习,共同进步。侵权联系[81616952@qq.com]

Copyright (C)ICode9.com, All Rights Reserved.

ICode9版权所有