-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathForests2.cpp
More file actions
85 lines (68 loc) · 2.42 KB
/
Forests2.cpp
File metadata and controls
85 lines (68 loc) · 2.42 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
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
#include <iostream>
#include <sstream>
#include <map>
#include <set>
#include <bits/stdc++.h>
using namespace std;
typedef vector<int> vi;
class UnionFind { // OOP style
private:
vi p, rank, setSize; // vi p is the key part
int numSets;
public:
UnionFind(int N) {
p.assign(N, 0); for (int i = 0; i < N; ++i) p[i] = i;
rank.assign(N, 0); // optional speedup
setSize.assign(N, 1); // optional feature
numSets = N; // optional feature
}
int findSet(int i) { return (p[i] == i) ? i : (p[i] = findSet(p[i])); }
bool isSameSet(int i, int j) { return findSet(i) == findSet(j); }
int numDisjointSets() { return numSets; } // optional
int sizeOfSet(int i) { return setSize[findSet(i)]; } // optional
void unionSet(int i, int j) {
if (isSameSet(i, j)) return; // i and j are in same set
int x = findSet(i), y = findSet(j); // find both rep items
if (rank[x] > rank[y]) swap(x, y); // keep x 'shorter' than y
p[x] = y; // set x under y
if (rank[x] == rank[y]) ++rank[y]; // optional speedup
setSize[y] += setSize[x]; // combine set sizes at y
--numSets; // a union reduces numSets
}
};
int main() {
int casos;
int P, T;
int i, j;
map<int,set<int>> map;
set<set<int>> sets;
string s;
while (!cin.eof())
{
cin >> casos;
while (casos > 0)
{
sets.clear();
map.clear();
cin >> P >> T;
getline(cin, s);
UnionFind UF(P+T);
/*for (int k = 1; k <= P; k++)
UF.unionSet(k + T, 0);*/
//map[i].insert(0);
while (getline(cin, s) && (int)s[0] != 13){
stringstream ss(s);
ss >> i >> j;
//map[i].insert(j);
UF.unionSet(i + T - 1, j - 1);
}
/*for (auto m : map)
sets.insert(m.second);*/
//cout << sets.size() << endl;
cout << UF.numDisjointSets() << endl;
--casos;
if(casos > 0)
cout << endl;
}
}
}