标签:链接 friends tps space 并查集 targe str names include
题目链接请戳 这里
解题思路
基本的并查集
代码
#include<stdio.h> #include<string.h> #include<algorithm> #define N 30010 #define M 50010 using namespace std; int cit[N], gro[N]; void make_set(int n) { for (int i = 1; i <= n; i++) cit[i] = i; } int get_fa(int x) { return (cit[x] == x) ? x : cit[x] = get_fa(cit[x]); } int main() { int t; int n, m; scanf("%d", &t); while (t--) { scanf("%d%d", &n, &m); make_set(n); memset(gro, 0, sizeof(gro)); for (int i = 0; i < m; i++) { int a, b; scanf("%d%d", &a, &b); int fa = get_fa(a); int fb = get_fa(b); cit[fa] = fb; } //需要整理各个并查集 for (int i = 1; i <= n; i++) get_fa(i); for (int i = 1; i <= n; i++) gro[cit[i]]++; int maxn = 0; for (int i = 1; i <= n; i++) maxn = max(maxn, gro[i]); printf("%d\n", maxn); } return 0; }
标签:链接 friends tps space 并查集 targe str names include
原文地址:http://www.cnblogs.com/ZengWangli/p/6151900.html