-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsocnet.cpp
More file actions
69 lines (63 loc) · 1.23 KB
/
socnet.cpp
File metadata and controls
69 lines (63 loc) · 1.23 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
#include <iostream>
#include <cstdio>
using namespace std;
int parent[100005], wt[100005];
void initialise(int len) {
for (int i = 0; i <= len; i++) {
parent[i] = i;
wt[i] = 1;
}
}
int root(int i) {
while (i != parent[i]) {
parent[i] = parent[parent[i]];
i = parent[i];
}
return i;
}
void merge (int a, int b, int m) {
if (root(a) != root(b) && wt[root(a)]+ wt[root(b)] <= m) {
if (wt[root(a)] > wt[root(b)]) {
wt[root(b)] += wt[root(a)];
parent[root(a)] = root(b);
}
else {
wt[root(a)] += wt[root(b)];
parent[root(b)] = root(a);
}
}
}
void display(int len) {
for (int i = 1; i <= len; ++i) {
cout << parent[i] << " ";
}
cout << endl;
for (int i = 1; i <= len; ++i) {
cout << wt[i] << " ";
}
cout << endl;
}
int main() {
freopen("input.txt", "r", stdin);
int users, m, q, x, y;
char type;
scanf("%d %d %d", &users, &m, &q);;
initialise(users);
while (q--) {
scanf("%s", &type);;
switch(type) {
case 'A': scanf("%d %d", &x, &y);
merge(x, y, m);
break;
case 'E': scanf("%d %d", &x, &y);
if (root(x) == root(y))
printf("Yes\n");
else printf("No\n");
break;
case 'S': scanf("%d", &x);
printf("%d\n", wt[root(x)]);
break;
}
}
return 0;
}