-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathUVa10608.cpp
More file actions
53 lines (50 loc) · 1.06 KB
/
UVa10608.cpp
File metadata and controls
53 lines (50 loc) · 1.06 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
#include <cmath>
#include <cstdio>
#include <vector>
#include <iostream>
#include <algorithm>
using namespace std;
// shortcuts for "common" data types in contests
typedef long long ll;
typedef pair<int, int> ii;
typedef vector<ii> vii;
typedef vector<int> vi;
#define INF 1000000000
class UnionFindK
{
vi p;
int nSets;
public:
UnionFindK(int N) { p.assign(N, -1); nSets = N; }
int findSet(int u) { if (p[u] < 0) return u; else return p[u] = findSet(p[u]); }
int unionSet(int u, int v) {
int uSet = findSet(u);
int vSet = findSet(v);
if (uSet != vSet) {
if (-p[vSet] > -p[uSet]) swap(uSet, vSet);
p[uSet] += p[vSet];
p[vSet] = uSet;
--nSets;
}
return -p[uSet]; // return size
}
int sizeOfSet(int u) { return -p[findSet(u)]; }
int numberOfSets() { return nSets; }
};
int main()
{
int T; scanf("%d\n", &T);
while (T--)
{
int n, m; scanf("%d %d\n", &n, &m);
UnionFindK uf(n);
int maxSize = 1;
while (m--)
{
int u, v; scanf("%d %d\n", &u, &v);
maxSize = max(maxSize, uf.unionSet(--u, --v));
}
printf("%d\n", maxSize);
}
return 0;
}