// ~ Be Name Khoda ~
#include<bits/stdc++.h>
//#pragma GCC optimize ("Ofast")
//#pragma GCC target("avx2")
//#pragma GCC optimize("unroll-loops,O3")
using namespace std;
typedef long long ll;
#define pb push_back
#define mp make_pair
#define all(x) x.begin(), x.end()
#define fi first
#define se second
const int maxn = 1e6 + 10;
const int maxn5 = 2e5 + 10;
const int maxnt = 1.2e6 + 10;
const int maxn3 = 1e3 + 10;
const int mod = 1e9 + 7;
const int lg = 18;
const ll inf = 1e18;
int mark[maxn5 * lg * 2], s[maxn5], t[maxn5], h[maxn5];
int par[lg][maxn5], newnode = 0, in[lg][maxn5], out[lg][maxn5];
vector <int> adj[maxn5], ed[maxn5 * lg * 2];
bool cy = false;
void dfs_lca(int v){
if(!out[0][v])
out[0][v] = newnode++;
if(!in[0][v])
in[0][v] = newnode++;
for(int i = 1; i < lg && par[i - 1][v] != -1; i++){
par[i][v] = par[i - 1][par[i - 1][v]];
out[i][v] = newnode++;
in[i][v] = newnode++;
ed[out[i][v]].pb(out[i - 1][v]);
ed[out[i][v]].pb(out[i - 1][par[i - 1][v]]);
ed[in[i - 1][v]].pb(in[i][v]);
ed[in[i - 1][par[i - 1][v]]].pb(in[i][v]);
}
for(auto u : adj[v]) if(u != par[0][v]){
par[0][u] = v;
h[u] = h[v] + 1;
dfs_lca(u);
}
}
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];
}
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;
in[j][i] = out[j][i] = 0;
}
}
for(int i = 0; i < n - 1; i++){
int a, b; cin >> a >> b;
a--; b--;
adj[a].pb(b);
adj[b].pb(a);
}
int m; cin >> m;
newnode = m;
for(int i = 0; i < m; i++){
cin >> s[i] >> t[i];
s[i]--; t[i]--;
if(!in[0][s[i]])
in[0][s[i]] = newnode++;
if(!out[0][t[i]])
out[0][t[i]] = newnode++;
ed[i].pb(in[0][s[i]]);
ed[out[0][t[i]]].pb(i);
}
h[0] = 0;
dfs_lca(0);
for(int i = 0; i < m; i++){
int v = s[i], u = t[i];
if(v == u)
continue;
ed[i].pb(out[0][v]);
ed[in[0][u]].pb(i);
if(h[v] < h[u])
swap(u, v);
int c = lca(u, v);
bool re = true;
if(c == u){
re = false;
v = par[0][v];
}
else{
u = par[0][u];
v = par[0][v];
}
int d = h[v] - h[u];
for(int j = 0; j < lg; j++) if((d >> j)&1){
ed[i].pb(out[j][v]);
ed[in[j][v]].pb(i);
v = par[j][v];
}
if(u == v){
if(re){
ed[i].pb(out[0][v]);
ed[in[0][v]].pb(i);
}
continue;
}
for(int j = lg - 1; j >= 0; j--) if(par[j][v] != par[j][u]){
ed[i].pb(out[j][v]);
ed[i].pb(out[j][u]);
ed[in[j][v]].pb(i);
ed[in[j][u]].pb(i);
v = par[j][v];
u = par[j][u];
}
ed[i].pb(out[0][v]);
ed[i].pb(out[0][par[0][v]]);
ed[in[0][v]].pb(i);
ed[in[0][par[0][v]]].pb(i);
}
cy = false;
for(int i = 0; i < newnode; i++) if(!mark[i])
dfs(i);
cout << (cy ? "No\n" : "Yes\n");
for(int i = 0; i < newnode; i++){
ed[i].clear();
mark[i] = 0;
}
}
}
/*
Why so fast...?
Hold on...
Hold your breath...
Need anything else?
Nein, Kein Problem...
Das Leben war schon immer so grausam :)
*/
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
91 ms |
174404 KB |
Output is correct |
2 |
Correct |
82 ms |
174356 KB |
Output is correct |
3 |
Correct |
90 ms |
174288 KB |
Output is correct |
4 |
Correct |
123 ms |
174412 KB |
Output is correct |
5 |
Correct |
136 ms |
174500 KB |
Output is correct |
6 |
Correct |
93 ms |
174556 KB |
Output is correct |
7 |
Correct |
81 ms |
174580 KB |
Output is correct |
8 |
Correct |
81 ms |
174620 KB |
Output is correct |
9 |
Correct |
343 ms |
181852 KB |
Output is correct |
10 |
Correct |
1080 ms |
352772 KB |
Output is correct |
11 |
Correct |
91 ms |
174364 KB |
Output is correct |
12 |
Correct |
153 ms |
174612 KB |
Output is correct |
13 |
Correct |
1061 ms |
358052 KB |
Output is correct |
14 |
Correct |
924 ms |
358152 KB |
Output is correct |
15 |
Correct |
1469 ms |
383576 KB |
Output is correct |
16 |
Correct |
1920 ms |
434788 KB |
Output is correct |
17 |
Correct |
1216 ms |
361716 KB |
Output is correct |
18 |
Correct |
1034 ms |
361536 KB |
Output is correct |
19 |
Correct |
1199 ms |
361688 KB |
Output is correct |
20 |
Correct |
1184 ms |
361860 KB |
Output is correct |
21 |
Correct |
1241 ms |
377964 KB |
Output is correct |
22 |
Correct |
853 ms |
357048 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
81 ms |
174340 KB |
Output is correct |
2 |
Correct |
82 ms |
174320 KB |
Output is correct |
3 |
Correct |
94 ms |
174532 KB |
Output is correct |
4 |
Correct |
94 ms |
174484 KB |
Output is correct |
5 |
Correct |
83 ms |
174484 KB |
Output is correct |
6 |
Correct |
87 ms |
174600 KB |
Output is correct |
7 |
Correct |
83 ms |
174500 KB |
Output is correct |
8 |
Correct |
82 ms |
174604 KB |
Output is correct |
9 |
Correct |
85 ms |
174528 KB |
Output is correct |
10 |
Correct |
81 ms |
174592 KB |
Output is correct |
11 |
Correct |
81 ms |
174528 KB |
Output is correct |
12 |
Correct |
81 ms |
174500 KB |
Output is correct |
13 |
Correct |
82 ms |
174540 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
81 ms |
174340 KB |
Output is correct |
2 |
Correct |
82 ms |
174320 KB |
Output is correct |
3 |
Correct |
94 ms |
174532 KB |
Output is correct |
4 |
Correct |
94 ms |
174484 KB |
Output is correct |
5 |
Correct |
83 ms |
174484 KB |
Output is correct |
6 |
Correct |
87 ms |
174600 KB |
Output is correct |
7 |
Correct |
83 ms |
174500 KB |
Output is correct |
8 |
Correct |
82 ms |
174604 KB |
Output is correct |
9 |
Correct |
85 ms |
174528 KB |
Output is correct |
10 |
Correct |
81 ms |
174592 KB |
Output is correct |
11 |
Correct |
81 ms |
174528 KB |
Output is correct |
12 |
Correct |
81 ms |
174500 KB |
Output is correct |
13 |
Correct |
82 ms |
174540 KB |
Output is correct |
14 |
Correct |
80 ms |
174332 KB |
Output is correct |
15 |
Correct |
84 ms |
174384 KB |
Output is correct |
16 |
Correct |
82 ms |
174572 KB |
Output is correct |
17 |
Correct |
90 ms |
174572 KB |
Output is correct |
18 |
Correct |
83 ms |
174540 KB |
Output is correct |
19 |
Incorrect |
80 ms |
174476 KB |
Output isn't correct |
20 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
81 ms |
174340 KB |
Output is correct |
2 |
Correct |
82 ms |
174320 KB |
Output is correct |
3 |
Correct |
94 ms |
174532 KB |
Output is correct |
4 |
Correct |
94 ms |
174484 KB |
Output is correct |
5 |
Correct |
83 ms |
174484 KB |
Output is correct |
6 |
Correct |
87 ms |
174600 KB |
Output is correct |
7 |
Correct |
83 ms |
174500 KB |
Output is correct |
8 |
Correct |
82 ms |
174604 KB |
Output is correct |
9 |
Correct |
85 ms |
174528 KB |
Output is correct |
10 |
Correct |
81 ms |
174592 KB |
Output is correct |
11 |
Correct |
81 ms |
174528 KB |
Output is correct |
12 |
Correct |
81 ms |
174500 KB |
Output is correct |
13 |
Correct |
82 ms |
174540 KB |
Output is correct |
14 |
Correct |
80 ms |
174332 KB |
Output is correct |
15 |
Correct |
84 ms |
174384 KB |
Output is correct |
16 |
Correct |
82 ms |
174572 KB |
Output is correct |
17 |
Correct |
90 ms |
174572 KB |
Output is correct |
18 |
Correct |
83 ms |
174540 KB |
Output is correct |
19 |
Incorrect |
80 ms |
174476 KB |
Output isn't correct |
20 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
81 ms |
174340 KB |
Output is correct |
2 |
Correct |
82 ms |
174320 KB |
Output is correct |
3 |
Correct |
94 ms |
174532 KB |
Output is correct |
4 |
Correct |
94 ms |
174484 KB |
Output is correct |
5 |
Correct |
83 ms |
174484 KB |
Output is correct |
6 |
Correct |
87 ms |
174600 KB |
Output is correct |
7 |
Correct |
83 ms |
174500 KB |
Output is correct |
8 |
Correct |
82 ms |
174604 KB |
Output is correct |
9 |
Correct |
85 ms |
174528 KB |
Output is correct |
10 |
Correct |
81 ms |
174592 KB |
Output is correct |
11 |
Correct |
81 ms |
174528 KB |
Output is correct |
12 |
Correct |
81 ms |
174500 KB |
Output is correct |
13 |
Correct |
82 ms |
174540 KB |
Output is correct |
14 |
Correct |
80 ms |
174332 KB |
Output is correct |
15 |
Correct |
84 ms |
174384 KB |
Output is correct |
16 |
Correct |
82 ms |
174572 KB |
Output is correct |
17 |
Correct |
90 ms |
174572 KB |
Output is correct |
18 |
Correct |
83 ms |
174540 KB |
Output is correct |
19 |
Incorrect |
80 ms |
174476 KB |
Output isn't correct |
20 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
80 ms |
174292 KB |
Output is correct |
2 |
Correct |
83 ms |
174568 KB |
Output is correct |
3 |
Correct |
78 ms |
174380 KB |
Output is correct |
4 |
Correct |
106 ms |
174332 KB |
Output is correct |
5 |
Correct |
91 ms |
174428 KB |
Output is correct |
6 |
Correct |
79 ms |
174412 KB |
Output is correct |
7 |
Correct |
80 ms |
174536 KB |
Output is correct |
8 |
Incorrect |
78 ms |
174364 KB |
Output isn't correct |
9 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
91 ms |
174404 KB |
Output is correct |
2 |
Correct |
82 ms |
174356 KB |
Output is correct |
3 |
Correct |
90 ms |
174288 KB |
Output is correct |
4 |
Correct |
123 ms |
174412 KB |
Output is correct |
5 |
Correct |
136 ms |
174500 KB |
Output is correct |
6 |
Correct |
93 ms |
174556 KB |
Output is correct |
7 |
Correct |
81 ms |
174580 KB |
Output is correct |
8 |
Correct |
81 ms |
174620 KB |
Output is correct |
9 |
Correct |
343 ms |
181852 KB |
Output is correct |
10 |
Correct |
1080 ms |
352772 KB |
Output is correct |
11 |
Correct |
91 ms |
174364 KB |
Output is correct |
12 |
Correct |
153 ms |
174612 KB |
Output is correct |
13 |
Correct |
1061 ms |
358052 KB |
Output is correct |
14 |
Correct |
924 ms |
358152 KB |
Output is correct |
15 |
Correct |
1469 ms |
383576 KB |
Output is correct |
16 |
Correct |
1920 ms |
434788 KB |
Output is correct |
17 |
Correct |
1216 ms |
361716 KB |
Output is correct |
18 |
Correct |
1034 ms |
361536 KB |
Output is correct |
19 |
Correct |
1199 ms |
361688 KB |
Output is correct |
20 |
Correct |
1184 ms |
361860 KB |
Output is correct |
21 |
Correct |
1241 ms |
377964 KB |
Output is correct |
22 |
Correct |
853 ms |
357048 KB |
Output is correct |
23 |
Correct |
81 ms |
174340 KB |
Output is correct |
24 |
Correct |
82 ms |
174320 KB |
Output is correct |
25 |
Correct |
94 ms |
174532 KB |
Output is correct |
26 |
Correct |
94 ms |
174484 KB |
Output is correct |
27 |
Correct |
83 ms |
174484 KB |
Output is correct |
28 |
Correct |
87 ms |
174600 KB |
Output is correct |
29 |
Correct |
83 ms |
174500 KB |
Output is correct |
30 |
Correct |
82 ms |
174604 KB |
Output is correct |
31 |
Correct |
85 ms |
174528 KB |
Output is correct |
32 |
Correct |
81 ms |
174592 KB |
Output is correct |
33 |
Correct |
81 ms |
174528 KB |
Output is correct |
34 |
Correct |
81 ms |
174500 KB |
Output is correct |
35 |
Correct |
82 ms |
174540 KB |
Output is correct |
36 |
Correct |
80 ms |
174332 KB |
Output is correct |
37 |
Correct |
84 ms |
174384 KB |
Output is correct |
38 |
Correct |
82 ms |
174572 KB |
Output is correct |
39 |
Correct |
90 ms |
174572 KB |
Output is correct |
40 |
Correct |
83 ms |
174540 KB |
Output is correct |
41 |
Incorrect |
80 ms |
174476 KB |
Output isn't correct |
42 |
Halted |
0 ms |
0 KB |
- |