// 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 i = 0; i < m; i++) for(int j = 0; j < m; j++) if(i != j){
if(isinpath(s[i], t[i], s[j]))
ed[j].pb(i);
if(isinpath(s[i], t[i], t[j]))
ed[i].pb(j);
}
for(int i = 0; i < m; i++) if(!mark[i])
dfs(i);
cout << (cy ? "No\n" : "Yes\n");
}
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
7 ms |
9812 KB |
Output is correct |
2 |
Correct |
6 ms |
9848 KB |
Output is correct |
3 |
Correct |
5 ms |
9812 KB |
Output is correct |
4 |
Correct |
16 ms |
10236 KB |
Output is correct |
5 |
Correct |
25 ms |
10608 KB |
Output is correct |
6 |
Correct |
7 ms |
9948 KB |
Output is correct |
7 |
Correct |
6 ms |
9812 KB |
Output is correct |
8 |
Correct |
16 ms |
9964 KB |
Output is correct |
9 |
Correct |
257 ms |
13676 KB |
Output is correct |
10 |
Correct |
63 ms |
33956 KB |
Output is correct |
11 |
Correct |
13 ms |
9940 KB |
Output is correct |
12 |
Correct |
148 ms |
10760 KB |
Output is correct |
13 |
Execution timed out |
5083 ms |
34104 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 |
9812 KB |
Output is correct |
3 |
Correct |
6 ms |
9940 KB |
Output is correct |
4 |
Correct |
8 ms |
9812 KB |
Output is correct |
5 |
Correct |
6 ms |
9812 KB |
Output is correct |
6 |
Correct |
6 ms |
9896 KB |
Output is correct |
7 |
Correct |
6 ms |
9860 KB |
Output is correct |
8 |
Correct |
6 ms |
9932 KB |
Output is correct |
9 |
Correct |
6 ms |
9812 KB |
Output is correct |
10 |
Correct |
6 ms |
9860 KB |
Output is correct |
11 |
Correct |
5 ms |
9812 KB |
Output is correct |
12 |
Correct |
5 ms |
9852 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 |
9812 KB |
Output is correct |
3 |
Correct |
6 ms |
9940 KB |
Output is correct |
4 |
Correct |
8 ms |
9812 KB |
Output is correct |
5 |
Correct |
6 ms |
9812 KB |
Output is correct |
6 |
Correct |
6 ms |
9896 KB |
Output is correct |
7 |
Correct |
6 ms |
9860 KB |
Output is correct |
8 |
Correct |
6 ms |
9932 KB |
Output is correct |
9 |
Correct |
6 ms |
9812 KB |
Output is correct |
10 |
Correct |
6 ms |
9860 KB |
Output is correct |
11 |
Correct |
5 ms |
9812 KB |
Output is correct |
12 |
Correct |
5 ms |
9852 KB |
Output is correct |
13 |
Correct |
5 ms |
9812 KB |
Output is correct |
14 |
Correct |
6 ms |
9844 KB |
Output is correct |
15 |
Correct |
6 ms |
9844 KB |
Output is correct |
16 |
Correct |
6 ms |
9860 KB |
Output is correct |
17 |
Correct |
6 ms |
9856 KB |
Output is correct |
18 |
Correct |
7 ms |
9864 KB |
Output is correct |
19 |
Correct |
5 ms |
9812 KB |
Output is correct |
20 |
Correct |
6 ms |
9864 KB |
Output is correct |
21 |
Correct |
6 ms |
9812 KB |
Output is correct |
22 |
Correct |
6 ms |
9940 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 |
9928 KB |
Output is correct |
26 |
Correct |
5 ms |
9848 KB |
Output is correct |
27 |
Correct |
6 ms |
9924 KB |
Output is correct |
28 |
Correct |
6 ms |
9904 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
5 ms |
9812 KB |
Output is correct |
2 |
Correct |
5 ms |
9812 KB |
Output is correct |
3 |
Correct |
6 ms |
9940 KB |
Output is correct |
4 |
Correct |
8 ms |
9812 KB |
Output is correct |
5 |
Correct |
6 ms |
9812 KB |
Output is correct |
6 |
Correct |
6 ms |
9896 KB |
Output is correct |
7 |
Correct |
6 ms |
9860 KB |
Output is correct |
8 |
Correct |
6 ms |
9932 KB |
Output is correct |
9 |
Correct |
6 ms |
9812 KB |
Output is correct |
10 |
Correct |
6 ms |
9860 KB |
Output is correct |
11 |
Correct |
5 ms |
9812 KB |
Output is correct |
12 |
Correct |
5 ms |
9852 KB |
Output is correct |
13 |
Correct |
5 ms |
9812 KB |
Output is correct |
14 |
Correct |
6 ms |
9844 KB |
Output is correct |
15 |
Correct |
6 ms |
9844 KB |
Output is correct |
16 |
Correct |
6 ms |
9860 KB |
Output is correct |
17 |
Correct |
6 ms |
9856 KB |
Output is correct |
18 |
Correct |
7 ms |
9864 KB |
Output is correct |
19 |
Correct |
5 ms |
9812 KB |
Output is correct |
20 |
Correct |
6 ms |
9864 KB |
Output is correct |
21 |
Correct |
6 ms |
9812 KB |
Output is correct |
22 |
Correct |
6 ms |
9940 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 |
9928 KB |
Output is correct |
26 |
Correct |
5 ms |
9848 KB |
Output is correct |
27 |
Correct |
6 ms |
9924 KB |
Output is correct |
28 |
Correct |
6 ms |
9904 KB |
Output is correct |
29 |
Correct |
17 ms |
9940 KB |
Output is correct |
30 |
Correct |
16 ms |
9936 KB |
Output is correct |
31 |
Correct |
13 ms |
9940 KB |
Output is correct |
32 |
Correct |
11 ms |
9940 KB |
Output is correct |
33 |
Correct |
6 ms |
9812 KB |
Output is correct |
34 |
Correct |
25 ms |
9932 KB |
Output is correct |
35 |
Correct |
25 ms |
9980 KB |
Output is correct |
36 |
Correct |
27 ms |
9864 KB |
Output is correct |
37 |
Correct |
17 ms |
9812 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
5 ms |
9812 KB |
Output is correct |
2 |
Correct |
5 ms |
9812 KB |
Output is correct |
3 |
Correct |
6 ms |
9940 KB |
Output is correct |
4 |
Correct |
8 ms |
9812 KB |
Output is correct |
5 |
Correct |
6 ms |
9812 KB |
Output is correct |
6 |
Correct |
6 ms |
9896 KB |
Output is correct |
7 |
Correct |
6 ms |
9860 KB |
Output is correct |
8 |
Correct |
6 ms |
9932 KB |
Output is correct |
9 |
Correct |
6 ms |
9812 KB |
Output is correct |
10 |
Correct |
6 ms |
9860 KB |
Output is correct |
11 |
Correct |
5 ms |
9812 KB |
Output is correct |
12 |
Correct |
5 ms |
9852 KB |
Output is correct |
13 |
Correct |
5 ms |
9812 KB |
Output is correct |
14 |
Correct |
6 ms |
9844 KB |
Output is correct |
15 |
Correct |
6 ms |
9844 KB |
Output is correct |
16 |
Correct |
6 ms |
9860 KB |
Output is correct |
17 |
Correct |
6 ms |
9856 KB |
Output is correct |
18 |
Correct |
7 ms |
9864 KB |
Output is correct |
19 |
Correct |
5 ms |
9812 KB |
Output is correct |
20 |
Correct |
6 ms |
9864 KB |
Output is correct |
21 |
Correct |
6 ms |
9812 KB |
Output is correct |
22 |
Correct |
6 ms |
9940 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 |
9928 KB |
Output is correct |
26 |
Correct |
5 ms |
9848 KB |
Output is correct |
27 |
Correct |
6 ms |
9924 KB |
Output is correct |
28 |
Correct |
6 ms |
9904 KB |
Output is correct |
29 |
Correct |
17 ms |
9940 KB |
Output is correct |
30 |
Correct |
16 ms |
9936 KB |
Output is correct |
31 |
Correct |
13 ms |
9940 KB |
Output is correct |
32 |
Correct |
11 ms |
9940 KB |
Output is correct |
33 |
Correct |
6 ms |
9812 KB |
Output is correct |
34 |
Correct |
25 ms |
9932 KB |
Output is correct |
35 |
Correct |
25 ms |
9980 KB |
Output is correct |
36 |
Correct |
27 ms |
9864 KB |
Output is correct |
37 |
Correct |
17 ms |
9812 KB |
Output is correct |
38 |
Correct |
273 ms |
13340 KB |
Output is correct |
39 |
Correct |
63 ms |
33616 KB |
Output is correct |
40 |
Correct |
468 ms |
12236 KB |
Output is correct |
41 |
Correct |
468 ms |
11092 KB |
Output is correct |
42 |
Correct |
558 ms |
12656 KB |
Output is correct |
43 |
Correct |
34 ms |
11092 KB |
Output is correct |
44 |
Correct |
427 ms |
10256 KB |
Output is correct |
45 |
Correct |
80 ms |
24780 KB |
Output is correct |
46 |
Correct |
81 ms |
24752 KB |
Output is correct |
47 |
Correct |
79 ms |
28468 KB |
Output is correct |
48 |
Correct |
80 ms |
28536 KB |
Output is correct |
49 |
Correct |
76 ms |
24952 KB |
Output is correct |
50 |
Correct |
78 ms |
25016 KB |
Output is correct |
51 |
Correct |
50 ms |
26036 KB |
Output is correct |
52 |
Correct |
48 ms |
25932 KB |
Output is correct |
53 |
Correct |
488 ms |
11156 KB |
Output is correct |
54 |
Correct |
91 ms |
24760 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
5 ms |
9844 KB |
Output is correct |
2 |
Correct |
5 ms |
9812 KB |
Output is correct |
3 |
Correct |
6 ms |
9804 KB |
Output is correct |
4 |
Correct |
7 ms |
9852 KB |
Output is correct |
5 |
Correct |
14 ms |
9984 KB |
Output is correct |
6 |
Correct |
5 ms |
9812 KB |
Output is correct |
7 |
Correct |
6 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 |
27 ms |
9812 KB |
Output is correct |
13 |
Correct |
212 ms |
10396 KB |
Output is correct |
14 |
Correct |
111 ms |
10620 KB |
Output is correct |
15 |
Correct |
171 ms |
10492 KB |
Output is correct |
16 |
Execution timed out |
5036 ms |
26428 KB |
Time limit exceeded |
17 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
7 ms |
9812 KB |
Output is correct |
2 |
Correct |
6 ms |
9848 KB |
Output is correct |
3 |
Correct |
5 ms |
9812 KB |
Output is correct |
4 |
Correct |
16 ms |
10236 KB |
Output is correct |
5 |
Correct |
25 ms |
10608 KB |
Output is correct |
6 |
Correct |
7 ms |
9948 KB |
Output is correct |
7 |
Correct |
6 ms |
9812 KB |
Output is correct |
8 |
Correct |
16 ms |
9964 KB |
Output is correct |
9 |
Correct |
257 ms |
13676 KB |
Output is correct |
10 |
Correct |
63 ms |
33956 KB |
Output is correct |
11 |
Correct |
13 ms |
9940 KB |
Output is correct |
12 |
Correct |
148 ms |
10760 KB |
Output is correct |
13 |
Execution timed out |
5083 ms |
34104 KB |
Time limit exceeded |
14 |
Halted |
0 ms |
0 KB |
- |