#include<bits/stdc++.h>
using namespace std;
const int nax = 120001;
const int lg = 17;
vector<int>g[nax], t[nax];
int n, m;
int bed[nax], work[nax];
int sp[nax][lg];
int tt = 0, st[nax], en[nax];
int vis[nax], dep[nax];
void dfs(int v, int p = 1, int d = 0) {
dep[v] = d;
sp[v][0] = p;
st[v] = tt++;
for(int j=1; j<lg; ++j) {
sp[v][j] = sp[sp[v][j-1]][j-1];
}
for(int u : g[v]) if(u!=p) {
dfs(u, v, d+1);
}
en[v] = tt-1;
}
bool anc(int u, int v) {
return st[u] <= st[v] && en[u] >= en[v];
}
int LIFT(int u, int v) {
for(int j=lg-1; j>=0; --j) if(!anc(sp[u][j], v)) {
u = sp[u][j];
}
if(!anc(u, v)) u = sp[u][0];
return u;
}
bool lies(int u, int v, int lca, int x) {
if(anc(lca, x)) {
return anc(x, u) || anc(x, v);
}
return false;
}
int bond(int i, int j) {
int lca1 = LIFT(bed[i], work[i]);
int lca2 = LIFT(bed[j], work[j]);
int len1 = dep[bed[i]] + dep[work[i]] - 2 * dep[lca1];
int len2 = dep[bed[j]] + dep[work[j]] - 2 * dep[lca2];
bool spd = false;
if(len1 < len2) {
spd = true;
swap(i, j);
swap(len1, len2);
swap(lca1, lca2);
}
bool f = lies(bed[i], work[i], lca1, bed[j]);
bool ff = lies(bed[i], work[i], lca1, work[j]);
if(f && ff) return 1;
if(spd) {
swap(i, j);
swap(len1, len2);
swap(lca1, lca2);
f = lies(bed[i], work[i], lca1, bed[j]);
ff = lies(bed[i], work[i], lca1, work[j]);
}
if(f) {
t[j].push_back(i);
}
if(ff) {
t[i].push_back(j);
}
return 0;
}
int go(int v) {
vis[v] = 1;
for(int u : t[v]) {
if(!vis[u]) {
if(go(u)) return 1;
} else {
if(vis[u]==1) return 1;
}
}
vis[v] = 2;
return 0;
}
void PlayGround() {
cin>>n;
for(int i=1; i<=n; ++i) {
g[i].clear(), t[i].clear(), tt = vis[i] = 0;
}
for(int i=0; i<n-1; ++i) {
int u, v;
cin>>u>>v;
g[u].push_back(v);
g[v].push_back(u);
}
dfs(1);
cin>>m;
for(int i=1; i<=m; ++i) {
cin>>bed[i]>>work[i];
}
for(int i=1; i<=m; ++i) {
for(int j=1; j<=m; ++j) if(i-j) {
if(bond(i, j)) {
cout<<"No\n";
return;
}
}
}
for(int i=1; i<=m; ++i) if(!vis[i]) {
if(go(i)) {
cout<<"No\n";
return;
}
}
cout<<"Yes\n";
// cerr << "Time elapsed: " << 1.0 * clock() / CLOCKS_PER_SEC << " s.\n";
}
int main() {
ios_base::sync_with_stdio(false);
cin.tie(NULL);
int tc;
cin>>tc;
while(tc--) {
PlayGround();
}
return 0;
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
3 ms |
5972 KB |
Output is correct |
2 |
Correct |
5 ms |
5972 KB |
Output is correct |
3 |
Correct |
3 ms |
5972 KB |
Output is correct |
4 |
Correct |
14 ms |
6016 KB |
Output is correct |
5 |
Correct |
29 ms |
6636 KB |
Output is correct |
6 |
Correct |
6 ms |
5972 KB |
Output is correct |
7 |
Correct |
5 ms |
6064 KB |
Output is correct |
8 |
Correct |
24 ms |
6100 KB |
Output is correct |
9 |
Correct |
490 ms |
9544 KB |
Output is correct |
10 |
Correct |
112 ms |
29772 KB |
Output is correct |
11 |
Correct |
11 ms |
6108 KB |
Output is correct |
12 |
Correct |
107 ms |
6860 KB |
Output is correct |
13 |
Execution timed out |
5028 ms |
29680 KB |
Time limit exceeded |
14 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
3 ms |
5972 KB |
Output is correct |
2 |
Correct |
3 ms |
5972 KB |
Output is correct |
3 |
Correct |
4 ms |
5972 KB |
Output is correct |
4 |
Correct |
5 ms |
5944 KB |
Output is correct |
5 |
Correct |
5 ms |
5972 KB |
Output is correct |
6 |
Correct |
5 ms |
5976 KB |
Output is correct |
7 |
Correct |
6 ms |
5976 KB |
Output is correct |
8 |
Correct |
5 ms |
5980 KB |
Output is correct |
9 |
Correct |
4 ms |
5972 KB |
Output is correct |
10 |
Correct |
5 ms |
5972 KB |
Output is correct |
11 |
Correct |
5 ms |
5972 KB |
Output is correct |
12 |
Correct |
4 ms |
5976 KB |
Output is correct |
13 |
Correct |
4 ms |
5972 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
3 ms |
5972 KB |
Output is correct |
2 |
Correct |
3 ms |
5972 KB |
Output is correct |
3 |
Correct |
4 ms |
5972 KB |
Output is correct |
4 |
Correct |
5 ms |
5944 KB |
Output is correct |
5 |
Correct |
5 ms |
5972 KB |
Output is correct |
6 |
Correct |
5 ms |
5976 KB |
Output is correct |
7 |
Correct |
6 ms |
5976 KB |
Output is correct |
8 |
Correct |
5 ms |
5980 KB |
Output is correct |
9 |
Correct |
4 ms |
5972 KB |
Output is correct |
10 |
Correct |
5 ms |
5972 KB |
Output is correct |
11 |
Correct |
5 ms |
5972 KB |
Output is correct |
12 |
Correct |
4 ms |
5976 KB |
Output is correct |
13 |
Correct |
4 ms |
5972 KB |
Output is correct |
14 |
Correct |
3 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 |
4 ms |
5972 KB |
Output is correct |
18 |
Correct |
5 ms |
5972 KB |
Output is correct |
19 |
Correct |
5 ms |
5972 KB |
Output is correct |
20 |
Correct |
4 ms |
5972 KB |
Output is correct |
21 |
Correct |
5 ms |
5972 KB |
Output is correct |
22 |
Correct |
4 ms |
5972 KB |
Output is correct |
23 |
Correct |
4 ms |
5972 KB |
Output is correct |
24 |
Correct |
3 ms |
5972 KB |
Output is correct |
25 |
Correct |
4 ms |
5972 KB |
Output is correct |
26 |
Correct |
4 ms |
6008 KB |
Output is correct |
27 |
Correct |
4 ms |
5972 KB |
Output is correct |
28 |
Correct |
4 ms |
5964 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
3 ms |
5972 KB |
Output is correct |
2 |
Correct |
3 ms |
5972 KB |
Output is correct |
3 |
Correct |
4 ms |
5972 KB |
Output is correct |
4 |
Correct |
5 ms |
5944 KB |
Output is correct |
5 |
Correct |
5 ms |
5972 KB |
Output is correct |
6 |
Correct |
5 ms |
5976 KB |
Output is correct |
7 |
Correct |
6 ms |
5976 KB |
Output is correct |
8 |
Correct |
5 ms |
5980 KB |
Output is correct |
9 |
Correct |
4 ms |
5972 KB |
Output is correct |
10 |
Correct |
5 ms |
5972 KB |
Output is correct |
11 |
Correct |
5 ms |
5972 KB |
Output is correct |
12 |
Correct |
4 ms |
5976 KB |
Output is correct |
13 |
Correct |
4 ms |
5972 KB |
Output is correct |
14 |
Correct |
3 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 |
4 ms |
5972 KB |
Output is correct |
18 |
Correct |
5 ms |
5972 KB |
Output is correct |
19 |
Correct |
5 ms |
5972 KB |
Output is correct |
20 |
Correct |
4 ms |
5972 KB |
Output is correct |
21 |
Correct |
5 ms |
5972 KB |
Output is correct |
22 |
Correct |
4 ms |
5972 KB |
Output is correct |
23 |
Correct |
4 ms |
5972 KB |
Output is correct |
24 |
Correct |
3 ms |
5972 KB |
Output is correct |
25 |
Correct |
4 ms |
5972 KB |
Output is correct |
26 |
Correct |
4 ms |
6008 KB |
Output is correct |
27 |
Correct |
4 ms |
5972 KB |
Output is correct |
28 |
Correct |
4 ms |
5964 KB |
Output is correct |
29 |
Correct |
24 ms |
6088 KB |
Output is correct |
30 |
Correct |
8 ms |
5972 KB |
Output is correct |
31 |
Correct |
7 ms |
5980 KB |
Output is correct |
32 |
Correct |
12 ms |
6068 KB |
Output is correct |
33 |
Correct |
6 ms |
5972 KB |
Output is correct |
34 |
Correct |
20 ms |
5972 KB |
Output is correct |
35 |
Correct |
14 ms |
6100 KB |
Output is correct |
36 |
Correct |
40 ms |
6044 KB |
Output is correct |
37 |
Correct |
28 ms |
6060 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
3 ms |
5972 KB |
Output is correct |
2 |
Correct |
3 ms |
5972 KB |
Output is correct |
3 |
Correct |
4 ms |
5972 KB |
Output is correct |
4 |
Correct |
5 ms |
5944 KB |
Output is correct |
5 |
Correct |
5 ms |
5972 KB |
Output is correct |
6 |
Correct |
5 ms |
5976 KB |
Output is correct |
7 |
Correct |
6 ms |
5976 KB |
Output is correct |
8 |
Correct |
5 ms |
5980 KB |
Output is correct |
9 |
Correct |
4 ms |
5972 KB |
Output is correct |
10 |
Correct |
5 ms |
5972 KB |
Output is correct |
11 |
Correct |
5 ms |
5972 KB |
Output is correct |
12 |
Correct |
4 ms |
5976 KB |
Output is correct |
13 |
Correct |
4 ms |
5972 KB |
Output is correct |
14 |
Correct |
3 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 |
4 ms |
5972 KB |
Output is correct |
18 |
Correct |
5 ms |
5972 KB |
Output is correct |
19 |
Correct |
5 ms |
5972 KB |
Output is correct |
20 |
Correct |
4 ms |
5972 KB |
Output is correct |
21 |
Correct |
5 ms |
5972 KB |
Output is correct |
22 |
Correct |
4 ms |
5972 KB |
Output is correct |
23 |
Correct |
4 ms |
5972 KB |
Output is correct |
24 |
Correct |
3 ms |
5972 KB |
Output is correct |
25 |
Correct |
4 ms |
5972 KB |
Output is correct |
26 |
Correct |
4 ms |
6008 KB |
Output is correct |
27 |
Correct |
4 ms |
5972 KB |
Output is correct |
28 |
Correct |
4 ms |
5964 KB |
Output is correct |
29 |
Correct |
24 ms |
6088 KB |
Output is correct |
30 |
Correct |
8 ms |
5972 KB |
Output is correct |
31 |
Correct |
7 ms |
5980 KB |
Output is correct |
32 |
Correct |
12 ms |
6068 KB |
Output is correct |
33 |
Correct |
6 ms |
5972 KB |
Output is correct |
34 |
Correct |
20 ms |
5972 KB |
Output is correct |
35 |
Correct |
14 ms |
6100 KB |
Output is correct |
36 |
Correct |
40 ms |
6044 KB |
Output is correct |
37 |
Correct |
28 ms |
6060 KB |
Output is correct |
38 |
Correct |
517 ms |
9596 KB |
Output is correct |
39 |
Correct |
106 ms |
29896 KB |
Output is correct |
40 |
Correct |
449 ms |
8016 KB |
Output is correct |
41 |
Correct |
970 ms |
8020 KB |
Output is correct |
42 |
Correct |
586 ms |
8252 KB |
Output is correct |
43 |
Correct |
39 ms |
7968 KB |
Output is correct |
44 |
Correct |
481 ms |
6532 KB |
Output is correct |
45 |
Correct |
130 ms |
21152 KB |
Output is correct |
46 |
Correct |
141 ms |
21044 KB |
Output is correct |
47 |
Correct |
167 ms |
24696 KB |
Output is correct |
48 |
Correct |
150 ms |
24772 KB |
Output is correct |
49 |
Correct |
68 ms |
21196 KB |
Output is correct |
50 |
Correct |
167 ms |
21156 KB |
Output is correct |
51 |
Correct |
68 ms |
22240 KB |
Output is correct |
52 |
Correct |
63 ms |
22352 KB |
Output is correct |
53 |
Correct |
875 ms |
7364 KB |
Output is correct |
54 |
Correct |
168 ms |
21012 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
5 ms |
5972 KB |
Output is correct |
2 |
Correct |
3 ms |
5972 KB |
Output is correct |
3 |
Correct |
3 ms |
5972 KB |
Output is correct |
4 |
Correct |
5 ms |
5972 KB |
Output is correct |
5 |
Correct |
11 ms |
5992 KB |
Output is correct |
6 |
Correct |
4 ms |
5972 KB |
Output is correct |
7 |
Correct |
5 ms |
6040 KB |
Output is correct |
8 |
Correct |
3 ms |
5972 KB |
Output is correct |
9 |
Correct |
4 ms |
5960 KB |
Output is correct |
10 |
Correct |
4 ms |
6024 KB |
Output is correct |
11 |
Correct |
4 ms |
5968 KB |
Output is correct |
12 |
Correct |
46 ms |
6056 KB |
Output is correct |
13 |
Correct |
49 ms |
6524 KB |
Output is correct |
14 |
Correct |
166 ms |
6860 KB |
Output is correct |
15 |
Correct |
151 ms |
6624 KB |
Output is correct |
16 |
Execution timed out |
5035 ms |
21528 KB |
Time limit exceeded |
17 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
3 ms |
5972 KB |
Output is correct |
2 |
Correct |
5 ms |
5972 KB |
Output is correct |
3 |
Correct |
3 ms |
5972 KB |
Output is correct |
4 |
Correct |
14 ms |
6016 KB |
Output is correct |
5 |
Correct |
29 ms |
6636 KB |
Output is correct |
6 |
Correct |
6 ms |
5972 KB |
Output is correct |
7 |
Correct |
5 ms |
6064 KB |
Output is correct |
8 |
Correct |
24 ms |
6100 KB |
Output is correct |
9 |
Correct |
490 ms |
9544 KB |
Output is correct |
10 |
Correct |
112 ms |
29772 KB |
Output is correct |
11 |
Correct |
11 ms |
6108 KB |
Output is correct |
12 |
Correct |
107 ms |
6860 KB |
Output is correct |
13 |
Execution timed out |
5028 ms |
29680 KB |
Time limit exceeded |
14 |
Halted |
0 ms |
0 KB |
- |