// Lotfan komak
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
#define all(x) x.begin(), x.end()
#define fi first
#define se second
#define pb push_back
const int maxn5 = 2e5 + 10;
const int lg = 20;
const int mod = 1e9 + 7;
vector <int> adj[maxn5], ed[maxn5];
bool cy = false;
int mark[maxn5], par[lg + 1][maxn5];
int s[maxn5], t[maxn5], ti = 0, st[maxn5], ft[maxn5];
int h[maxn5];
void dfs_lca(int v){
st[v] = ++ti;
for(int i = 1; i < lg && par[i - 1][v] != -1; i++)
par[i][v] = par[i - 1][par[i - 1][v]];
for(auto u : adj[v]) if(u != par[0][v]){
par[0][u] = v;
h[u] = h[v] + 1;
dfs_lca(u);
}
ft[v] = ti;
}
void dfs(int v){
mark[v] = 1;
for(auto u : ed[v]){
if(mark[u] == 0)
dfs(u);
else if(mark[u] == 1)
cy = true;
}
mark[v] = 2;
}
inline int lca(int a, int b){
if(h[a] < h[b])
swap(a, b);
int d = h[a] - h[b];
for(int i = 0; i < lg; i++) if((d >> i)&1)
a = par[i][a];
if(a == b)
return a;
for(int i = lg - 1; i >= 0; i--) if(par[i][a] != par[i][b]){
a = par[i][a];
b = par[i][b];
}
return par[0][a];
}
inline bool isinpath(int a, int b, int c){
int x = lca(a, b);
if(h[c] < h[x])
return false;
if(st[c] <= st[a] && ft[c] >= ft[a])
return true;
swap(a, b);
return (st[c] <= st[a] && ft[c] >= ft[a]);
}
int main(){
ios_base::sync_with_stdio(false); cin.tie(0);
int tt; cin >> tt;
while(tt--){
int n; cin >> n;
for(int i = 0; i < n; i++){
adj[i].clear();
for(int j = 0; j < lg; j++)
par[j][i] = -1;
}
for(int i = 0; i < n - 1; i++){
int a, b; cin >> a >> b;
a--; b--;
adj[a].pb(b);
adj[b].pb(a);
}
h[0] = 0;
ti = 0;
dfs_lca(0);
int m; cin >> m;
cy = false;
for(int i = 0; i < m; i++){
ed[i].clear();
mark[i] = 0;
}
for(int i = 0; i < m; i++){
cin >> s[i] >> t[i];
s[i]--; t[i]--;
for(int j = 0; j < i; j++){
if(isinpath(s[i], t[i], s[j]) && isinpath(s[i], t[i], t[j])){
cy = true;
break;
}
if(isinpath(s[j], t[j], s[i]) && isinpath(s[j], t[j], t[i])){
cy = true;
break;
}
if(isinpath(s[i], t[i], s[j])){
if(lca(s[i], t[i]) == s[i]){
if(!(st[s[i]] <= st[t[j]] && ft[s[i]] >= ft[t[j]])){
cy = true;
break;
}
}
else{
if(st[s[i]] <= st[t[j]] && ft[s[i]] >= ft[t[j]]){
cy = true;
break;
}
}
ed[j].pb(i);
continue;
}
if(isinpath(s[i], t[i], t[j])){
if(lca(s[i], t[i]) == t[i]){
if(!(st[t[i]] <= st[s[j]] && ft[t[i]] >= ft[s[j]])){
cy = true;
break;
}
}
else{
if(st[t[i]] <= st[s[j]] && ft[t[i]] >= ft[s[j]]){
cy = true;
break;
}
}
ed[i].pb(j);
continue;
}
swap(i, j);
if(isinpath(s[i], t[i], s[j])){
if(lca(s[i], t[i]) == s[i]){
if(!(st[s[i]] <= st[t[j]] && ft[s[i]] >= ft[t[j]])){
cy = true;
swap(i, j);
break;
}
}
else{
if(st[s[i]] <= st[t[j]] && ft[s[i]] >= ft[t[j]]){
cy = true;
swap(i, j);
break;
}
}
ed[j].pb(i);
swap(i, j);
continue;
}
if(isinpath(s[i], t[i], t[j])){
if(lca(s[i], t[i]) == t[i]){
if(!(st[t[i]] <= st[s[j]] && ft[t[i]] >= ft[s[j]])){
cy = true;
swap(i, j);
break;
}
}
else{
if(st[t[i]] <= st[s[j]] && ft[t[i]] >= ft[s[j]]){
cy = true;
swap(i, j);
break;
}
}
ed[i].pb(j);
swap(i, j);
continue;
}
swap(i, j);
}
}
if(cy){
cout << "No\n";
continue;
}
for(int i = 0; i < m; i++) if(!mark[i])
dfs(i);
cout << (cy ? "No\n" : "Yes\n");
}
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
5 ms |
9812 KB |
Output is correct |
2 |
Correct |
8 ms |
9848 KB |
Output is correct |
3 |
Correct |
5 ms |
9812 KB |
Output is correct |
4 |
Correct |
14 ms |
9876 KB |
Output is correct |
5 |
Correct |
23 ms |
9812 KB |
Output is correct |
6 |
Correct |
7 ms |
9844 KB |
Output is correct |
7 |
Correct |
6 ms |
9812 KB |
Output is correct |
8 |
Correct |
17 ms |
9904 KB |
Output is correct |
9 |
Correct |
292 ms |
11624 KB |
Output is correct |
10 |
Correct |
64 ms |
32528 KB |
Output is correct |
11 |
Correct |
12 ms |
9812 KB |
Output is correct |
12 |
Correct |
77 ms |
9864 KB |
Output is correct |
13 |
Execution timed out |
5049 ms |
34140 KB |
Time limit exceeded |
14 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
5 ms |
9812 KB |
Output is correct |
2 |
Correct |
5 ms |
9856 KB |
Output is correct |
3 |
Correct |
6 ms |
9816 KB |
Output is correct |
4 |
Correct |
6 ms |
9896 KB |
Output is correct |
5 |
Correct |
7 ms |
9812 KB |
Output is correct |
6 |
Correct |
6 ms |
9812 KB |
Output is correct |
7 |
Correct |
6 ms |
9896 KB |
Output is correct |
8 |
Correct |
6 ms |
9812 KB |
Output is correct |
9 |
Correct |
6 ms |
9812 KB |
Output is correct |
10 |
Correct |
6 ms |
9812 KB |
Output is correct |
11 |
Correct |
5 ms |
9812 KB |
Output is correct |
12 |
Correct |
5 ms |
9812 KB |
Output is correct |
13 |
Correct |
5 ms |
9812 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
5 ms |
9812 KB |
Output is correct |
2 |
Correct |
5 ms |
9856 KB |
Output is correct |
3 |
Correct |
6 ms |
9816 KB |
Output is correct |
4 |
Correct |
6 ms |
9896 KB |
Output is correct |
5 |
Correct |
7 ms |
9812 KB |
Output is correct |
6 |
Correct |
6 ms |
9812 KB |
Output is correct |
7 |
Correct |
6 ms |
9896 KB |
Output is correct |
8 |
Correct |
6 ms |
9812 KB |
Output is correct |
9 |
Correct |
6 ms |
9812 KB |
Output is correct |
10 |
Correct |
6 ms |
9812 KB |
Output is correct |
11 |
Correct |
5 ms |
9812 KB |
Output is correct |
12 |
Correct |
5 ms |
9812 KB |
Output is correct |
13 |
Correct |
5 ms |
9812 KB |
Output is correct |
14 |
Correct |
5 ms |
9812 KB |
Output is correct |
15 |
Correct |
7 ms |
9812 KB |
Output is correct |
16 |
Correct |
6 ms |
9812 KB |
Output is correct |
17 |
Correct |
6 ms |
9892 KB |
Output is correct |
18 |
Correct |
6 ms |
9812 KB |
Output is correct |
19 |
Correct |
5 ms |
9812 KB |
Output is correct |
20 |
Correct |
6 ms |
9812 KB |
Output is correct |
21 |
Correct |
6 ms |
9812 KB |
Output is correct |
22 |
Correct |
6 ms |
9812 KB |
Output is correct |
23 |
Correct |
5 ms |
9812 KB |
Output is correct |
24 |
Correct |
5 ms |
9812 KB |
Output is correct |
25 |
Correct |
6 ms |
9812 KB |
Output is correct |
26 |
Correct |
5 ms |
9812 KB |
Output is correct |
27 |
Correct |
6 ms |
9780 KB |
Output is correct |
28 |
Correct |
6 ms |
9744 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
5 ms |
9812 KB |
Output is correct |
2 |
Correct |
5 ms |
9856 KB |
Output is correct |
3 |
Correct |
6 ms |
9816 KB |
Output is correct |
4 |
Correct |
6 ms |
9896 KB |
Output is correct |
5 |
Correct |
7 ms |
9812 KB |
Output is correct |
6 |
Correct |
6 ms |
9812 KB |
Output is correct |
7 |
Correct |
6 ms |
9896 KB |
Output is correct |
8 |
Correct |
6 ms |
9812 KB |
Output is correct |
9 |
Correct |
6 ms |
9812 KB |
Output is correct |
10 |
Correct |
6 ms |
9812 KB |
Output is correct |
11 |
Correct |
5 ms |
9812 KB |
Output is correct |
12 |
Correct |
5 ms |
9812 KB |
Output is correct |
13 |
Correct |
5 ms |
9812 KB |
Output is correct |
14 |
Correct |
5 ms |
9812 KB |
Output is correct |
15 |
Correct |
7 ms |
9812 KB |
Output is correct |
16 |
Correct |
6 ms |
9812 KB |
Output is correct |
17 |
Correct |
6 ms |
9892 KB |
Output is correct |
18 |
Correct |
6 ms |
9812 KB |
Output is correct |
19 |
Correct |
5 ms |
9812 KB |
Output is correct |
20 |
Correct |
6 ms |
9812 KB |
Output is correct |
21 |
Correct |
6 ms |
9812 KB |
Output is correct |
22 |
Correct |
6 ms |
9812 KB |
Output is correct |
23 |
Correct |
5 ms |
9812 KB |
Output is correct |
24 |
Correct |
5 ms |
9812 KB |
Output is correct |
25 |
Correct |
6 ms |
9812 KB |
Output is correct |
26 |
Correct |
5 ms |
9812 KB |
Output is correct |
27 |
Correct |
6 ms |
9780 KB |
Output is correct |
28 |
Correct |
6 ms |
9744 KB |
Output is correct |
29 |
Correct |
15 ms |
9920 KB |
Output is correct |
30 |
Correct |
21 ms |
9904 KB |
Output is correct |
31 |
Correct |
12 ms |
9900 KB |
Output is correct |
32 |
Correct |
14 ms |
9812 KB |
Output is correct |
33 |
Correct |
7 ms |
9812 KB |
Output is correct |
34 |
Correct |
27 ms |
9776 KB |
Output is correct |
35 |
Correct |
21 ms |
9896 KB |
Output is correct |
36 |
Correct |
38 ms |
9900 KB |
Output is correct |
37 |
Correct |
23 ms |
9812 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
5 ms |
9812 KB |
Output is correct |
2 |
Correct |
5 ms |
9856 KB |
Output is correct |
3 |
Correct |
6 ms |
9816 KB |
Output is correct |
4 |
Correct |
6 ms |
9896 KB |
Output is correct |
5 |
Correct |
7 ms |
9812 KB |
Output is correct |
6 |
Correct |
6 ms |
9812 KB |
Output is correct |
7 |
Correct |
6 ms |
9896 KB |
Output is correct |
8 |
Correct |
6 ms |
9812 KB |
Output is correct |
9 |
Correct |
6 ms |
9812 KB |
Output is correct |
10 |
Correct |
6 ms |
9812 KB |
Output is correct |
11 |
Correct |
5 ms |
9812 KB |
Output is correct |
12 |
Correct |
5 ms |
9812 KB |
Output is correct |
13 |
Correct |
5 ms |
9812 KB |
Output is correct |
14 |
Correct |
5 ms |
9812 KB |
Output is correct |
15 |
Correct |
7 ms |
9812 KB |
Output is correct |
16 |
Correct |
6 ms |
9812 KB |
Output is correct |
17 |
Correct |
6 ms |
9892 KB |
Output is correct |
18 |
Correct |
6 ms |
9812 KB |
Output is correct |
19 |
Correct |
5 ms |
9812 KB |
Output is correct |
20 |
Correct |
6 ms |
9812 KB |
Output is correct |
21 |
Correct |
6 ms |
9812 KB |
Output is correct |
22 |
Correct |
6 ms |
9812 KB |
Output is correct |
23 |
Correct |
5 ms |
9812 KB |
Output is correct |
24 |
Correct |
5 ms |
9812 KB |
Output is correct |
25 |
Correct |
6 ms |
9812 KB |
Output is correct |
26 |
Correct |
5 ms |
9812 KB |
Output is correct |
27 |
Correct |
6 ms |
9780 KB |
Output is correct |
28 |
Correct |
6 ms |
9744 KB |
Output is correct |
29 |
Correct |
15 ms |
9920 KB |
Output is correct |
30 |
Correct |
21 ms |
9904 KB |
Output is correct |
31 |
Correct |
12 ms |
9900 KB |
Output is correct |
32 |
Correct |
14 ms |
9812 KB |
Output is correct |
33 |
Correct |
7 ms |
9812 KB |
Output is correct |
34 |
Correct |
27 ms |
9776 KB |
Output is correct |
35 |
Correct |
21 ms |
9896 KB |
Output is correct |
36 |
Correct |
38 ms |
9900 KB |
Output is correct |
37 |
Correct |
23 ms |
9812 KB |
Output is correct |
38 |
Correct |
268 ms |
11724 KB |
Output is correct |
39 |
Correct |
61 ms |
32460 KB |
Output is correct |
40 |
Correct |
618 ms |
10988 KB |
Output is correct |
41 |
Correct |
954 ms |
10772 KB |
Output is correct |
42 |
Correct |
735 ms |
11336 KB |
Output is correct |
43 |
Correct |
32 ms |
10744 KB |
Output is correct |
44 |
Correct |
517 ms |
10376 KB |
Output is correct |
45 |
Correct |
134 ms |
24356 KB |
Output is correct |
46 |
Correct |
138 ms |
24412 KB |
Output is correct |
47 |
Correct |
126 ms |
28092 KB |
Output is correct |
48 |
Correct |
129 ms |
28276 KB |
Output is correct |
49 |
Correct |
134 ms |
24532 KB |
Output is correct |
50 |
Correct |
135 ms |
24568 KB |
Output is correct |
51 |
Correct |
53 ms |
25548 KB |
Output is correct |
52 |
Correct |
52 ms |
25584 KB |
Output is correct |
53 |
Correct |
707 ms |
10836 KB |
Output is correct |
54 |
Correct |
132 ms |
24380 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
5 ms |
9812 KB |
Output is correct |
2 |
Correct |
5 ms |
9820 KB |
Output is correct |
3 |
Correct |
5 ms |
9812 KB |
Output is correct |
4 |
Correct |
5 ms |
9812 KB |
Output is correct |
5 |
Correct |
11 ms |
9860 KB |
Output is correct |
6 |
Correct |
5 ms |
9812 KB |
Output is correct |
7 |
Correct |
5 ms |
9812 KB |
Output is correct |
8 |
Correct |
5 ms |
9812 KB |
Output is correct |
9 |
Correct |
5 ms |
9812 KB |
Output is correct |
10 |
Correct |
5 ms |
9812 KB |
Output is correct |
11 |
Correct |
5 ms |
9812 KB |
Output is correct |
12 |
Correct |
35 ms |
9896 KB |
Output is correct |
13 |
Incorrect |
291 ms |
9908 KB |
Output isn't correct |
14 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
5 ms |
9812 KB |
Output is correct |
2 |
Correct |
8 ms |
9848 KB |
Output is correct |
3 |
Correct |
5 ms |
9812 KB |
Output is correct |
4 |
Correct |
14 ms |
9876 KB |
Output is correct |
5 |
Correct |
23 ms |
9812 KB |
Output is correct |
6 |
Correct |
7 ms |
9844 KB |
Output is correct |
7 |
Correct |
6 ms |
9812 KB |
Output is correct |
8 |
Correct |
17 ms |
9904 KB |
Output is correct |
9 |
Correct |
292 ms |
11624 KB |
Output is correct |
10 |
Correct |
64 ms |
32528 KB |
Output is correct |
11 |
Correct |
12 ms |
9812 KB |
Output is correct |
12 |
Correct |
77 ms |
9864 KB |
Output is correct |
13 |
Execution timed out |
5049 ms |
34140 KB |
Time limit exceeded |
14 |
Halted |
0 ms |
0 KB |
- |