#include <bits/stdc++.h>
using namespace std;
class dsu {
public:
vector<int> p;
vector<int> sz;
int n;
dsu(int _n) : n(_n) {
p.resize(n);
sz.resize(n);
for (int i = 0; i < n; i++) {
p[i] = i;
sz[i] = 1;
}
}
inline int get(int x) {
return (x == p[x] ? x : (p[x] = get(p[x])));
}
inline bool unite(int x, int y) {
x = get(x);
y = get(y);
if (x != y) {
if (sz[x] > sz[y]) {
swap(x, y);
}
p[x] = y;
sz[y] += sz[x];
return true;
}
return false;
}
};
int main() {
// freopen("in.txt", "r", stdin);
// freopen("out.txt", "w", stdout);
ios::sync_with_stdio(false);
cin.tie(0);
int n, k;
cin >> n >> k;
vector<vector<pair<int, int>>> g(n);
int q = n - 1 + k;
vector<char> foo(q);
vector<int> x(q);
vector<int> y(q);
for (int i = 0; i < q; i++) {
cin >> foo[i] >> x[i];
if (foo[i] != 'C') {
cin >> y[i];
}
--x[i], --y[i];
if (foo[i] == 'S') {
g[x[i]].emplace_back(y[i], i);
g[y[i]].emplace_back(x[i], i);
}
}
vector<int> par(n);
vector<int> wei(n);
vector<int> dep(n);
function<void(int, int)> Dfs = [&](int u, int pr) {
dep[u] = dep[pr] + 1;
for (auto& e : g[u]) {
int v = e.first;
int w = e.second;
if (v == pr) {
continue;
}
par[v] = u;
wei[v] = w;
Dfs(v, u);
}
};
Dfs(0, 0);
wei[0] = 1000000;
const int L = 20;
vector<vector<int>> up(n, vector<int>(L));
for (int i = 0; i < n; i++) {
up[i][0] = par[i];
}
for (int j = 1; j < L; j++) {
for (int i = 0; i < n; i++) {
up[i][j] = up[up[i][j - 1]][j - 1];
}
}
auto Lca = [&](int u, int v) {
if (dep[u] < dep[v]) {
swap(u, v);
}
for (int i = L - 1; i >= 0; i--) {
if (dep[up[u][i]] >= dep[v]) {
u = up[u][i];
}
}
if (u == v) {
return u;
}
for (int i = L - 1; i >= 0; i--) {
if (up[u][i] != up[v][i]) {
u = up[u][i];
v = up[v][i];
}
}
return up[u][0];
};
auto Lift = [&](int x, int d) {
for (int i = L - 1; i >= 0; i--) {
if (d >> i & 1) {
x = up[x][i];
}
}
return x;
};
vector<vector<int>> go(n, vector<int>(2, 0));
vector<int> order(n);
iota(order.begin(), order.end(), 0);
sort(order.begin(), order.end(), [&](int i, int j) {
return dep[i] < dep[j];
});
for (int id = 1; id < n; id++) {
int i = order[id];
if (wei[par[i]] > wei[i]) {
go[i][0] = go[par[i]][0];
go[i][1] = par[i];
} else {
go[i][0] = par[i];
go[i][1] = go[par[i]][1];
}
}
vector<int> qans(q);
for (int i = 0; i < q; i++) {
if (foo[i] != 'Q') {
continue;
}
if (x[i] == y[i]) {
qans[i] = 1;
continue;
}
int z = Lca(x[i], y[i]);
bool ok = true;
if (x[i] != z && y[i] != z) {
int p_x = Lift(x[i], dep[x[i]] - dep[z] - 1);
int p_y = Lift(y[i], dep[y[i]] - dep[z] - 1);
if (wei[p_y] > wei[p_x]) {
ok = false;
}
}
if (x[i] != z && wei[x[i]] > i) {
ok = false;
}
if (y[i] != z && wei[Lift(y[i], dep[y[i]] - dep[z] - 1)] > i) {
ok = false;
}
if (dep[go[y[i]][0]] <= dep[z] && dep[go[x[i]][1]] <= dep[z] && ok) {
qans[i] = 1;
} else {
qans[i] = 0;
}
}
vector<int> ans(n, 1);
vector<pair<int, int>> edges;
if (n <= 4000) {
for (int i = 0; i < q; i++) {
if (foo[i] == 'S') {
edges.emplace_back(x[i], y[i]);
dsu d(n);
vector<int> nans(n, 1);
for (int j = edges.size() - 1; j >= 0; j--) {
int u = edges[j].first;
int v = edges[j].second;
int xv = nans[v];
int xu = nans[u];
nans[u] += xv;
nans[v] += xu;
assert(d.unite(u, v));
}
swap(ans, nans);
}
if (foo[i] == 'C') {
qans[i] = ans[x[i]];
}
}
}
for (int i = 0; i < q; i++) {
if (foo[i] == 'Q') {
cout << (qans[i] == 0 ? "no" : "yes") << '\n';
} else if (foo[i] == 'C') {
cout << qans[i] << '\n';
}
}
return 0;
}
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
59 ms |
2380 KB |
Output is correct |
2 |
Correct |
228 ms |
3592 KB |
Output is correct |
3 |
Correct |
212 ms |
3816 KB |
Output is correct |
4 |
Correct |
236 ms |
3744 KB |
Output is correct |
5 |
Correct |
191 ms |
3832 KB |
Output is correct |
6 |
Correct |
213 ms |
3804 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
59 ms |
2380 KB |
Output is correct |
2 |
Correct |
228 ms |
3592 KB |
Output is correct |
3 |
Correct |
212 ms |
3816 KB |
Output is correct |
4 |
Correct |
236 ms |
3744 KB |
Output is correct |
5 |
Correct |
191 ms |
3832 KB |
Output is correct |
6 |
Correct |
213 ms |
3804 KB |
Output is correct |
7 |
Correct |
53 ms |
2492 KB |
Output is correct |
8 |
Correct |
194 ms |
4384 KB |
Output is correct |
9 |
Correct |
203 ms |
4556 KB |
Output is correct |
10 |
Correct |
203 ms |
4480 KB |
Output is correct |
11 |
Correct |
172 ms |
4640 KB |
Output is correct |
12 |
Correct |
194 ms |
4516 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
64 ms |
2512 KB |
Output is correct |
2 |
Correct |
168 ms |
34572 KB |
Output is correct |
3 |
Correct |
214 ms |
34428 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
64 ms |
2512 KB |
Output is correct |
2 |
Correct |
168 ms |
34572 KB |
Output is correct |
3 |
Correct |
214 ms |
34428 KB |
Output is correct |
4 |
Correct |
49 ms |
2352 KB |
Output is correct |
5 |
Incorrect |
157 ms |
34508 KB |
Extra information in the output file |
6 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
43 ms |
2368 KB |
Output is correct |
2 |
Correct |
188 ms |
43092 KB |
Output is correct |
3 |
Correct |
187 ms |
43084 KB |
Output is correct |
4 |
Correct |
211 ms |
42988 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
43 ms |
2368 KB |
Output is correct |
2 |
Correct |
188 ms |
43092 KB |
Output is correct |
3 |
Correct |
187 ms |
43084 KB |
Output is correct |
4 |
Correct |
211 ms |
42988 KB |
Output is correct |
5 |
Correct |
48 ms |
2276 KB |
Output is correct |
6 |
Incorrect |
156 ms |
45700 KB |
Extra information in the output file |
7 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
53 ms |
2476 KB |
Output is correct |
2 |
Correct |
232 ms |
34608 KB |
Output is correct |
3 |
Correct |
173 ms |
34600 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
53 ms |
2476 KB |
Output is correct |
2 |
Correct |
232 ms |
34608 KB |
Output is correct |
3 |
Correct |
173 ms |
34600 KB |
Output is correct |
4 |
Correct |
57 ms |
2272 KB |
Output is correct |
5 |
Incorrect |
144 ms |
37132 KB |
Extra information in the output file |
6 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
43 ms |
2360 KB |
Output is correct |
2 |
Correct |
185 ms |
43080 KB |
Output is correct |
3 |
Correct |
165 ms |
42928 KB |
Output is correct |
4 |
Correct |
209 ms |
42952 KB |
Output is correct |
5 |
Correct |
53 ms |
2272 KB |
Output is correct |
6 |
Correct |
193 ms |
34436 KB |
Output is correct |
7 |
Correct |
169 ms |
34564 KB |
Output is correct |
8 |
Correct |
273 ms |
35260 KB |
Output is correct |
9 |
Correct |
220 ms |
35332 KB |
Output is correct |
10 |
Correct |
262 ms |
38020 KB |
Output is correct |
11 |
Correct |
339 ms |
37316 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
43 ms |
2360 KB |
Output is correct |
2 |
Correct |
185 ms |
43080 KB |
Output is correct |
3 |
Correct |
165 ms |
42928 KB |
Output is correct |
4 |
Correct |
209 ms |
42952 KB |
Output is correct |
5 |
Correct |
53 ms |
2272 KB |
Output is correct |
6 |
Correct |
193 ms |
34436 KB |
Output is correct |
7 |
Correct |
169 ms |
34564 KB |
Output is correct |
8 |
Correct |
273 ms |
35260 KB |
Output is correct |
9 |
Correct |
220 ms |
35332 KB |
Output is correct |
10 |
Correct |
262 ms |
38020 KB |
Output is correct |
11 |
Correct |
339 ms |
37316 KB |
Output is correct |
12 |
Correct |
43 ms |
2272 KB |
Output is correct |
13 |
Incorrect |
142 ms |
45592 KB |
Extra information in the output file |
14 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
50 ms |
2368 KB |
Output is correct |
2 |
Correct |
240 ms |
3568 KB |
Output is correct |
3 |
Correct |
209 ms |
3704 KB |
Output is correct |
4 |
Correct |
236 ms |
3672 KB |
Output is correct |
5 |
Correct |
193 ms |
4040 KB |
Output is correct |
6 |
Correct |
208 ms |
3776 KB |
Output is correct |
7 |
Correct |
54 ms |
2476 KB |
Output is correct |
8 |
Correct |
181 ms |
34572 KB |
Output is correct |
9 |
Correct |
185 ms |
34420 KB |
Output is correct |
10 |
Correct |
48 ms |
2244 KB |
Output is correct |
11 |
Correct |
174 ms |
43020 KB |
Output is correct |
12 |
Correct |
181 ms |
43020 KB |
Output is correct |
13 |
Correct |
232 ms |
42956 KB |
Output is correct |
14 |
Correct |
51 ms |
2248 KB |
Output is correct |
15 |
Correct |
201 ms |
34428 KB |
Output is correct |
16 |
Correct |
170 ms |
34532 KB |
Output is correct |
17 |
Correct |
259 ms |
35236 KB |
Output is correct |
18 |
Correct |
217 ms |
35356 KB |
Output is correct |
19 |
Correct |
263 ms |
37964 KB |
Output is correct |
20 |
Correct |
354 ms |
37260 KB |
Output is correct |
21 |
Correct |
192 ms |
34492 KB |
Output is correct |
22 |
Correct |
218 ms |
34440 KB |
Output is correct |
23 |
Correct |
203 ms |
34960 KB |
Output is correct |
24 |
Correct |
239 ms |
34996 KB |
Output is correct |
25 |
Correct |
236 ms |
36632 KB |
Output is correct |
26 |
Correct |
175 ms |
34352 KB |
Output is correct |
27 |
Correct |
161 ms |
34320 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
50 ms |
2368 KB |
Output is correct |
2 |
Correct |
240 ms |
3568 KB |
Output is correct |
3 |
Correct |
209 ms |
3704 KB |
Output is correct |
4 |
Correct |
236 ms |
3672 KB |
Output is correct |
5 |
Correct |
193 ms |
4040 KB |
Output is correct |
6 |
Correct |
208 ms |
3776 KB |
Output is correct |
7 |
Correct |
54 ms |
2476 KB |
Output is correct |
8 |
Correct |
181 ms |
34572 KB |
Output is correct |
9 |
Correct |
185 ms |
34420 KB |
Output is correct |
10 |
Correct |
48 ms |
2244 KB |
Output is correct |
11 |
Correct |
174 ms |
43020 KB |
Output is correct |
12 |
Correct |
181 ms |
43020 KB |
Output is correct |
13 |
Correct |
232 ms |
42956 KB |
Output is correct |
14 |
Correct |
51 ms |
2248 KB |
Output is correct |
15 |
Correct |
201 ms |
34428 KB |
Output is correct |
16 |
Correct |
170 ms |
34532 KB |
Output is correct |
17 |
Correct |
259 ms |
35236 KB |
Output is correct |
18 |
Correct |
217 ms |
35356 KB |
Output is correct |
19 |
Correct |
263 ms |
37964 KB |
Output is correct |
20 |
Correct |
354 ms |
37260 KB |
Output is correct |
21 |
Correct |
192 ms |
34492 KB |
Output is correct |
22 |
Correct |
218 ms |
34440 KB |
Output is correct |
23 |
Correct |
203 ms |
34960 KB |
Output is correct |
24 |
Correct |
239 ms |
34996 KB |
Output is correct |
25 |
Correct |
236 ms |
36632 KB |
Output is correct |
26 |
Correct |
175 ms |
34352 KB |
Output is correct |
27 |
Correct |
161 ms |
34320 KB |
Output is correct |
28 |
Correct |
55 ms |
2308 KB |
Output is correct |
29 |
Correct |
209 ms |
4396 KB |
Output is correct |
30 |
Correct |
188 ms |
4516 KB |
Output is correct |
31 |
Correct |
218 ms |
4444 KB |
Output is correct |
32 |
Correct |
172 ms |
4680 KB |
Output is correct |
33 |
Correct |
201 ms |
4508 KB |
Output is correct |
34 |
Correct |
59 ms |
3008 KB |
Output is correct |
35 |
Incorrect |
174 ms |
36928 KB |
Extra information in the output file |
36 |
Halted |
0 ms |
0 KB |
- |