#include <bits/stdc++.h>
#define fi first
#define se second
using namespace std;
const int N_MAX = 1.2e5 + 11;
const int LG_N = 20;
vector<int> G[N_MAX], H[N_MAX];
vector<pair<int, int>> Q;
int anc[LG_N][N_MAX], p[N_MAX], d[N_MAX];
int st[N_MAX], ed[N_MAX];
bool vis[N_MAX];
bool in_stack[N_MAX];
int n, q;
void clear(){
for(int i = 0; i <= n; i++) G[i].clear();
for(int i = 0; i <= n; i++) H[i].clear();
fill(st, st + n + 1, 0);
fill(ed, ed + n + 1, 0);
fill(vis, vis + n + 1, 0);
fill(in_stack, in_stack + n + 1, 0);
Q.clear();
}
void dfs(int v, int pa = -1){
for(auto i : G[v]){
if(i != pa){
p[i] = anc[0][i] = v; d[i] = d[v] + 1;
for(int j = 1; j < LG_N; j++){
anc[j][i] = anc[j - 1][anc[j - 1][i]];
}
dfs(i, v);
}
}
}
bool dfs2(int v){
vis[v] = in_stack[v] = true;
for(auto i : H[v]){
if(in_stack[i]) return true;
if(vis[i]) continue;
bool fail = dfs2(i);
if(fail) return true;
}
in_stack[v] = false;
return false;
}
int kth_anc(int u, int k){
for(int j = LG_N - 1; j >= 0; j--){
if(k & (1 << j)) u = anc[j][u];
}
return u;
}
int lca(int u, int v){
if(d[u] < d[v]) swap(u, v);
u = kth_anc(u, d[u] - d[v]);
for(int j = LG_N - 1; j >= 0; j--){
if(anc[j][u] != anc[j][v]){
u = anc[j][u], v = anc[j][v];
}
}
return u == v ? u : p[u];
}
void solve(){
cin >> n;
for(int i = 0; i < n - 1; i++){
int u, v; cin >> u >> v;
G[u].push_back(v);
G[v].push_back(u);
}
dfs(1);
cin >> q;
Q.push_back({-1, -1});
for(int i = 1; i <= q; i++){
int x, y; cin >> x >> y;
Q.push_back({x, y});
st[x] = i; ed[y] = i;
}
for(int i = 1; i <= q; i++){
auto [x, y] = Q[i]; int l = lca(x, y);
vector<int> P, p1, p2;
while(x != l){
p1.push_back(x); x = p[x];
}
while(y != l){
p2.push_back(y); y = p[y];
}
for(auto i : p1) P.push_back(i);
P.push_back(l);
reverse(p2.begin(), p2.end());
for(auto i : p2) P.push_back(i);
for(auto p : P) {
if(st[p] && st[p] != i){
H[i].push_back(st[p]);
}
if(ed[p] && ed[p] != i){
H[ed[p]].push_back(i);
}
}
}
bool fail = false;
for(int i = 1; i <= q; i++){
if(!vis[i]) fail |= dfs2(i);
if(fail) break;
}
cout << (!fail ? "Yes" : "No") << endl;
}
int32_t main(){
int q; cin >> q;
while(q--){
solve();
clear();
}
}
Compilation message
jail.cpp: In function 'void solve()':
jail.cpp:84:8: warning: structured bindings only available with '-std=c++17' or '-std=gnu++17'
84 | auto [x, y] = Q[i]; int l = lca(x, y);
| ^
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
3 ms |
5972 KB |
Output is correct |
2 |
Correct |
3 ms |
5972 KB |
Output is correct |
3 |
Correct |
4 ms |
5972 KB |
Output is correct |
4 |
Correct |
26 ms |
6100 KB |
Output is correct |
5 |
Correct |
53 ms |
6828 KB |
Output is correct |
6 |
Correct |
6 ms |
6100 KB |
Output is correct |
7 |
Correct |
6 ms |
6092 KB |
Output is correct |
8 |
Correct |
9 ms |
6220 KB |
Output is correct |
9 |
Correct |
185 ms |
10548 KB |
Output is correct |
10 |
Correct |
704 ms |
33808 KB |
Output is correct |
11 |
Correct |
17 ms |
6228 KB |
Output is correct |
12 |
Correct |
90 ms |
7136 KB |
Output is correct |
13 |
Correct |
256 ms |
62640 KB |
Output is correct |
14 |
Correct |
291 ms |
77292 KB |
Output is correct |
15 |
Execution timed out |
5090 ms |
832984 KB |
Time limit exceeded |
16 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
3 ms |
5972 KB |
Output is correct |
2 |
Correct |
3 ms |
5972 KB |
Output is correct |
3 |
Correct |
6 ms |
6056 KB |
Output is correct |
4 |
Correct |
5 ms |
6100 KB |
Output is correct |
5 |
Correct |
6 ms |
6092 KB |
Output is correct |
6 |
Correct |
6 ms |
6200 KB |
Output is correct |
7 |
Correct |
6 ms |
6100 KB |
Output is correct |
8 |
Correct |
7 ms |
6100 KB |
Output is correct |
9 |
Correct |
5 ms |
6100 KB |
Output is correct |
10 |
Correct |
6 ms |
6088 KB |
Output is correct |
11 |
Correct |
6 ms |
6100 KB |
Output is correct |
12 |
Correct |
5 ms |
6104 KB |
Output is correct |
13 |
Correct |
5 ms |
6088 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
3 ms |
5972 KB |
Output is correct |
2 |
Correct |
3 ms |
5972 KB |
Output is correct |
3 |
Correct |
6 ms |
6056 KB |
Output is correct |
4 |
Correct |
5 ms |
6100 KB |
Output is correct |
5 |
Correct |
6 ms |
6092 KB |
Output is correct |
6 |
Correct |
6 ms |
6200 KB |
Output is correct |
7 |
Correct |
6 ms |
6100 KB |
Output is correct |
8 |
Correct |
7 ms |
6100 KB |
Output is correct |
9 |
Correct |
5 ms |
6100 KB |
Output is correct |
10 |
Correct |
6 ms |
6088 KB |
Output is correct |
11 |
Correct |
6 ms |
6100 KB |
Output is correct |
12 |
Correct |
5 ms |
6104 KB |
Output is correct |
13 |
Correct |
5 ms |
6088 KB |
Output is correct |
14 |
Correct |
3 ms |
5972 KB |
Output is correct |
15 |
Correct |
3 ms |
5972 KB |
Output is correct |
16 |
Correct |
6 ms |
6212 KB |
Output is correct |
17 |
Correct |
6 ms |
6100 KB |
Output is correct |
18 |
Correct |
5 ms |
6100 KB |
Output is correct |
19 |
Correct |
3 ms |
5972 KB |
Output is correct |
20 |
Correct |
6 ms |
6100 KB |
Output is correct |
21 |
Correct |
6 ms |
6092 KB |
Output is correct |
22 |
Correct |
6 ms |
6164 KB |
Output is correct |
23 |
Correct |
4 ms |
5972 KB |
Output is correct |
24 |
Correct |
5 ms |
5972 KB |
Output is correct |
25 |
Correct |
6 ms |
6100 KB |
Output is correct |
26 |
Correct |
4 ms |
6100 KB |
Output is correct |
27 |
Correct |
6 ms |
6096 KB |
Output is correct |
28 |
Correct |
3 ms |
5972 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
3 ms |
5972 KB |
Output is correct |
2 |
Correct |
3 ms |
5972 KB |
Output is correct |
3 |
Correct |
6 ms |
6056 KB |
Output is correct |
4 |
Correct |
5 ms |
6100 KB |
Output is correct |
5 |
Correct |
6 ms |
6092 KB |
Output is correct |
6 |
Correct |
6 ms |
6200 KB |
Output is correct |
7 |
Correct |
6 ms |
6100 KB |
Output is correct |
8 |
Correct |
7 ms |
6100 KB |
Output is correct |
9 |
Correct |
5 ms |
6100 KB |
Output is correct |
10 |
Correct |
6 ms |
6088 KB |
Output is correct |
11 |
Correct |
6 ms |
6100 KB |
Output is correct |
12 |
Correct |
5 ms |
6104 KB |
Output is correct |
13 |
Correct |
5 ms |
6088 KB |
Output is correct |
14 |
Correct |
3 ms |
5972 KB |
Output is correct |
15 |
Correct |
3 ms |
5972 KB |
Output is correct |
16 |
Correct |
6 ms |
6212 KB |
Output is correct |
17 |
Correct |
6 ms |
6100 KB |
Output is correct |
18 |
Correct |
5 ms |
6100 KB |
Output is correct |
19 |
Correct |
3 ms |
5972 KB |
Output is correct |
20 |
Correct |
6 ms |
6100 KB |
Output is correct |
21 |
Correct |
6 ms |
6092 KB |
Output is correct |
22 |
Correct |
6 ms |
6164 KB |
Output is correct |
23 |
Correct |
4 ms |
5972 KB |
Output is correct |
24 |
Correct |
5 ms |
5972 KB |
Output is correct |
25 |
Correct |
6 ms |
6100 KB |
Output is correct |
26 |
Correct |
4 ms |
6100 KB |
Output is correct |
27 |
Correct |
6 ms |
6096 KB |
Output is correct |
28 |
Correct |
3 ms |
5972 KB |
Output is correct |
29 |
Correct |
9 ms |
6228 KB |
Output is correct |
30 |
Correct |
7 ms |
6100 KB |
Output is correct |
31 |
Correct |
7 ms |
6100 KB |
Output is correct |
32 |
Correct |
7 ms |
6096 KB |
Output is correct |
33 |
Correct |
6 ms |
6092 KB |
Output is correct |
34 |
Correct |
8 ms |
6100 KB |
Output is correct |
35 |
Correct |
8 ms |
6212 KB |
Output is correct |
36 |
Correct |
7 ms |
6100 KB |
Output is correct |
37 |
Correct |
6 ms |
6092 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
3 ms |
5972 KB |
Output is correct |
2 |
Correct |
3 ms |
5972 KB |
Output is correct |
3 |
Correct |
6 ms |
6056 KB |
Output is correct |
4 |
Correct |
5 ms |
6100 KB |
Output is correct |
5 |
Correct |
6 ms |
6092 KB |
Output is correct |
6 |
Correct |
6 ms |
6200 KB |
Output is correct |
7 |
Correct |
6 ms |
6100 KB |
Output is correct |
8 |
Correct |
7 ms |
6100 KB |
Output is correct |
9 |
Correct |
5 ms |
6100 KB |
Output is correct |
10 |
Correct |
6 ms |
6088 KB |
Output is correct |
11 |
Correct |
6 ms |
6100 KB |
Output is correct |
12 |
Correct |
5 ms |
6104 KB |
Output is correct |
13 |
Correct |
5 ms |
6088 KB |
Output is correct |
14 |
Correct |
3 ms |
5972 KB |
Output is correct |
15 |
Correct |
3 ms |
5972 KB |
Output is correct |
16 |
Correct |
6 ms |
6212 KB |
Output is correct |
17 |
Correct |
6 ms |
6100 KB |
Output is correct |
18 |
Correct |
5 ms |
6100 KB |
Output is correct |
19 |
Correct |
3 ms |
5972 KB |
Output is correct |
20 |
Correct |
6 ms |
6100 KB |
Output is correct |
21 |
Correct |
6 ms |
6092 KB |
Output is correct |
22 |
Correct |
6 ms |
6164 KB |
Output is correct |
23 |
Correct |
4 ms |
5972 KB |
Output is correct |
24 |
Correct |
5 ms |
5972 KB |
Output is correct |
25 |
Correct |
6 ms |
6100 KB |
Output is correct |
26 |
Correct |
4 ms |
6100 KB |
Output is correct |
27 |
Correct |
6 ms |
6096 KB |
Output is correct |
28 |
Correct |
3 ms |
5972 KB |
Output is correct |
29 |
Correct |
9 ms |
6228 KB |
Output is correct |
30 |
Correct |
7 ms |
6100 KB |
Output is correct |
31 |
Correct |
7 ms |
6100 KB |
Output is correct |
32 |
Correct |
7 ms |
6096 KB |
Output is correct |
33 |
Correct |
6 ms |
6092 KB |
Output is correct |
34 |
Correct |
8 ms |
6100 KB |
Output is correct |
35 |
Correct |
8 ms |
6212 KB |
Output is correct |
36 |
Correct |
7 ms |
6100 KB |
Output is correct |
37 |
Correct |
6 ms |
6092 KB |
Output is correct |
38 |
Correct |
186 ms |
10572 KB |
Output is correct |
39 |
Correct |
680 ms |
33644 KB |
Output is correct |
40 |
Correct |
122 ms |
9420 KB |
Output is correct |
41 |
Correct |
84 ms |
8168 KB |
Output is correct |
42 |
Correct |
110 ms |
9484 KB |
Output is correct |
43 |
Correct |
63 ms |
8140 KB |
Output is correct |
44 |
Correct |
23 ms |
6612 KB |
Output is correct |
45 |
Correct |
123 ms |
22648 KB |
Output is correct |
46 |
Correct |
118 ms |
22664 KB |
Output is correct |
47 |
Correct |
188 ms |
28008 KB |
Output is correct |
48 |
Correct |
176 ms |
27876 KB |
Output is correct |
49 |
Correct |
108 ms |
22800 KB |
Output is correct |
50 |
Correct |
105 ms |
22872 KB |
Output is correct |
51 |
Correct |
97 ms |
24192 KB |
Output is correct |
52 |
Correct |
104 ms |
24164 KB |
Output is correct |
53 |
Correct |
26 ms |
7364 KB |
Output is correct |
54 |
Correct |
146 ms |
22520 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
3 ms |
5972 KB |
Output is correct |
2 |
Correct |
3 ms |
5972 KB |
Output is correct |
3 |
Correct |
3 ms |
5972 KB |
Output is correct |
4 |
Correct |
3 ms |
5972 KB |
Output is correct |
5 |
Correct |
17 ms |
6076 KB |
Output is correct |
6 |
Correct |
5 ms |
6208 KB |
Output is correct |
7 |
Correct |
4 ms |
6096 KB |
Output is correct |
8 |
Correct |
3 ms |
6076 KB |
Output is correct |
9 |
Correct |
3 ms |
5972 KB |
Output is correct |
10 |
Correct |
4 ms |
5972 KB |
Output is correct |
11 |
Correct |
4 ms |
6168 KB |
Output is correct |
12 |
Correct |
6 ms |
6100 KB |
Output is correct |
13 |
Correct |
47 ms |
6600 KB |
Output is correct |
14 |
Correct |
71 ms |
6848 KB |
Output is correct |
15 |
Correct |
66 ms |
6736 KB |
Output is correct |
16 |
Correct |
153 ms |
23208 KB |
Output is correct |
17 |
Correct |
333 ms |
29180 KB |
Output is correct |
18 |
Correct |
705 ms |
49256 KB |
Output is correct |
19 |
Correct |
175 ms |
23288 KB |
Output is correct |
20 |
Correct |
186 ms |
23392 KB |
Output is correct |
21 |
Correct |
177 ms |
23372 KB |
Output is correct |
22 |
Correct |
253 ms |
29792 KB |
Output is correct |
23 |
Correct |
264 ms |
30272 KB |
Output is correct |
24 |
Correct |
250 ms |
29976 KB |
Output is correct |
25 |
Correct |
292 ms |
30132 KB |
Output is correct |
26 |
Correct |
275 ms |
30348 KB |
Output is correct |
27 |
Correct |
439 ms |
33412 KB |
Output is correct |
28 |
Correct |
473 ms |
34896 KB |
Output is correct |
29 |
Correct |
455 ms |
31984 KB |
Output is correct |
30 |
Correct |
298 ms |
28144 KB |
Output is correct |
31 |
Correct |
307 ms |
28692 KB |
Output is correct |
32 |
Correct |
296 ms |
28040 KB |
Output is correct |
33 |
Correct |
302 ms |
28640 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
3 ms |
5972 KB |
Output is correct |
2 |
Correct |
3 ms |
5972 KB |
Output is correct |
3 |
Correct |
4 ms |
5972 KB |
Output is correct |
4 |
Correct |
26 ms |
6100 KB |
Output is correct |
5 |
Correct |
53 ms |
6828 KB |
Output is correct |
6 |
Correct |
6 ms |
6100 KB |
Output is correct |
7 |
Correct |
6 ms |
6092 KB |
Output is correct |
8 |
Correct |
9 ms |
6220 KB |
Output is correct |
9 |
Correct |
185 ms |
10548 KB |
Output is correct |
10 |
Correct |
704 ms |
33808 KB |
Output is correct |
11 |
Correct |
17 ms |
6228 KB |
Output is correct |
12 |
Correct |
90 ms |
7136 KB |
Output is correct |
13 |
Correct |
256 ms |
62640 KB |
Output is correct |
14 |
Correct |
291 ms |
77292 KB |
Output is correct |
15 |
Execution timed out |
5090 ms |
832984 KB |
Time limit exceeded |
16 |
Halted |
0 ms |
0 KB |
- |