#include <bits/stdc++.h>
using namespace std;
using ll = long long;
using pii = pair<int, int>;
#define pb push_back
#define ff first
#define ss second
#define ins insert
struct FT{
vector<int> bit;
int n;
FT(int ns){
n = ns;
bit.resize(n + 1);
}
void add(int v, int k){
while (v <= n){
bit[v] += k;
v |= (v + 1);
}
}
void upd(int l, int r, int x){
add(l, x);
add(r + 1, -x);
}
int get(int v){
int out = 0;
while (v > 0){
out += bit[v];
v = (v & (v + 1)) - 1;
}
return out;
}
};
int main(){
ios_base::sync_with_stdio(0);
cin.tie(0);
cout.tie(0);
int tt; cin>>tt;
while (tt--){
int n; cin>>n;
vector<int> g[n + 1];
for (int i = 1; i < n; i++){
int a, b; cin>>a>>b;
g[a].pb(b);
g[b].pb(a);
}
const int lg = log2(n);
vector<vector<int>> pw(n + 1, vector<int>(lg + 1));
vector<int> tin(n + 1), tout(n + 1);
int timer = 0;
function<void(int, int)> fill = [&](int v, int pr){
tin[v] = ++timer;
pw[v][0] = pr;
for (int i = 1; i <= lg; i++){
pw[v][i] = pw[pw[v][i - 1]][i - 1];
}
for (int i: g[v]){
if (i == pr) continue;
fill(i, v);
}
tout[v] = timer;
};
fill(1, 1);
auto check = [&](int x, int y){
return (tin[x] <= tin[y] && tout[x] >= tout[y]);
};
int m; cin>>m;
vector<int> s(m + 1), t(m + 1);
for (int i = 1; i <= m; i++){
cin>>s[i]>>t[i];
}
auto lca = [&](int x, int y){
if (check(x, y)) return x;
if (check(y, x)) return y;
for (int i = lg; i >= 0; i--){
if (!check(pw[x][i], y)){
x = pw[x][i];
}
}
return pw[x][0];
};
auto on = [&](int x, int y, int z){
int l = lca(x, y);
return check(l, z) && (check(z, x) || check(z, y));
};
vector<int> f[m + 1];
bool out = 1;
for (int i = 1; i <= m; i++){
for (int j = 1; j <= m; j++){
if (i == j) continue;
bool b1 = on(s[i], t[i], s[j]), b2 = on(s[i], t[i], t[j]);
if (b1 && b2){
out = 0;
break;
}
if (b1) f[i].pb(j);
else if (b2) f[j].pb(i);
}
if (!out) break;
}
if (!out){
cout<<"No"<<"\n";
continue;
}
vector<bool> used(m + 1), seen(m + 1);
vector<int> order;
function<void(int)> dfs = [&](int v){
if (!out) return;
used[v] = seen[v] = 1;
for (int i: f[v]){
if (used[i]){
if (seen[i]){
out = 0;
return;
}
continue;
}
dfs(i);
}
order.pb(v);
seen[v] = 0;
};
for (int i = 1; i <= m; i++){
if (!used[i]){
dfs(i);
}
}
cout<<((out) ? "Yes" : "No")<<"\n";
}
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
344 KB |
Output is correct |
2 |
Correct |
0 ms |
348 KB |
Output is correct |
3 |
Correct |
0 ms |
348 KB |
Output is correct |
4 |
Correct |
14 ms |
484 KB |
Output is correct |
5 |
Correct |
19 ms |
480 KB |
Output is correct |
6 |
Correct |
1 ms |
348 KB |
Output is correct |
7 |
Correct |
1 ms |
348 KB |
Output is correct |
8 |
Correct |
3 ms |
348 KB |
Output is correct |
9 |
Correct |
39 ms |
3004 KB |
Output is correct |
10 |
Correct |
43 ms |
32592 KB |
Output is correct |
11 |
Correct |
4 ms |
348 KB |
Output is correct |
12 |
Correct |
26 ms |
484 KB |
Output is correct |
13 |
Execution timed out |
5085 ms |
39448 KB |
Time limit exceeded |
14 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
348 KB |
Output is correct |
2 |
Correct |
0 ms |
348 KB |
Output is correct |
3 |
Correct |
1 ms |
348 KB |
Output is correct |
4 |
Correct |
1 ms |
348 KB |
Output is correct |
5 |
Correct |
2 ms |
348 KB |
Output is correct |
6 |
Correct |
1 ms |
348 KB |
Output is correct |
7 |
Correct |
1 ms |
344 KB |
Output is correct |
8 |
Correct |
1 ms |
348 KB |
Output is correct |
9 |
Correct |
1 ms |
348 KB |
Output is correct |
10 |
Correct |
2 ms |
348 KB |
Output is correct |
11 |
Correct |
1 ms |
348 KB |
Output is correct |
12 |
Correct |
1 ms |
348 KB |
Output is correct |
13 |
Correct |
1 ms |
348 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
348 KB |
Output is correct |
2 |
Correct |
0 ms |
348 KB |
Output is correct |
3 |
Correct |
1 ms |
348 KB |
Output is correct |
4 |
Correct |
1 ms |
348 KB |
Output is correct |
5 |
Correct |
2 ms |
348 KB |
Output is correct |
6 |
Correct |
1 ms |
348 KB |
Output is correct |
7 |
Correct |
1 ms |
344 KB |
Output is correct |
8 |
Correct |
1 ms |
348 KB |
Output is correct |
9 |
Correct |
1 ms |
348 KB |
Output is correct |
10 |
Correct |
2 ms |
348 KB |
Output is correct |
11 |
Correct |
1 ms |
348 KB |
Output is correct |
12 |
Correct |
1 ms |
348 KB |
Output is correct |
13 |
Correct |
1 ms |
348 KB |
Output is correct |
14 |
Correct |
1 ms |
348 KB |
Output is correct |
15 |
Correct |
0 ms |
348 KB |
Output is correct |
16 |
Correct |
1 ms |
344 KB |
Output is correct |
17 |
Correct |
1 ms |
348 KB |
Output is correct |
18 |
Correct |
1 ms |
348 KB |
Output is correct |
19 |
Correct |
1 ms |
348 KB |
Output is correct |
20 |
Correct |
1 ms |
348 KB |
Output is correct |
21 |
Correct |
2 ms |
348 KB |
Output is correct |
22 |
Correct |
1 ms |
348 KB |
Output is correct |
23 |
Correct |
0 ms |
348 KB |
Output is correct |
24 |
Correct |
0 ms |
348 KB |
Output is correct |
25 |
Correct |
1 ms |
344 KB |
Output is correct |
26 |
Correct |
0 ms |
348 KB |
Output is correct |
27 |
Correct |
1 ms |
348 KB |
Output is correct |
28 |
Correct |
0 ms |
348 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
348 KB |
Output is correct |
2 |
Correct |
0 ms |
348 KB |
Output is correct |
3 |
Correct |
1 ms |
348 KB |
Output is correct |
4 |
Correct |
1 ms |
348 KB |
Output is correct |
5 |
Correct |
2 ms |
348 KB |
Output is correct |
6 |
Correct |
1 ms |
348 KB |
Output is correct |
7 |
Correct |
1 ms |
344 KB |
Output is correct |
8 |
Correct |
1 ms |
348 KB |
Output is correct |
9 |
Correct |
1 ms |
348 KB |
Output is correct |
10 |
Correct |
2 ms |
348 KB |
Output is correct |
11 |
Correct |
1 ms |
348 KB |
Output is correct |
12 |
Correct |
1 ms |
348 KB |
Output is correct |
13 |
Correct |
1 ms |
348 KB |
Output is correct |
14 |
Correct |
1 ms |
348 KB |
Output is correct |
15 |
Correct |
0 ms |
348 KB |
Output is correct |
16 |
Correct |
1 ms |
344 KB |
Output is correct |
17 |
Correct |
1 ms |
348 KB |
Output is correct |
18 |
Correct |
1 ms |
348 KB |
Output is correct |
19 |
Correct |
1 ms |
348 KB |
Output is correct |
20 |
Correct |
1 ms |
348 KB |
Output is correct |
21 |
Correct |
2 ms |
348 KB |
Output is correct |
22 |
Correct |
1 ms |
348 KB |
Output is correct |
23 |
Correct |
0 ms |
348 KB |
Output is correct |
24 |
Correct |
0 ms |
348 KB |
Output is correct |
25 |
Correct |
1 ms |
344 KB |
Output is correct |
26 |
Correct |
0 ms |
348 KB |
Output is correct |
27 |
Correct |
1 ms |
348 KB |
Output is correct |
28 |
Correct |
0 ms |
348 KB |
Output is correct |
29 |
Correct |
2 ms |
604 KB |
Output is correct |
30 |
Correct |
2 ms |
348 KB |
Output is correct |
31 |
Correct |
2 ms |
344 KB |
Output is correct |
32 |
Correct |
3 ms |
348 KB |
Output is correct |
33 |
Correct |
1 ms |
532 KB |
Output is correct |
34 |
Correct |
3 ms |
544 KB |
Output is correct |
35 |
Correct |
3 ms |
348 KB |
Output is correct |
36 |
Correct |
6 ms |
348 KB |
Output is correct |
37 |
Correct |
4 ms |
348 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
348 KB |
Output is correct |
2 |
Correct |
0 ms |
348 KB |
Output is correct |
3 |
Correct |
1 ms |
348 KB |
Output is correct |
4 |
Correct |
1 ms |
348 KB |
Output is correct |
5 |
Correct |
2 ms |
348 KB |
Output is correct |
6 |
Correct |
1 ms |
348 KB |
Output is correct |
7 |
Correct |
1 ms |
344 KB |
Output is correct |
8 |
Correct |
1 ms |
348 KB |
Output is correct |
9 |
Correct |
1 ms |
348 KB |
Output is correct |
10 |
Correct |
2 ms |
348 KB |
Output is correct |
11 |
Correct |
1 ms |
348 KB |
Output is correct |
12 |
Correct |
1 ms |
348 KB |
Output is correct |
13 |
Correct |
1 ms |
348 KB |
Output is correct |
14 |
Correct |
1 ms |
348 KB |
Output is correct |
15 |
Correct |
0 ms |
348 KB |
Output is correct |
16 |
Correct |
1 ms |
344 KB |
Output is correct |
17 |
Correct |
1 ms |
348 KB |
Output is correct |
18 |
Correct |
1 ms |
348 KB |
Output is correct |
19 |
Correct |
1 ms |
348 KB |
Output is correct |
20 |
Correct |
1 ms |
348 KB |
Output is correct |
21 |
Correct |
2 ms |
348 KB |
Output is correct |
22 |
Correct |
1 ms |
348 KB |
Output is correct |
23 |
Correct |
0 ms |
348 KB |
Output is correct |
24 |
Correct |
0 ms |
348 KB |
Output is correct |
25 |
Correct |
1 ms |
344 KB |
Output is correct |
26 |
Correct |
0 ms |
348 KB |
Output is correct |
27 |
Correct |
1 ms |
348 KB |
Output is correct |
28 |
Correct |
0 ms |
348 KB |
Output is correct |
29 |
Correct |
2 ms |
604 KB |
Output is correct |
30 |
Correct |
2 ms |
348 KB |
Output is correct |
31 |
Correct |
2 ms |
344 KB |
Output is correct |
32 |
Correct |
3 ms |
348 KB |
Output is correct |
33 |
Correct |
1 ms |
532 KB |
Output is correct |
34 |
Correct |
3 ms |
544 KB |
Output is correct |
35 |
Correct |
3 ms |
348 KB |
Output is correct |
36 |
Correct |
6 ms |
348 KB |
Output is correct |
37 |
Correct |
4 ms |
348 KB |
Output is correct |
38 |
Correct |
40 ms |
4296 KB |
Output is correct |
39 |
Correct |
44 ms |
34116 KB |
Output is correct |
40 |
Correct |
108 ms |
2740 KB |
Output is correct |
41 |
Correct |
182 ms |
2488 KB |
Output is correct |
42 |
Correct |
126 ms |
2740 KB |
Output is correct |
43 |
Correct |
37 ms |
2496 KB |
Output is correct |
44 |
Correct |
67 ms |
864 KB |
Output is correct |
45 |
Correct |
61 ms |
21584 KB |
Output is correct |
46 |
Correct |
72 ms |
21668 KB |
Output is correct |
47 |
Correct |
59 ms |
27252 KB |
Output is correct |
48 |
Correct |
58 ms |
27216 KB |
Output is correct |
49 |
Correct |
46 ms |
21700 KB |
Output is correct |
50 |
Correct |
58 ms |
21848 KB |
Output is correct |
51 |
Correct |
45 ms |
23492 KB |
Output is correct |
52 |
Correct |
41 ms |
23376 KB |
Output is correct |
53 |
Correct |
116 ms |
1880 KB |
Output is correct |
54 |
Correct |
63 ms |
21596 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
348 KB |
Output is correct |
2 |
Correct |
0 ms |
348 KB |
Output is correct |
3 |
Correct |
0 ms |
348 KB |
Output is correct |
4 |
Correct |
0 ms |
348 KB |
Output is correct |
5 |
Correct |
5 ms |
348 KB |
Output is correct |
6 |
Correct |
1 ms |
348 KB |
Output is correct |
7 |
Correct |
1 ms |
348 KB |
Output is correct |
8 |
Correct |
0 ms |
348 KB |
Output is correct |
9 |
Correct |
0 ms |
348 KB |
Output is correct |
10 |
Correct |
0 ms |
348 KB |
Output is correct |
11 |
Correct |
0 ms |
348 KB |
Output is correct |
12 |
Correct |
6 ms |
348 KB |
Output is correct |
13 |
Correct |
25 ms |
968 KB |
Output is correct |
14 |
Correct |
44 ms |
1104 KB |
Output is correct |
15 |
Correct |
35 ms |
1108 KB |
Output is correct |
16 |
Correct |
4838 ms |
22368 KB |
Output is correct |
17 |
Correct |
4731 ms |
24404 KB |
Output is correct |
18 |
Execution timed out |
5053 ms |
27016 KB |
Time limit exceeded |
19 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
344 KB |
Output is correct |
2 |
Correct |
0 ms |
348 KB |
Output is correct |
3 |
Correct |
0 ms |
348 KB |
Output is correct |
4 |
Correct |
14 ms |
484 KB |
Output is correct |
5 |
Correct |
19 ms |
480 KB |
Output is correct |
6 |
Correct |
1 ms |
348 KB |
Output is correct |
7 |
Correct |
1 ms |
348 KB |
Output is correct |
8 |
Correct |
3 ms |
348 KB |
Output is correct |
9 |
Correct |
39 ms |
3004 KB |
Output is correct |
10 |
Correct |
43 ms |
32592 KB |
Output is correct |
11 |
Correct |
4 ms |
348 KB |
Output is correct |
12 |
Correct |
26 ms |
484 KB |
Output is correct |
13 |
Execution timed out |
5085 ms |
39448 KB |
Time limit exceeded |
14 |
Halted |
0 ms |
0 KB |
- |