1118 Birds in Forest (25 分)(超时原因)

该博客介绍了如何使用并查集解决一个经典问题:在森林中统计最大数量的树,并判断任意两只鸟是否在同一棵树上。通过输入鸟的图片信息和查询,利用并查集的路径压缩优化策略,实现高效计算和判断。文章强调了初始化、路径优化以及编号范围等细节处理的重要性。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

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

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 10
4
.

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

结尾无空行

经典并查集问题,昨天晚上没休息好,完全不在状态,连并查集都忘了初始化了淦。
注意并查集的路径优化问题,如果不优化,应该会超时

如果两只鸟的父亲一样,说明是在同一棵树上

注意一些细节:编号是从 1 开始的,路径优化问题,需要掌握

#include<iostream>
#include<bits/stdc++.h>
#include<string>
#include<set>
#include<map>
#include<vector>
#include<queue>
#include<deque>
#include<unordered_set>
#include<unordered_map>
#include<cctype>
#include<algorithm>
#include<stack>
using namespace std;
const int N = 10001;
int father[N];
bool flag[N] = {false};
set<int> ss;
int findFather(int x) {
	if (x == father[x]) {
		return x;
	} else {
		int F = findFather(father[x]);
		father[x] = F;
		return F;
	}
}
void init() {
	for (int i = 0; i < N; i++) {
		father[i] = i;
	}
}
void Union(int x, int y) {
	int fa = findFather(x);
	int fb = findFather(y);
	if (fa != fb) {
		father[fa] = fb; 
	}
}
int main() {
	int n;
	cin >> n;
	init();
	set<int>st;
	int maxn = -1;
	while (n--) {
		int k;
		cin >> k;
		int x;
		cin >> x;
		st.insert(x);
		flag[x] = true;
		maxn = max(maxn, x);
		for (int i = 1; i < k; i++) {
			int y;
			cin >> y;
			st.insert(y);
			flag[y] = true;
			maxn = max(maxn, y);
			Union(x, y);
		}
	}
	int count = 0;
	for (int i = 1; i <= maxn; i++) {
		if (father[i] == i) {
			count++;
		}
	}
	cout << count << " " << st.size() << endl;
	int m;
	cin >> m;
	while (m--) {
		int x, y;
		cin >> x >> y;
		if (findFather(x) == findFather(y)) {
			cout << "Yes" << endl; 
		} else {
			cout << "No" << endl;
		}
	}
	return 0;
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

打赏作者

_努力努力再努力_

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

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

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

打赏作者

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

抵扣说明:

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

余额充值