-
Notifications
You must be signed in to change notification settings - Fork 13
/
Copy path3727A.cpp
205 lines (205 loc) · 4.26 KB
/
3727A.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
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
#include <algorithm>
#include <csetjmp>
#include <cstdio>
#include <cstring>
#include <vector>
int read() {
int res = 0, c;
do
c = getchar();
while(c < '0' || c > '9');
while('0' <= c && c <= '9') {
res = res * 10 + c - '0';
c = getchar();
}
return res;
}
int s;
int SG1(int x) {
return x;
}
int SG2(int x) {
if(s & 1)
return x & 1;
int val = x % (s + 1);
if(val == s)
return 2;
return val % 2;
}
int SG3(int x) {
return x / s;
}
int SG4(int x) {
if(x == 0)
return 0;
switch(x & 3) {
case 0:
return x - 1;
case 3:
return x + 1;
default:
return x;
}
}
typedef int (*SGFunc)(int);
SGFunc fun[5] = { 0, SG1, SG2, SG3, SG4 };
const int size = 30005;
struct Edge {
int to, nxt;
} E[size * 2];
int last[size], cnt;
void addEdge(int u, int v) {
++cnt;
E[cnt].to = v, E[cnt].nxt = last[u];
last[u] = cnt;
}
int crt, tsiz, msiz, siz[size];
bool vis[size];
void reset() {
memset(last, 0, sizeof(last));
cnt = 0;
memset(vis, 0, sizeof(vis));
}
void getRootImpl(int u, int p) {
siz[u] = 1;
int csiz = 0;
for(int i = last[u]; i; i = E[i].nxt) {
int v = E[i].to;
if(p != v && !vis[v]) {
getRootImpl(v, u);
siz[u] += siz[v];
csiz = std::max(csiz, siz[v]);
}
}
csiz = std::max(csiz, tsiz - siz[u]);
if(csiz < msiz)
msiz = csiz, crt = u;
}
int getRoot(int u, int usiz) {
tsiz = usiz, msiz = 1 << 30;
getRootImpl(u, 0);
return crt;
}
const int modv = 65537;
std::vector<int> LUT[modv];
bool findA(int val) {
int id = val % modv;
return std::find(LUT[id].begin(), LUT[id].end(),
val) != LUT[id].end();
}
int xcnt = 0, xbuf[size];
void insertA(int val) {
if(!findA(val)) {
LUT[val % modv].push_back(val);
xbuf[++xcnt] = val % modv;
}
}
void doClearA() {
for(int i = 1; i <= xcnt; ++i)
LUT[xbuf[i]].clear();
xcnt = 0;
}
bool vcnt[4];
bool findB(int val) {
return vcnt[val];
}
void insertB(int val) {
vcnt[val] = true;
}
void doClearB() {
memset(vcnt, 0, sizeof(vcnt));
}
typedef bool (*FindFunc)(int);
typedef void (*InsFunc)(int);
typedef void (*ClrFunc)();
FindFunc find;
InsFunc insert;
ClrFunc doClear;
jmp_buf buf[10], *cbuf;
int SG[size], ccnt, A[size];
void DFS(int u, int p, int xorv) {
if(xorv == 0)
longjmp(*cbuf, 1);
A[++ccnt] = xorv;
for(int i = last[u]; i; i = E[i].nxt) {
int v = E[i].to;
if(v != p && !vis[v])
DFS(v, u, xorv ^ SG[v]);
}
}
void solve(int u) {
int rt = SG[u];
for(int i = last[u]; i; i = E[i].nxt) {
int v = E[i].to;
if(!vis[v]) {
ccnt = 0;
DFS(v, u, SG[v]);
for(int i = 1; i <= ccnt; ++i) {
int val = A[i] ^ rt;
if(val == 0 || find(val))
longjmp(*cbuf, 1);
}
for(int i = 1; i <= ccnt; ++i)
insert(A[i]);
}
}
doClear();
}
void divide(int u) {
vis[u] = true;
solve(u);
for(int i = last[u]; i; i = E[i].nxt) {
int v = E[i].to;
if(!vis[v]) {
int nrt = getRoot(v, siz[v]);
divide(nrt);
}
}
}
bool foo() {
reset();
int n = read();
for(int i = 1; i < n; ++i) {
int u = read();
int v = read();
addEdge(u, v);
addEdge(v, u);
}
for(int i = 1; i <= n; ++i) {
SG[i] = read();
if(SG[i] == 0)
return true;
}
int k = read();
SGFunc sgf = fun[k];
if(k == 2 || k == 3)
s = read();
for(int i = 1; i <= n; ++i)
SG[i] = sgf(SG[i]);
if(k == 2) {
insert = insertB;
find = findB;
doClear = doClearB;
} else {
insert = insertA;
find = findA;
doClear = doClearA;
}
if(setjmp(*cbuf)) {
doClear();
return true;
} else {
divide(getRoot(1, n));
return false;
}
}
int main() {
int t = read();
for(int i = 1; i <= t; ++i) {
cbuf = buf + i;
puts(foo() ?
"Mutalisk ride face how to lose?" :
"The commentary cannot go on!");
}
return 0;
}