#include <bits/stdc++.h>
using namespace std;
typedef pair<int,int> ii;
int q;
int n,m;
int s[120005], t[120005];
int p[120005][18];
int d[120005];
vector<int> G2[120005];
vector<int> G[120005];
int vis[120005];
vector<ii> K;
int lca(int x, int y){
///printf("LCA %d %d\n",x,y);
if (d[x] > d[y]) swap(x,y);
int diff = d[y]-d[x];
for (int k = 0; k < 18; k++){
if (diff>>k&1) {
y = p[y][k];
}
}
if (x == y) return x;
for (int k = 17; k >= 0; k--){
if (p[x][k] && p[y][k] && p[x][k] != p[y][k]){
x = p[x][k];
y = p[y][k];
}
}
assert(p[x][0] == p[y][0]);
return p[x][0];
}
void dfs(int u, int p_){
p[u][0] = p_;
for (auto v: G[u]){
if (v == p_) continue;
d[v] = d[u]+1;
dfs(v,u);
}
}
bool check(int x, int y, int z){
int r = lca(x,y);
return (lca(r,z) == r) && (lca(x,z) == z || lca(y,z) == z);
}
bool dfs2(int u){
vis[u] = 2;
bool can = true;
for (auto v : G2[u]){
if (vis[v] == 2) return false;
else if (vis[v] == 0){
can &= dfs2(v);
};
}
vis[u] = 1;
return can;
}
int main(){
scanf("%d",&q);
while (q--){
scanf("%d",&n);
for (int i = 1; i <= n; i++) G[i].clear();
for (int i = 1,a,b; i < n; i++){
scanf("%d%d",&a,&b);
G[a].push_back(b);
G[b].push_back(a);
}
scanf("%d",&m);
for (int i = 0; i < m; i++){
scanf("%d%d",&s[i],&t[i]);
}
if (m <= 500 && q <= 20){
d[1] = 0;
dfs(1,0);
for (int k = 1; k < 18; k++){
for (int i = 1; i <= n; i++){
p[i][k] = p[p[i][k-1]][k-1];
}
}
for (int i = 0; i < m; i++) G2[i].clear();
for (int i = 0; i < m; i++){
for (int j = 0; j < m; j++){
if (i == j) continue;
if (check(s[i], t[i], s[j])){
///printf("%d before %d\n",j,i);
G2[i].push_back(j);
}
if (check(s[i], t[i], t[j])){
///printf("%d before %d\n",i,j);
G2[j].push_back(i);
}
}
}
bool can = true;
fill(vis,vis+m, 0);
for (int i = 0; i < m; i++){
if (vis[i] == 0){
can &= dfs2(i);
}
}
printf("%s\n",can?"Yes":"No");
}
else{
K.clear();
for (int i = 0; i < m; i++){
K.push_back({s[i],t[i]});
}
sort(K.begin(),K.end());
bool can = true;
for (int i = 1; i < m; i++){
if (K[i].second < K[i-1].second) can = false;
}
printf("%s\n",can?"Yes":"No");
}
}
}
Compilation message
jail.cpp: In function 'int main()':
jail.cpp:58:10: warning: ignoring return value of 'int scanf(const char*, ...)' declared with attribute 'warn_unused_result' [-Wunused-result]
58 | scanf("%d",&q);
| ~~~~~^~~~~~~~~
jail.cpp:60:14: warning: ignoring return value of 'int scanf(const char*, ...)' declared with attribute 'warn_unused_result' [-Wunused-result]
60 | scanf("%d",&n);
| ~~~~~^~~~~~~~~
jail.cpp:63:18: warning: ignoring return value of 'int scanf(const char*, ...)' declared with attribute 'warn_unused_result' [-Wunused-result]
63 | scanf("%d%d",&a,&b);
| ~~~~~^~~~~~~~~~~~~~
jail.cpp:67:14: warning: ignoring return value of 'int scanf(const char*, ...)' declared with attribute 'warn_unused_result' [-Wunused-result]
67 | scanf("%d",&m);
| ~~~~~^~~~~~~~~
jail.cpp:69:18: warning: ignoring return value of 'int scanf(const char*, ...)' declared with attribute 'warn_unused_result' [-Wunused-result]
69 | scanf("%d%d",&s[i],&t[i]);
| ~~~~~^~~~~~~~~~~~~~~~~~~~
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
3 ms |
5844 KB |
Output is correct |
2 |
Correct |
3 ms |
5972 KB |
Output is correct |
3 |
Correct |
3 ms |
5972 KB |
Output is correct |
4 |
Correct |
12 ms |
5968 KB |
Output is correct |
5 |
Correct |
23 ms |
5964 KB |
Output is correct |
6 |
Correct |
4 ms |
5972 KB |
Output is correct |
7 |
Correct |
4 ms |
6008 KB |
Output is correct |
8 |
Correct |
54 ms |
6008 KB |
Output is correct |
9 |
Correct |
1658 ms |
8884 KB |
Output is correct |
10 |
Correct |
136 ms |
27412 KB |
Output is correct |
11 |
Correct |
8 ms |
5972 KB |
Output is correct |
12 |
Correct |
62 ms |
6232 KB |
Output is correct |
13 |
Correct |
49 ms |
11132 KB |
Output is correct |
14 |
Correct |
50 ms |
11008 KB |
Output is correct |
15 |
Correct |
51 ms |
10944 KB |
Output is correct |
16 |
Correct |
68 ms |
11840 KB |
Output is correct |
17 |
Correct |
49 ms |
10952 KB |
Output is correct |
18 |
Correct |
61 ms |
11880 KB |
Output is correct |
19 |
Correct |
50 ms |
10904 KB |
Output is correct |
20 |
Correct |
56 ms |
10952 KB |
Output is correct |
21 |
Correct |
51 ms |
10956 KB |
Output is correct |
22 |
Correct |
53 ms |
10936 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
4 ms |
5972 KB |
Output is correct |
2 |
Correct |
3 ms |
5880 KB |
Output is correct |
3 |
Correct |
4 ms |
5972 KB |
Output is correct |
4 |
Correct |
4 ms |
5972 KB |
Output is correct |
5 |
Correct |
4 ms |
5972 KB |
Output is correct |
6 |
Correct |
4 ms |
5972 KB |
Output is correct |
7 |
Correct |
5 ms |
5956 KB |
Output is correct |
8 |
Correct |
4 ms |
5972 KB |
Output is correct |
9 |
Correct |
4 ms |
5972 KB |
Output is correct |
10 |
Correct |
4 ms |
5972 KB |
Output is correct |
11 |
Correct |
4 ms |
5972 KB |
Output is correct |
12 |
Correct |
3 ms |
5972 KB |
Output is correct |
13 |
Correct |
3 ms |
5972 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
4 ms |
5972 KB |
Output is correct |
2 |
Correct |
3 ms |
5880 KB |
Output is correct |
3 |
Correct |
4 ms |
5972 KB |
Output is correct |
4 |
Correct |
4 ms |
5972 KB |
Output is correct |
5 |
Correct |
4 ms |
5972 KB |
Output is correct |
6 |
Correct |
4 ms |
5972 KB |
Output is correct |
7 |
Correct |
5 ms |
5956 KB |
Output is correct |
8 |
Correct |
4 ms |
5972 KB |
Output is correct |
9 |
Correct |
4 ms |
5972 KB |
Output is correct |
10 |
Correct |
4 ms |
5972 KB |
Output is correct |
11 |
Correct |
4 ms |
5972 KB |
Output is correct |
12 |
Correct |
3 ms |
5972 KB |
Output is correct |
13 |
Correct |
3 ms |
5972 KB |
Output is correct |
14 |
Correct |
3 ms |
5972 KB |
Output is correct |
15 |
Correct |
3 ms |
5844 KB |
Output is correct |
16 |
Correct |
4 ms |
5984 KB |
Output is correct |
17 |
Correct |
4 ms |
6004 KB |
Output is correct |
18 |
Correct |
4 ms |
5972 KB |
Output is correct |
19 |
Correct |
3 ms |
5844 KB |
Output is correct |
20 |
Correct |
5 ms |
5956 KB |
Output is correct |
21 |
Correct |
4 ms |
5972 KB |
Output is correct |
22 |
Correct |
4 ms |
5972 KB |
Output is correct |
23 |
Correct |
4 ms |
5844 KB |
Output is correct |
24 |
Correct |
4 ms |
5972 KB |
Output is correct |
25 |
Correct |
5 ms |
5904 KB |
Output is correct |
26 |
Correct |
5 ms |
5972 KB |
Output is correct |
27 |
Correct |
4 ms |
5972 KB |
Output is correct |
28 |
Correct |
4 ms |
5844 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
4 ms |
5972 KB |
Output is correct |
2 |
Correct |
3 ms |
5880 KB |
Output is correct |
3 |
Correct |
4 ms |
5972 KB |
Output is correct |
4 |
Correct |
4 ms |
5972 KB |
Output is correct |
5 |
Correct |
4 ms |
5972 KB |
Output is correct |
6 |
Correct |
4 ms |
5972 KB |
Output is correct |
7 |
Correct |
5 ms |
5956 KB |
Output is correct |
8 |
Correct |
4 ms |
5972 KB |
Output is correct |
9 |
Correct |
4 ms |
5972 KB |
Output is correct |
10 |
Correct |
4 ms |
5972 KB |
Output is correct |
11 |
Correct |
4 ms |
5972 KB |
Output is correct |
12 |
Correct |
3 ms |
5972 KB |
Output is correct |
13 |
Correct |
3 ms |
5972 KB |
Output is correct |
14 |
Correct |
3 ms |
5972 KB |
Output is correct |
15 |
Correct |
3 ms |
5844 KB |
Output is correct |
16 |
Correct |
4 ms |
5984 KB |
Output is correct |
17 |
Correct |
4 ms |
6004 KB |
Output is correct |
18 |
Correct |
4 ms |
5972 KB |
Output is correct |
19 |
Correct |
3 ms |
5844 KB |
Output is correct |
20 |
Correct |
5 ms |
5956 KB |
Output is correct |
21 |
Correct |
4 ms |
5972 KB |
Output is correct |
22 |
Correct |
4 ms |
5972 KB |
Output is correct |
23 |
Correct |
4 ms |
5844 KB |
Output is correct |
24 |
Correct |
4 ms |
5972 KB |
Output is correct |
25 |
Correct |
5 ms |
5904 KB |
Output is correct |
26 |
Correct |
5 ms |
5972 KB |
Output is correct |
27 |
Correct |
4 ms |
5972 KB |
Output is correct |
28 |
Correct |
4 ms |
5844 KB |
Output is correct |
29 |
Correct |
53 ms |
6040 KB |
Output is correct |
30 |
Correct |
36 ms |
5972 KB |
Output is correct |
31 |
Correct |
35 ms |
6152 KB |
Output is correct |
32 |
Correct |
22 ms |
5996 KB |
Output is correct |
33 |
Correct |
7 ms |
5972 KB |
Output is correct |
34 |
Correct |
81 ms |
6004 KB |
Output is correct |
35 |
Correct |
93 ms |
5972 KB |
Output is correct |
36 |
Correct |
57 ms |
5988 KB |
Output is correct |
37 |
Correct |
34 ms |
6004 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
4 ms |
5972 KB |
Output is correct |
2 |
Correct |
3 ms |
5880 KB |
Output is correct |
3 |
Correct |
4 ms |
5972 KB |
Output is correct |
4 |
Correct |
4 ms |
5972 KB |
Output is correct |
5 |
Correct |
4 ms |
5972 KB |
Output is correct |
6 |
Correct |
4 ms |
5972 KB |
Output is correct |
7 |
Correct |
5 ms |
5956 KB |
Output is correct |
8 |
Correct |
4 ms |
5972 KB |
Output is correct |
9 |
Correct |
4 ms |
5972 KB |
Output is correct |
10 |
Correct |
4 ms |
5972 KB |
Output is correct |
11 |
Correct |
4 ms |
5972 KB |
Output is correct |
12 |
Correct |
3 ms |
5972 KB |
Output is correct |
13 |
Correct |
3 ms |
5972 KB |
Output is correct |
14 |
Correct |
3 ms |
5972 KB |
Output is correct |
15 |
Correct |
3 ms |
5844 KB |
Output is correct |
16 |
Correct |
4 ms |
5984 KB |
Output is correct |
17 |
Correct |
4 ms |
6004 KB |
Output is correct |
18 |
Correct |
4 ms |
5972 KB |
Output is correct |
19 |
Correct |
3 ms |
5844 KB |
Output is correct |
20 |
Correct |
5 ms |
5956 KB |
Output is correct |
21 |
Correct |
4 ms |
5972 KB |
Output is correct |
22 |
Correct |
4 ms |
5972 KB |
Output is correct |
23 |
Correct |
4 ms |
5844 KB |
Output is correct |
24 |
Correct |
4 ms |
5972 KB |
Output is correct |
25 |
Correct |
5 ms |
5904 KB |
Output is correct |
26 |
Correct |
5 ms |
5972 KB |
Output is correct |
27 |
Correct |
4 ms |
5972 KB |
Output is correct |
28 |
Correct |
4 ms |
5844 KB |
Output is correct |
29 |
Correct |
53 ms |
6040 KB |
Output is correct |
30 |
Correct |
36 ms |
5972 KB |
Output is correct |
31 |
Correct |
35 ms |
6152 KB |
Output is correct |
32 |
Correct |
22 ms |
5996 KB |
Output is correct |
33 |
Correct |
7 ms |
5972 KB |
Output is correct |
34 |
Correct |
81 ms |
6004 KB |
Output is correct |
35 |
Correct |
93 ms |
5972 KB |
Output is correct |
36 |
Correct |
57 ms |
5988 KB |
Output is correct |
37 |
Correct |
34 ms |
6004 KB |
Output is correct |
38 |
Correct |
1634 ms |
9000 KB |
Output is correct |
39 |
Correct |
138 ms |
27360 KB |
Output is correct |
40 |
Correct |
2117 ms |
8160 KB |
Output is correct |
41 |
Correct |
1910 ms |
6752 KB |
Output is correct |
42 |
Correct |
1677 ms |
8152 KB |
Output is correct |
43 |
Correct |
42 ms |
6740 KB |
Output is correct |
44 |
Correct |
1821 ms |
6416 KB |
Output is correct |
45 |
Correct |
227 ms |
18792 KB |
Output is correct |
46 |
Correct |
226 ms |
18664 KB |
Output is correct |
47 |
Correct |
252 ms |
22372 KB |
Output is correct |
48 |
Correct |
256 ms |
22508 KB |
Output is correct |
49 |
Correct |
235 ms |
18776 KB |
Output is correct |
50 |
Correct |
249 ms |
18708 KB |
Output is correct |
51 |
Correct |
72 ms |
19888 KB |
Output is correct |
52 |
Correct |
77 ms |
19796 KB |
Output is correct |
53 |
Correct |
1277 ms |
6912 KB |
Output is correct |
54 |
Correct |
174 ms |
18620 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
3 ms |
5844 KB |
Output is correct |
2 |
Correct |
3 ms |
5968 KB |
Output is correct |
3 |
Correct |
3 ms |
5972 KB |
Output is correct |
4 |
Correct |
3 ms |
5844 KB |
Output is correct |
5 |
Correct |
9 ms |
5972 KB |
Output is correct |
6 |
Correct |
6 ms |
5972 KB |
Output is correct |
7 |
Correct |
5 ms |
5972 KB |
Output is correct |
8 |
Correct |
3 ms |
5952 KB |
Output is correct |
9 |
Correct |
5 ms |
5844 KB |
Output is correct |
10 |
Correct |
4 ms |
5872 KB |
Output is correct |
11 |
Correct |
5 ms |
5972 KB |
Output is correct |
12 |
Correct |
61 ms |
5972 KB |
Output is correct |
13 |
Incorrect |
128 ms |
6160 KB |
Output isn't correct |
14 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
3 ms |
5844 KB |
Output is correct |
2 |
Correct |
3 ms |
5972 KB |
Output is correct |
3 |
Correct |
3 ms |
5972 KB |
Output is correct |
4 |
Correct |
12 ms |
5968 KB |
Output is correct |
5 |
Correct |
23 ms |
5964 KB |
Output is correct |
6 |
Correct |
4 ms |
5972 KB |
Output is correct |
7 |
Correct |
4 ms |
6008 KB |
Output is correct |
8 |
Correct |
54 ms |
6008 KB |
Output is correct |
9 |
Correct |
1658 ms |
8884 KB |
Output is correct |
10 |
Correct |
136 ms |
27412 KB |
Output is correct |
11 |
Correct |
8 ms |
5972 KB |
Output is correct |
12 |
Correct |
62 ms |
6232 KB |
Output is correct |
13 |
Correct |
49 ms |
11132 KB |
Output is correct |
14 |
Correct |
50 ms |
11008 KB |
Output is correct |
15 |
Correct |
51 ms |
10944 KB |
Output is correct |
16 |
Correct |
68 ms |
11840 KB |
Output is correct |
17 |
Correct |
49 ms |
10952 KB |
Output is correct |
18 |
Correct |
61 ms |
11880 KB |
Output is correct |
19 |
Correct |
50 ms |
10904 KB |
Output is correct |
20 |
Correct |
56 ms |
10952 KB |
Output is correct |
21 |
Correct |
51 ms |
10956 KB |
Output is correct |
22 |
Correct |
53 ms |
10936 KB |
Output is correct |
23 |
Correct |
4 ms |
5972 KB |
Output is correct |
24 |
Correct |
3 ms |
5880 KB |
Output is correct |
25 |
Correct |
4 ms |
5972 KB |
Output is correct |
26 |
Correct |
4 ms |
5972 KB |
Output is correct |
27 |
Correct |
4 ms |
5972 KB |
Output is correct |
28 |
Correct |
4 ms |
5972 KB |
Output is correct |
29 |
Correct |
5 ms |
5956 KB |
Output is correct |
30 |
Correct |
4 ms |
5972 KB |
Output is correct |
31 |
Correct |
4 ms |
5972 KB |
Output is correct |
32 |
Correct |
4 ms |
5972 KB |
Output is correct |
33 |
Correct |
4 ms |
5972 KB |
Output is correct |
34 |
Correct |
3 ms |
5972 KB |
Output is correct |
35 |
Correct |
3 ms |
5972 KB |
Output is correct |
36 |
Correct |
3 ms |
5972 KB |
Output is correct |
37 |
Correct |
3 ms |
5844 KB |
Output is correct |
38 |
Correct |
4 ms |
5984 KB |
Output is correct |
39 |
Correct |
4 ms |
6004 KB |
Output is correct |
40 |
Correct |
4 ms |
5972 KB |
Output is correct |
41 |
Correct |
3 ms |
5844 KB |
Output is correct |
42 |
Correct |
5 ms |
5956 KB |
Output is correct |
43 |
Correct |
4 ms |
5972 KB |
Output is correct |
44 |
Correct |
4 ms |
5972 KB |
Output is correct |
45 |
Correct |
4 ms |
5844 KB |
Output is correct |
46 |
Correct |
4 ms |
5972 KB |
Output is correct |
47 |
Correct |
5 ms |
5904 KB |
Output is correct |
48 |
Correct |
5 ms |
5972 KB |
Output is correct |
49 |
Correct |
4 ms |
5972 KB |
Output is correct |
50 |
Correct |
4 ms |
5844 KB |
Output is correct |
51 |
Correct |
53 ms |
6040 KB |
Output is correct |
52 |
Correct |
36 ms |
5972 KB |
Output is correct |
53 |
Correct |
35 ms |
6152 KB |
Output is correct |
54 |
Correct |
22 ms |
5996 KB |
Output is correct |
55 |
Correct |
7 ms |
5972 KB |
Output is correct |
56 |
Correct |
81 ms |
6004 KB |
Output is correct |
57 |
Correct |
93 ms |
5972 KB |
Output is correct |
58 |
Correct |
57 ms |
5988 KB |
Output is correct |
59 |
Correct |
34 ms |
6004 KB |
Output is correct |
60 |
Correct |
1634 ms |
9000 KB |
Output is correct |
61 |
Correct |
138 ms |
27360 KB |
Output is correct |
62 |
Correct |
2117 ms |
8160 KB |
Output is correct |
63 |
Correct |
1910 ms |
6752 KB |
Output is correct |
64 |
Correct |
1677 ms |
8152 KB |
Output is correct |
65 |
Correct |
42 ms |
6740 KB |
Output is correct |
66 |
Correct |
1821 ms |
6416 KB |
Output is correct |
67 |
Correct |
227 ms |
18792 KB |
Output is correct |
68 |
Correct |
226 ms |
18664 KB |
Output is correct |
69 |
Correct |
252 ms |
22372 KB |
Output is correct |
70 |
Correct |
256 ms |
22508 KB |
Output is correct |
71 |
Correct |
235 ms |
18776 KB |
Output is correct |
72 |
Correct |
249 ms |
18708 KB |
Output is correct |
73 |
Correct |
72 ms |
19888 KB |
Output is correct |
74 |
Correct |
77 ms |
19796 KB |
Output is correct |
75 |
Correct |
1277 ms |
6912 KB |
Output is correct |
76 |
Correct |
174 ms |
18620 KB |
Output is correct |
77 |
Correct |
3 ms |
5844 KB |
Output is correct |
78 |
Correct |
3 ms |
5968 KB |
Output is correct |
79 |
Correct |
3 ms |
5972 KB |
Output is correct |
80 |
Correct |
3 ms |
5844 KB |
Output is correct |
81 |
Correct |
9 ms |
5972 KB |
Output is correct |
82 |
Correct |
6 ms |
5972 KB |
Output is correct |
83 |
Correct |
5 ms |
5972 KB |
Output is correct |
84 |
Correct |
3 ms |
5952 KB |
Output is correct |
85 |
Correct |
5 ms |
5844 KB |
Output is correct |
86 |
Correct |
4 ms |
5872 KB |
Output is correct |
87 |
Correct |
5 ms |
5972 KB |
Output is correct |
88 |
Correct |
61 ms |
5972 KB |
Output is correct |
89 |
Incorrect |
128 ms |
6160 KB |
Output isn't correct |
90 |
Halted |
0 ms |
0 KB |
- |