/**********
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;
}
unordered_map
最新推荐文章于 2024-04-09 10:30:49 发布
本文介绍了如何使用unordered_map高效地建立大数之间的关系,并结合并查集的数据结构优化搜索深度。通过数组替代键值,展示了在Vjudge竞赛平台上一道题目(F)中的解决方案,适合C++11标准。
摘要由CSDN通过智能技术生成