-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathUnionFind.cpp
125 lines (107 loc) · 2.23 KB
/
UnionFind.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
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
#include <iostream>
#include <time.h>
#include <set>
#include <stack>
using namespace std;
int N, M, Q, T, test_case, Answer, tf, tps, tp, tq, tmp;
int ufA[100001][2], PQ[100001][3];
set<int> s;
stack<int> res;
/*Union Find class*/
class UF {
int id[100001];
int N;
int uF_root(int p);
int sz[100001];
public:
UF(int n);
void uF_union(int p, int q);
int uF_find(int p, int q);
};
UF::UF(int n) {
memset(id,0, sizeof(id));
memset(sz,0, sizeof(id));
N = n;
for (int i=0; i<N; i++) {
id[i] = i;
}
};
int UF::uF_root(int x) {
while (x != id[x]) {
id[x] = id[id[x]];
x = id[x];
}
return x;
};
int UF::uF_find(int p, int q) {
return uF_root(p) == uF_root(q) ? 1 : 0 ;
};
void UF::uF_union(int p, int q) {
int qr = uF_root(q);
int pr = uF_root(p);
if (qr==pr) return;
if(sz[qr] < sz[pr]) {
id[qr] = pr;
sz[pr]+=sz[qr];
} else {
id[pr] = qr;
sz[qr]+=sz[pr];
}
//id[pr] = qr;
}
int main(){
scanf("%d", &T);
clock_t tStart = clock();
for (test_case = 1; test_case<= T; test_case++) {
scanf("%d %d", &N, &M);
memset(ufA,0,sizeof(ufA));
memset(PQ, 0, sizeof(PQ));
for (int i=0; i<M; i++) {
scanf("%d %d", &ufA[i][0], &ufA[i][1]);
ufA[i][0]--;
ufA[i][1]--;
}
scanf("%d", &Q);
for (int i=0; i<Q; i++) {
scanf("%d", &tf);
if (tf==1) {
scanf("%d", &tps);
PQ[i][0] = tf;
PQ[i][1] = --tps;
s.insert(PQ[i][1]);
} else if (tf==2) {
scanf("%d %d", &tp, &tq);
PQ[i][0] = tf;
PQ[i][1] = --tp;
PQ[i][2] = --tq;
}
}
UF uf(N);
for (int i=0; i< M; i++) {
if(s.find(i) == s.end()){
uf.uF_union(ufA[i][0], ufA[i][1]);
}
}
cout << "#" << test_case << " ";
for (int i=Q-1; i>=0; i--) {
if (PQ[i][0] == 2) {
res.push(uf.uF_find(PQ[i][1], PQ[i][2]));
} else if (PQ[i][0] == 1) {
uf.uF_union(ufA[PQ[i][1]][0], ufA[PQ[i][1]][1]);
}
}
/*for (int i = 0; i< Q; i++) {
if (PQ[i][0] == 2) {
uf.uF_find(PQ[i][1], PQ[i][2]);
}
}*/
while (!res.empty()) {
tmp = res.top();
cout << tmp;
res.pop();
}
cout << endl;
}
printf("Time taken: %.2fs\n", (double)(clock() - tStart) / CLOCKS_PER_SEC);
return 0;
}