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 (≤10^4) which is the number of pictures. Then N lines follow, each describes a picture in the format:

K B?1?? B?2?? ... B?K??

where K is the number of birds in this picture, and B?i??‘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 1.

After the pictures there is a positive number Q (≤10^4) 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

题目描述:

第一行输入n组数据,每一组数据表示这些鸟在同一颗树上。最后输出,有几棵树,和鸟的数量。

再判断给一对鸟,是不是在同一颗树上,是输出Yes,不在同一颗树上输出No

解题思路:

这道题是典型的并查集题目,这里要注意一点是是,每次找节点的父节点时,应该直接把这个节点的父节点更新为最顶上的父节点,避免重复寻找例如3 -> 5 -> 6 ->8 更新完就变为  3->8, 5->8  6->8,

要不然会有个测试用例会超时

int find(int x){  //递归寻找每个节点的根节点
	if(x == father[x]){
		return x;
	}
	return find(father[x]);    //不更新路径
}

不超时应该  return father[x] = find(father[x])

正确的代码如下:

#include<cstdio>
#include<cstdlib>
#include<set>
#include<vector>
#include<algorithm>

using namespace std;

const int maxn = 10009; //题目要求10^4
int father[maxn];    //存放每个下标的父节点下标,根节点的父节点是自身 

int find(int x){  //递归寻找每个节点的根节点
	if(x == father[x]){
		return x;
	}
	return father[x] = find(father[x]);
}

void Union(int x,int y){  //合并两个节点
	int fx = find(x);
	int fy = find(y);

	if(fx != fy){
		father[fx] = fy;
	}
}

int main(){

	int n,k,a,b;

	int max = 0; //鸟的数量是最大的下标 

//	freopen("C:\\Users\\zzloyxt\\Desktop\\1.txt","r",stdin);
	scanf("%d",&n);
	for(int i=1;i<maxn;i++){
		father[i] = i; // 初始化数组,节点自己的father是自己
	}

	for(int i=1;i<=n;i++){
		scanf("%d",&k); // 输入有k 只鸟
		scanf("%d",&a); //输入第一只鸟
		if(a>max){
			max = a;
		}
		for(int j=1;j<k;j++){  // 输入剩余的k-1只
			scanf("%d",&b);
			if(b>max){
				max = b;
			}
			Union(a,b);
		}

	}	

	//输出鸟的数量和树的数量,因为下标是从1-m所以鸟的数量就是最大的m
	int count = 0;

	for(int i=1;i<=max;i++){
		if(find(i) == i){
			count++;
		}
	}

	printf("%d %d\n",count,max);
	scanf("%d",&k);
	for(int i=0;i<k;i++){
		scanf("%d %d",&a,&b);

		if(find(a) == find(b)){
			printf("Yes\n");
		}else{
			printf("No\n");
		}
	}

	return 0;
}

原文地址:https://www.cnblogs.com/zzlback/p/12409035.html

时间: 2024-10-28 23:32:24

1118 Birds in Forest (25分) 并查集的相关文章

PAT A 1118. Birds in Forest (25)

并查集合并 #include<iostream> using namespace std; const int MAX = 10010; int father[MAX],root[MAX]; int findfather(int x){ if(x==father[x]) return x; else{ int F=findfather(father[x]); father[x]=F; return F; } } void Union(int a , int b){ int faA=findfa

PAT-1021 Deepest Root (25 分) 并查集判断成环和联通+求树的深度

A graph which is connected and acyclic can be considered a tree. The height of the tree depends on the selected root. Now you are supposed to find the root that results in a highest tree. Such a root is called the deepest root. Input Specification: E

PAT甲级——1118 Birds in Forest (并查集)

此文章 同步发布在CSDN:https://blog.csdn.net/weixin_44385565/article/details/89819984 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. Yo

PAT 1118 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 pai

PAT-1107 Social Clusters (30 分) 并查集模板

1107 Social Clusters (30 分) When register on a social network, you are always asked to specify your hobbies in order to find some potential friends with the same hobbies. A social cluster is a set of people who have some of their hobbies in common. Y

PAT甲题题解-1114. Family Property (25)-(并查集模板题)

题意:给出每个人的家庭成员信息和自己的房产个数与房产总面积,让你统计出每个家庭的人口数.人均房产个数和人均房产面积.第一行输出家庭个数,随后每行输出家庭成员的最小编号.家庭人口数.人均房产个数.人均房产面积. 并查集,合并的时候编号小的作为父亲节点,最后父亲节点一样的即属于一个家庭,其它都是细节处理没啥好说了. #include <iostream> #include <cstdio> #include <algorithm> #include <string.h

1053 Path of Equal Weight (30分)(并查集)

Given a non-empty tree with root R, and with weight W?i?? assigned to each tree node T?i??. The weight of a path from R to L is defined to be the sum of the weights of all the nodes along the path from R to any leaf node L. Now given any weighted tre

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 pai

POJ 2524 并查集

Ubiquitous Religions Time Limit: 5000MS Memory Limit: 65536K Total Submissions: 23580 Accepted: 11609 Description There are so many different religions in the world today that it is difficult to keep track of them all. You are interested in finding o