#include <bits/stdc++.h>
using namespace std;
typedef pair<int, int> pii;
const int MAXN = 120000;
vector<pii> edges[MAXN];
pair<pii, int> queries[MAXN]; // start, end, time
int lca[MAXN][20];
pii lca_range[MAXN][20];
pii lca_range_rev[MAXN][20];
int depth[MAXN];
int n, k;
void dfs(int cur = 0, int p = -1) {
for (pii e: edges[cur]) {
int next = e.first;
if (next == p) continue;
depth[next] = depth[cur]+1;
lca[next][0] = cur;
lca_range[next][0] = pii(e.second, e.second);
lca_range_rev[next][0] = pii(e.second, e.second);
dfs(next, cur);
}
}
bool move(int &i, int pow, int &s) {
pii r = lca_range[i][pow];
if (s >= r.first) return 0;
s = r.second;
i = lca[i][pow];
return 1;
}
bool move_rev(int &i, int pow, int &e) {
pii r = lca_range_rev[i][pow];
if (r.second >= e || r.second == -1) return 0;
e = r.first;
i = lca[i][pow];
return 1;
}
void calc_lca(int i) {
int a = queries[i].first.first;
int b = queries[i].first.second;
int s = -1;
int e = queries[i].second;
int diff = depth[a]-depth[b];
int pow = 0;
while (diff > 0) {
if (diff & 1) {
bool bo = move(a, pow, s);
if (!bo) {
cout << "no\n";
return;
}
}
pow++;
diff /= 2;
}
diff = depth[b]-depth[a];
pow = 0;
while (diff > 0) {
if (diff & 1) {
bool bo = move_rev(b, pow, e);
if (!bo) {
cout << "no\n";
return;
}
}
pow++;
diff /= 2;
}
for (int i = 19; i >= 0; i--) {
if (lca[a][i] != lca[b][i]) {
bool bo = move(a, i, s);
if (!bo) {
cout << "no\n";
return;
}
bo = move_rev(b, i, e);
if (!bo) {
cout << "no\n";
return;
}
}
}
if (a != b) {
bool bo = move(a, 0, s);
if (!bo) {
cout << "no\n";
return;
}
bo = move_rev(b, 0, e);
if (!bo) {
cout << "no\n";
return;
}
}
if (s >= e) {
cout << "no\n";
}
else cout << "yes\n";
}
int main() {
ios_base::sync_with_stdio(false); cin.tie(NULL);
cin >> n >> k;
int t = 0;
int p = 0;
for (int i = 0; i < n+k-1; i++) {
char c;
int a, b;
cin >> c >> a >> b;
a--; b--;
assert(c != 'C');
if (c == 'S') {
edges[a].push_back(pii(b, t));
edges[b].push_back(pii(a, t));
t++;
}
else {
queries[p] = pair<pii, int>(pii(b, a), t);
p++;
}
}
assert(t == n-1);
assert(p == k);
dfs();
lca_range[0][0] = pii(-1, -1);
lca_range_rev[0][0] = pii(-1, -1);
for (int i = 1; i < 20; i++) {
for (int j = 0; j < n; j++) {
lca[j][i] = lca[lca[j][i-1]][i-1];
pii r1 = lca_range[j][i-1];
pii r2 = lca_range[lca[j][i-1]][i-1];
if (r1.second >= 0 && r1.second < r2.first) lca_range[j][i] = pii(r1.first, r2.second);
else lca_range[j][i] = pii(-1, -1);
r1 = lca_range_rev[j][i-1];
r2 = lca_range_rev[lca[j][i-1]][i-1];
if (r2.second >= 0 && r2.second < r1.first) lca_range_rev[j][i] = pii(r2.first, r1.second);
else lca_range_rev[j][i] = pii(-1, -1);
}
}
for (int i = 0; i < k; i++) {
calc_lca(i);
}
}
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
22 ms |
4932 KB |
Output is correct |
2 |
Correct |
29 ms |
8012 KB |
Output is correct |
3 |
Correct |
36 ms |
8060 KB |
Output is correct |
4 |
Correct |
29 ms |
8052 KB |
Output is correct |
5 |
Correct |
28 ms |
8268 KB |
Output is correct |
6 |
Correct |
30 ms |
8084 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
22 ms |
4932 KB |
Output is correct |
2 |
Correct |
29 ms |
8012 KB |
Output is correct |
3 |
Correct |
36 ms |
8060 KB |
Output is correct |
4 |
Correct |
29 ms |
8052 KB |
Output is correct |
5 |
Correct |
28 ms |
8268 KB |
Output is correct |
6 |
Correct |
30 ms |
8084 KB |
Output is correct |
7 |
Runtime error |
5 ms |
6368 KB |
Execution killed with signal 6 |
8 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
28 ms |
4944 KB |
Output is correct |
2 |
Correct |
172 ms |
59916 KB |
Output is correct |
3 |
Correct |
167 ms |
59928 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
28 ms |
4944 KB |
Output is correct |
2 |
Correct |
172 ms |
59916 KB |
Output is correct |
3 |
Correct |
167 ms |
59928 KB |
Output is correct |
4 |
Runtime error |
6 ms |
6356 KB |
Execution killed with signal 6 |
5 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
22 ms |
4980 KB |
Output is correct |
2 |
Correct |
189 ms |
66940 KB |
Output is correct |
3 |
Correct |
182 ms |
66948 KB |
Output is correct |
4 |
Correct |
176 ms |
66884 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
22 ms |
4980 KB |
Output is correct |
2 |
Correct |
189 ms |
66940 KB |
Output is correct |
3 |
Correct |
182 ms |
66948 KB |
Output is correct |
4 |
Correct |
176 ms |
66884 KB |
Output is correct |
5 |
Runtime error |
6 ms |
6376 KB |
Execution killed with signal 6 |
6 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
24 ms |
4916 KB |
Output is correct |
2 |
Correct |
162 ms |
60396 KB |
Output is correct |
3 |
Correct |
164 ms |
60788 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
24 ms |
4916 KB |
Output is correct |
2 |
Correct |
162 ms |
60396 KB |
Output is correct |
3 |
Correct |
164 ms |
60788 KB |
Output is correct |
4 |
Runtime error |
7 ms |
6356 KB |
Execution killed with signal 6 |
5 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
20 ms |
4840 KB |
Output is correct |
2 |
Correct |
165 ms |
66988 KB |
Output is correct |
3 |
Correct |
170 ms |
67080 KB |
Output is correct |
4 |
Correct |
164 ms |
66892 KB |
Output is correct |
5 |
Correct |
30 ms |
5836 KB |
Output is correct |
6 |
Correct |
175 ms |
60420 KB |
Output is correct |
7 |
Correct |
168 ms |
60764 KB |
Output is correct |
8 |
Correct |
187 ms |
61156 KB |
Output is correct |
9 |
Correct |
176 ms |
61200 KB |
Output is correct |
10 |
Correct |
203 ms |
65028 KB |
Output is correct |
11 |
Correct |
248 ms |
64460 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
20 ms |
4840 KB |
Output is correct |
2 |
Correct |
165 ms |
66988 KB |
Output is correct |
3 |
Correct |
170 ms |
67080 KB |
Output is correct |
4 |
Correct |
164 ms |
66892 KB |
Output is correct |
5 |
Correct |
30 ms |
5836 KB |
Output is correct |
6 |
Correct |
175 ms |
60420 KB |
Output is correct |
7 |
Correct |
168 ms |
60764 KB |
Output is correct |
8 |
Correct |
187 ms |
61156 KB |
Output is correct |
9 |
Correct |
176 ms |
61200 KB |
Output is correct |
10 |
Correct |
203 ms |
65028 KB |
Output is correct |
11 |
Correct |
248 ms |
64460 KB |
Output is correct |
12 |
Runtime error |
6 ms |
6356 KB |
Execution killed with signal 6 |
13 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
22 ms |
4884 KB |
Output is correct |
2 |
Correct |
30 ms |
7992 KB |
Output is correct |
3 |
Correct |
33 ms |
7996 KB |
Output is correct |
4 |
Correct |
30 ms |
8028 KB |
Output is correct |
5 |
Correct |
31 ms |
8140 KB |
Output is correct |
6 |
Correct |
31 ms |
8012 KB |
Output is correct |
7 |
Correct |
31 ms |
5796 KB |
Output is correct |
8 |
Correct |
162 ms |
59920 KB |
Output is correct |
9 |
Correct |
159 ms |
59896 KB |
Output is correct |
10 |
Correct |
22 ms |
5772 KB |
Output is correct |
11 |
Correct |
174 ms |
66948 KB |
Output is correct |
12 |
Correct |
170 ms |
67040 KB |
Output is correct |
13 |
Correct |
184 ms |
66816 KB |
Output is correct |
14 |
Correct |
25 ms |
5764 KB |
Output is correct |
15 |
Correct |
157 ms |
60348 KB |
Output is correct |
16 |
Correct |
170 ms |
60844 KB |
Output is correct |
17 |
Correct |
209 ms |
61260 KB |
Output is correct |
18 |
Correct |
192 ms |
61188 KB |
Output is correct |
19 |
Correct |
203 ms |
65104 KB |
Output is correct |
20 |
Correct |
214 ms |
64456 KB |
Output is correct |
21 |
Correct |
174 ms |
60412 KB |
Output is correct |
22 |
Correct |
167 ms |
60516 KB |
Output is correct |
23 |
Correct |
162 ms |
60724 KB |
Output is correct |
24 |
Correct |
178 ms |
60868 KB |
Output is correct |
25 |
Correct |
191 ms |
62048 KB |
Output is correct |
26 |
Correct |
165 ms |
60280 KB |
Output is correct |
27 |
Correct |
157 ms |
60232 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
22 ms |
4884 KB |
Output is correct |
2 |
Correct |
30 ms |
7992 KB |
Output is correct |
3 |
Correct |
33 ms |
7996 KB |
Output is correct |
4 |
Correct |
30 ms |
8028 KB |
Output is correct |
5 |
Correct |
31 ms |
8140 KB |
Output is correct |
6 |
Correct |
31 ms |
8012 KB |
Output is correct |
7 |
Correct |
31 ms |
5796 KB |
Output is correct |
8 |
Correct |
162 ms |
59920 KB |
Output is correct |
9 |
Correct |
159 ms |
59896 KB |
Output is correct |
10 |
Correct |
22 ms |
5772 KB |
Output is correct |
11 |
Correct |
174 ms |
66948 KB |
Output is correct |
12 |
Correct |
170 ms |
67040 KB |
Output is correct |
13 |
Correct |
184 ms |
66816 KB |
Output is correct |
14 |
Correct |
25 ms |
5764 KB |
Output is correct |
15 |
Correct |
157 ms |
60348 KB |
Output is correct |
16 |
Correct |
170 ms |
60844 KB |
Output is correct |
17 |
Correct |
209 ms |
61260 KB |
Output is correct |
18 |
Correct |
192 ms |
61188 KB |
Output is correct |
19 |
Correct |
203 ms |
65104 KB |
Output is correct |
20 |
Correct |
214 ms |
64456 KB |
Output is correct |
21 |
Correct |
174 ms |
60412 KB |
Output is correct |
22 |
Correct |
167 ms |
60516 KB |
Output is correct |
23 |
Correct |
162 ms |
60724 KB |
Output is correct |
24 |
Correct |
178 ms |
60868 KB |
Output is correct |
25 |
Correct |
191 ms |
62048 KB |
Output is correct |
26 |
Correct |
165 ms |
60280 KB |
Output is correct |
27 |
Correct |
157 ms |
60232 KB |
Output is correct |
28 |
Runtime error |
5 ms |
6352 KB |
Execution killed with signal 6 |
29 |
Halted |
0 ms |
0 KB |
- |