unordered_map

本文介绍了如何使用unordered_map高效地建立大数之间的关系,并结合并查集的数据结构优化搜索深度。通过数组替代键值,展示了在Vjudge竞赛平台上一道题目(F)中的解决方案,适合C++11标准。
摘要由CSDN通过智能技术生成
/**********
Author Smile
对于大数建立关系可以用unordered_map
搜索并查集的深度也可以用unordered_map
其中的键和值可以用数组来代替,便于访问
注:编译必须用-std=c++11
https://vjudge.csgrandeur.cn/contest/476820#problem/F
**********/
#include <iostream>
#include <unordered_map>

using namespace std;
typedef long long ll;
const ll N = 1E5 + 10;
unordered_map<ll, ll> F, C;
int T, n;
ll a[N], b[N];

ll find(ll x) {
	return x == F[x] ? x : F[x] = find(F[x]);
}

int main() {
	ios::sync_with_stdio(false);
	cin.tie(0), cout.tie(0);
	cin >> T;
	while (T--) {
		F.clear();
		C.clear();
		cin >> n;
		for (int i = 1; i <= n; ++i) {
			cin >> a[i] >> b[i];
			F[a[i]] = a[i];
			F[b[i]] = b[i];
		}
		for (int i = 1; i <= n; ++i) {
			if (find(a[i]) != find(b[i])) {
				F[find(b[i])] = F[find(a[i])];
			}
		}
		ll maxx = 0;
		for (int i = 1; i <= n; ++i) {
			C[find(a[i])]++;
			C[find(b[i])]++;
			maxx = max(maxx, C[find(a[i])]);
			maxx = max(maxx, C[find(b[i])]);
		}
		cout << maxx / 2 << endl;
	}
	return 0;
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值