-
Notifications
You must be signed in to change notification settings - Fork 23
Expand file tree
/
Copy path1703.cpp
More file actions
112 lines (107 loc) · 1.82 KB
/
1703.cpp
File metadata and controls
112 lines (107 loc) · 1.82 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
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
#include <stdio.h>
#include <algorithm>
#include <vector>
using namespace std;
int root[100005];
int distnode[100005];
int distval[100005];
int getroot(int x)
{
if (x == root[x]) return x;
return getroot(root[x]);
}
void mergeroot(int x, int y)
{
int getrootx = getroot(x);
int getrooty = getroot(y);
if (getrootx == getrooty) return;
int newroot = min(getrootx, getrooty);
vector<int> v;
int xx = x;
v.push_back(xx);
while (root[xx] != xx)
{
xx = root[xx];
v.push_back(xx);
}
int yy = y;
v.push_back(yy);
while (root[yy] != yy)
{
yy = root[yy];
v.push_back(yy);
}
for (int i = 0; i < v.size(); i++)
{
root[v[i]] = newroot;
}
if (getrootx == newroot)
{
distval[y] += 1+distval[x]%2;
distnode[y] = distnode[x];
}
else
{
distval[x] += 1+distval[y]%2;
distnode[x] = distnode[y];
}
}
void update(int x)
{
int node = distnode[x];
while (distnode[node] != node)
{
distval[x] += distval[node]%2;
node = distnode[node];
}
distnode[x] = node;
}
int main()
{
int t, i, j, k, n, m;
char c[4];
scanf("%d", &t);
while (t > 0)
{
t--;
scanf("%d %d", &n, &m);
for (i = 1; i <= n; i++)
{
root[i] = i;
distnode[i] = i;
distval[i] = 0;
}
for (i = 0; i < m; i++)
{
int a, b;
scanf("%s %d %d", &c, &a, &b);
update(a);
update(b);
if (c[0] == 'A')
{
if (n == 2)
{
if (a != b) printf("In different gangs.\n");
else printf("In the same gang.\n");
}
else if (getroot(a) != getroot(b))
{
printf("Not sure yet.\n");
}
else if (distval[a]%2 == distval[b]%2)
{
printf("In the same gang.\n");
}
else
{
printf("In different gangs.\n");
}
}
else
{
mergeroot(a, b);
}
}
}
return 0;
}