#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 < n; i++){
K.push_back({s[i],t[i]});
}
sort(K.begin(),K.end());
bool can = true;
for (int i = 1; i < n; 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 |
4 ms |
5972 KB |
Output is correct |
3 |
Correct |
3 ms |
5972 KB |
Output is correct |
4 |
Correct |
13 ms |
6100 KB |
Output is correct |
5 |
Correct |
26 ms |
6228 KB |
Output is correct |
6 |
Correct |
6 ms |
5972 KB |
Output is correct |
7 |
Correct |
6 ms |
5924 KB |
Output is correct |
8 |
Correct |
62 ms |
6152 KB |
Output is correct |
9 |
Correct |
1800 ms |
9144 KB |
Output is correct |
10 |
Correct |
147 ms |
27716 KB |
Output is correct |
11 |
Incorrect |
10 ms |
6068 KB |
Output isn't correct |
12 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
4 ms |
5880 KB |
Output is correct |
2 |
Correct |
4 ms |
5948 KB |
Output is correct |
3 |
Correct |
5 ms |
5972 KB |
Output is correct |
4 |
Correct |
5 ms |
5972 KB |
Output is correct |
5 |
Correct |
5 ms |
5960 KB |
Output is correct |
6 |
Correct |
4 ms |
5964 KB |
Output is correct |
7 |
Correct |
4 ms |
5972 KB |
Output is correct |
8 |
Correct |
5 ms |
5972 KB |
Output is correct |
9 |
Correct |
5 ms |
6024 KB |
Output is correct |
10 |
Correct |
4 ms |
5972 KB |
Output is correct |
11 |
Correct |
5 ms |
5964 KB |
Output is correct |
12 |
Correct |
4 ms |
5972 KB |
Output is correct |
13 |
Correct |
6 ms |
5972 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
4 ms |
5880 KB |
Output is correct |
2 |
Correct |
4 ms |
5948 KB |
Output is correct |
3 |
Correct |
5 ms |
5972 KB |
Output is correct |
4 |
Correct |
5 ms |
5972 KB |
Output is correct |
5 |
Correct |
5 ms |
5960 KB |
Output is correct |
6 |
Correct |
4 ms |
5964 KB |
Output is correct |
7 |
Correct |
4 ms |
5972 KB |
Output is correct |
8 |
Correct |
5 ms |
5972 KB |
Output is correct |
9 |
Correct |
5 ms |
6024 KB |
Output is correct |
10 |
Correct |
4 ms |
5972 KB |
Output is correct |
11 |
Correct |
5 ms |
5964 KB |
Output is correct |
12 |
Correct |
4 ms |
5972 KB |
Output is correct |
13 |
Correct |
6 ms |
5972 KB |
Output is correct |
14 |
Correct |
4 ms |
5972 KB |
Output is correct |
15 |
Correct |
4 ms |
5972 KB |
Output is correct |
16 |
Correct |
5 ms |
5972 KB |
Output is correct |
17 |
Correct |
5 ms |
5972 KB |
Output is correct |
18 |
Correct |
5 ms |
5972 KB |
Output is correct |
19 |
Correct |
4 ms |
5952 KB |
Output is correct |
20 |
Correct |
4 ms |
5972 KB |
Output is correct |
21 |
Correct |
5 ms |
6016 KB |
Output is correct |
22 |
Correct |
5 ms |
5972 KB |
Output is correct |
23 |
Correct |
4 ms |
5948 KB |
Output is correct |
24 |
Correct |
4 ms |
5948 KB |
Output is correct |
25 |
Correct |
5 ms |
5972 KB |
Output is correct |
26 |
Correct |
4 ms |
5948 KB |
Output is correct |
27 |
Correct |
5 ms |
6008 KB |
Output is correct |
28 |
Correct |
4 ms |
5844 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
4 ms |
5880 KB |
Output is correct |
2 |
Correct |
4 ms |
5948 KB |
Output is correct |
3 |
Correct |
5 ms |
5972 KB |
Output is correct |
4 |
Correct |
5 ms |
5972 KB |
Output is correct |
5 |
Correct |
5 ms |
5960 KB |
Output is correct |
6 |
Correct |
4 ms |
5964 KB |
Output is correct |
7 |
Correct |
4 ms |
5972 KB |
Output is correct |
8 |
Correct |
5 ms |
5972 KB |
Output is correct |
9 |
Correct |
5 ms |
6024 KB |
Output is correct |
10 |
Correct |
4 ms |
5972 KB |
Output is correct |
11 |
Correct |
5 ms |
5964 KB |
Output is correct |
12 |
Correct |
4 ms |
5972 KB |
Output is correct |
13 |
Correct |
6 ms |
5972 KB |
Output is correct |
14 |
Correct |
4 ms |
5972 KB |
Output is correct |
15 |
Correct |
4 ms |
5972 KB |
Output is correct |
16 |
Correct |
5 ms |
5972 KB |
Output is correct |
17 |
Correct |
5 ms |
5972 KB |
Output is correct |
18 |
Correct |
5 ms |
5972 KB |
Output is correct |
19 |
Correct |
4 ms |
5952 KB |
Output is correct |
20 |
Correct |
4 ms |
5972 KB |
Output is correct |
21 |
Correct |
5 ms |
6016 KB |
Output is correct |
22 |
Correct |
5 ms |
5972 KB |
Output is correct |
23 |
Correct |
4 ms |
5948 KB |
Output is correct |
24 |
Correct |
4 ms |
5948 KB |
Output is correct |
25 |
Correct |
5 ms |
5972 KB |
Output is correct |
26 |
Correct |
4 ms |
5948 KB |
Output is correct |
27 |
Correct |
5 ms |
6008 KB |
Output is correct |
28 |
Correct |
4 ms |
5844 KB |
Output is correct |
29 |
Correct |
60 ms |
6056 KB |
Output is correct |
30 |
Correct |
40 ms |
6020 KB |
Output is correct |
31 |
Correct |
35 ms |
6044 KB |
Output is correct |
32 |
Correct |
21 ms |
5972 KB |
Output is correct |
33 |
Correct |
7 ms |
5988 KB |
Output is correct |
34 |
Correct |
84 ms |
5972 KB |
Output is correct |
35 |
Correct |
100 ms |
6092 KB |
Output is correct |
36 |
Correct |
63 ms |
5972 KB |
Output is correct |
37 |
Correct |
37 ms |
6020 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
4 ms |
5880 KB |
Output is correct |
2 |
Correct |
4 ms |
5948 KB |
Output is correct |
3 |
Correct |
5 ms |
5972 KB |
Output is correct |
4 |
Correct |
5 ms |
5972 KB |
Output is correct |
5 |
Correct |
5 ms |
5960 KB |
Output is correct |
6 |
Correct |
4 ms |
5964 KB |
Output is correct |
7 |
Correct |
4 ms |
5972 KB |
Output is correct |
8 |
Correct |
5 ms |
5972 KB |
Output is correct |
9 |
Correct |
5 ms |
6024 KB |
Output is correct |
10 |
Correct |
4 ms |
5972 KB |
Output is correct |
11 |
Correct |
5 ms |
5964 KB |
Output is correct |
12 |
Correct |
4 ms |
5972 KB |
Output is correct |
13 |
Correct |
6 ms |
5972 KB |
Output is correct |
14 |
Correct |
4 ms |
5972 KB |
Output is correct |
15 |
Correct |
4 ms |
5972 KB |
Output is correct |
16 |
Correct |
5 ms |
5972 KB |
Output is correct |
17 |
Correct |
5 ms |
5972 KB |
Output is correct |
18 |
Correct |
5 ms |
5972 KB |
Output is correct |
19 |
Correct |
4 ms |
5952 KB |
Output is correct |
20 |
Correct |
4 ms |
5972 KB |
Output is correct |
21 |
Correct |
5 ms |
6016 KB |
Output is correct |
22 |
Correct |
5 ms |
5972 KB |
Output is correct |
23 |
Correct |
4 ms |
5948 KB |
Output is correct |
24 |
Correct |
4 ms |
5948 KB |
Output is correct |
25 |
Correct |
5 ms |
5972 KB |
Output is correct |
26 |
Correct |
4 ms |
5948 KB |
Output is correct |
27 |
Correct |
5 ms |
6008 KB |
Output is correct |
28 |
Correct |
4 ms |
5844 KB |
Output is correct |
29 |
Correct |
60 ms |
6056 KB |
Output is correct |
30 |
Correct |
40 ms |
6020 KB |
Output is correct |
31 |
Correct |
35 ms |
6044 KB |
Output is correct |
32 |
Correct |
21 ms |
5972 KB |
Output is correct |
33 |
Correct |
7 ms |
5988 KB |
Output is correct |
34 |
Correct |
84 ms |
5972 KB |
Output is correct |
35 |
Correct |
100 ms |
6092 KB |
Output is correct |
36 |
Correct |
63 ms |
5972 KB |
Output is correct |
37 |
Correct |
37 ms |
6020 KB |
Output is correct |
38 |
Correct |
1752 ms |
9160 KB |
Output is correct |
39 |
Correct |
157 ms |
27688 KB |
Output is correct |
40 |
Correct |
2266 ms |
8208 KB |
Output is correct |
41 |
Correct |
2035 ms |
6996 KB |
Output is correct |
42 |
Correct |
1770 ms |
8536 KB |
Output is correct |
43 |
Correct |
49 ms |
6988 KB |
Output is correct |
44 |
Correct |
1939 ms |
6476 KB |
Output is correct |
45 |
Correct |
245 ms |
19080 KB |
Output is correct |
46 |
Correct |
294 ms |
18956 KB |
Output is correct |
47 |
Correct |
262 ms |
22656 KB |
Output is correct |
48 |
Correct |
283 ms |
22800 KB |
Output is correct |
49 |
Correct |
317 ms |
19120 KB |
Output is correct |
50 |
Correct |
281 ms |
19288 KB |
Output is correct |
51 |
Correct |
91 ms |
19952 KB |
Output is correct |
52 |
Correct |
73 ms |
20184 KB |
Output is correct |
53 |
Correct |
1376 ms |
7036 KB |
Output is correct |
54 |
Correct |
221 ms |
19020 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
4 ms |
5972 KB |
Output is correct |
2 |
Correct |
4 ms |
5948 KB |
Output is correct |
3 |
Correct |
4 ms |
5956 KB |
Output is correct |
4 |
Correct |
4 ms |
5844 KB |
Output is correct |
5 |
Incorrect |
8 ms |
5844 KB |
Output isn't correct |
6 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
3 ms |
5844 KB |
Output is correct |
2 |
Correct |
4 ms |
5972 KB |
Output is correct |
3 |
Correct |
3 ms |
5972 KB |
Output is correct |
4 |
Correct |
13 ms |
6100 KB |
Output is correct |
5 |
Correct |
26 ms |
6228 KB |
Output is correct |
6 |
Correct |
6 ms |
5972 KB |
Output is correct |
7 |
Correct |
6 ms |
5924 KB |
Output is correct |
8 |
Correct |
62 ms |
6152 KB |
Output is correct |
9 |
Correct |
1800 ms |
9144 KB |
Output is correct |
10 |
Correct |
147 ms |
27716 KB |
Output is correct |
11 |
Incorrect |
10 ms |
6068 KB |
Output isn't correct |
12 |
Halted |
0 ms |
0 KB |
- |