// ~ 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);
if(c == u)
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)
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 |
82 ms |
174368 KB |
Output is correct |
2 |
Correct |
80 ms |
174412 KB |
Output is correct |
3 |
Correct |
78 ms |
174336 KB |
Output is correct |
4 |
Correct |
106 ms |
174668 KB |
Output is correct |
5 |
Correct |
135 ms |
174760 KB |
Output is correct |
6 |
Correct |
83 ms |
174616 KB |
Output is correct |
7 |
Correct |
84 ms |
174668 KB |
Output is correct |
8 |
Correct |
83 ms |
174668 KB |
Output is correct |
9 |
Correct |
390 ms |
182076 KB |
Output is correct |
10 |
Correct |
1078 ms |
352872 KB |
Output is correct |
11 |
Correct |
88 ms |
174500 KB |
Output is correct |
12 |
Correct |
151 ms |
175416 KB |
Output is correct |
13 |
Correct |
1010 ms |
360208 KB |
Output is correct |
14 |
Correct |
971 ms |
360052 KB |
Output is correct |
15 |
Correct |
1388 ms |
385580 KB |
Output is correct |
16 |
Correct |
1851 ms |
437688 KB |
Output is correct |
17 |
Correct |
1234 ms |
363920 KB |
Output is correct |
18 |
Correct |
1027 ms |
364408 KB |
Output is correct |
19 |
Correct |
1172 ms |
364036 KB |
Output is correct |
20 |
Correct |
1194 ms |
363852 KB |
Output is correct |
21 |
Correct |
1215 ms |
380072 KB |
Output is correct |
22 |
Correct |
842 ms |
359104 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
84 ms |
174312 KB |
Output is correct |
2 |
Correct |
88 ms |
174296 KB |
Output is correct |
3 |
Correct |
84 ms |
174600 KB |
Output is correct |
4 |
Correct |
85 ms |
174576 KB |
Output is correct |
5 |
Correct |
96 ms |
174560 KB |
Output is correct |
6 |
Correct |
88 ms |
174488 KB |
Output is correct |
7 |
Correct |
81 ms |
174596 KB |
Output is correct |
8 |
Correct |
82 ms |
174516 KB |
Output is correct |
9 |
Correct |
89 ms |
174540 KB |
Output is correct |
10 |
Correct |
83 ms |
174484 KB |
Output is correct |
11 |
Correct |
89 ms |
174540 KB |
Output is correct |
12 |
Incorrect |
79 ms |
174436 KB |
Output isn't correct |
13 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
84 ms |
174312 KB |
Output is correct |
2 |
Correct |
88 ms |
174296 KB |
Output is correct |
3 |
Correct |
84 ms |
174600 KB |
Output is correct |
4 |
Correct |
85 ms |
174576 KB |
Output is correct |
5 |
Correct |
96 ms |
174560 KB |
Output is correct |
6 |
Correct |
88 ms |
174488 KB |
Output is correct |
7 |
Correct |
81 ms |
174596 KB |
Output is correct |
8 |
Correct |
82 ms |
174516 KB |
Output is correct |
9 |
Correct |
89 ms |
174540 KB |
Output is correct |
10 |
Correct |
83 ms |
174484 KB |
Output is correct |
11 |
Correct |
89 ms |
174540 KB |
Output is correct |
12 |
Incorrect |
79 ms |
174436 KB |
Output isn't correct |
13 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
84 ms |
174312 KB |
Output is correct |
2 |
Correct |
88 ms |
174296 KB |
Output is correct |
3 |
Correct |
84 ms |
174600 KB |
Output is correct |
4 |
Correct |
85 ms |
174576 KB |
Output is correct |
5 |
Correct |
96 ms |
174560 KB |
Output is correct |
6 |
Correct |
88 ms |
174488 KB |
Output is correct |
7 |
Correct |
81 ms |
174596 KB |
Output is correct |
8 |
Correct |
82 ms |
174516 KB |
Output is correct |
9 |
Correct |
89 ms |
174540 KB |
Output is correct |
10 |
Correct |
83 ms |
174484 KB |
Output is correct |
11 |
Correct |
89 ms |
174540 KB |
Output is correct |
12 |
Incorrect |
79 ms |
174436 KB |
Output isn't correct |
13 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
84 ms |
174312 KB |
Output is correct |
2 |
Correct |
88 ms |
174296 KB |
Output is correct |
3 |
Correct |
84 ms |
174600 KB |
Output is correct |
4 |
Correct |
85 ms |
174576 KB |
Output is correct |
5 |
Correct |
96 ms |
174560 KB |
Output is correct |
6 |
Correct |
88 ms |
174488 KB |
Output is correct |
7 |
Correct |
81 ms |
174596 KB |
Output is correct |
8 |
Correct |
82 ms |
174516 KB |
Output is correct |
9 |
Correct |
89 ms |
174540 KB |
Output is correct |
10 |
Correct |
83 ms |
174484 KB |
Output is correct |
11 |
Correct |
89 ms |
174540 KB |
Output is correct |
12 |
Incorrect |
79 ms |
174436 KB |
Output isn't correct |
13 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
93 ms |
174400 KB |
Output is correct |
2 |
Correct |
79 ms |
174388 KB |
Output is correct |
3 |
Correct |
84 ms |
174332 KB |
Output is correct |
4 |
Correct |
94 ms |
174408 KB |
Output is correct |
5 |
Correct |
95 ms |
174340 KB |
Output is correct |
6 |
Incorrect |
81 ms |
174400 KB |
Output isn't correct |
7 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
82 ms |
174368 KB |
Output is correct |
2 |
Correct |
80 ms |
174412 KB |
Output is correct |
3 |
Correct |
78 ms |
174336 KB |
Output is correct |
4 |
Correct |
106 ms |
174668 KB |
Output is correct |
5 |
Correct |
135 ms |
174760 KB |
Output is correct |
6 |
Correct |
83 ms |
174616 KB |
Output is correct |
7 |
Correct |
84 ms |
174668 KB |
Output is correct |
8 |
Correct |
83 ms |
174668 KB |
Output is correct |
9 |
Correct |
390 ms |
182076 KB |
Output is correct |
10 |
Correct |
1078 ms |
352872 KB |
Output is correct |
11 |
Correct |
88 ms |
174500 KB |
Output is correct |
12 |
Correct |
151 ms |
175416 KB |
Output is correct |
13 |
Correct |
1010 ms |
360208 KB |
Output is correct |
14 |
Correct |
971 ms |
360052 KB |
Output is correct |
15 |
Correct |
1388 ms |
385580 KB |
Output is correct |
16 |
Correct |
1851 ms |
437688 KB |
Output is correct |
17 |
Correct |
1234 ms |
363920 KB |
Output is correct |
18 |
Correct |
1027 ms |
364408 KB |
Output is correct |
19 |
Correct |
1172 ms |
364036 KB |
Output is correct |
20 |
Correct |
1194 ms |
363852 KB |
Output is correct |
21 |
Correct |
1215 ms |
380072 KB |
Output is correct |
22 |
Correct |
842 ms |
359104 KB |
Output is correct |
23 |
Correct |
84 ms |
174312 KB |
Output is correct |
24 |
Correct |
88 ms |
174296 KB |
Output is correct |
25 |
Correct |
84 ms |
174600 KB |
Output is correct |
26 |
Correct |
85 ms |
174576 KB |
Output is correct |
27 |
Correct |
96 ms |
174560 KB |
Output is correct |
28 |
Correct |
88 ms |
174488 KB |
Output is correct |
29 |
Correct |
81 ms |
174596 KB |
Output is correct |
30 |
Correct |
82 ms |
174516 KB |
Output is correct |
31 |
Correct |
89 ms |
174540 KB |
Output is correct |
32 |
Correct |
83 ms |
174484 KB |
Output is correct |
33 |
Correct |
89 ms |
174540 KB |
Output is correct |
34 |
Incorrect |
79 ms |
174436 KB |
Output isn't correct |
35 |
Halted |
0 ms |
0 KB |
- |