-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathUVa11503.cpp
65 lines (62 loc) · 1.25 KB
/
UVa11503.cpp
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
54
55
56
57
58
59
60
61
62
63
64
65
#include <cmath>
#include <cstdio>
#include <vector>
#include <iostream>
#include <algorithm>
#include <map>
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
template <typename T>
class GenericUnionFind
{
vi p;
int nSets;
int idx;
map<T, int> keymap;
int findSetIdx(int u) { if (p[u] < 0) return u; else return p[u] = findSetIdx(p[u]); }
public:
GenericUnionFind(int N) { p.assign(N, -1); nSets = N; idx = 0; }
int findSet(T key) {
if (keymap.count(key) == 0)
{
keymap[key] = idx;
if (idx == (int) p.size())
p.push_back(-1);
++idx;
}
return findSetIdx(keymap[key]);
}
int unionSet(T u, T 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; cin >> T;
while (T--)
{
int m; cin >> m;
GenericUnionFind<string> uf(1);
while (m--)
{
string u, v; cin >> u >> v;
printf("%d\n", uf.unionSet(u, v));
}
}
return 0;
}