//by szh
#include<bits/stdc++.h>
using namespace std;
#define fi first
#define se second
#define pii pair<int,int>
#define pll pair<long long,long long>
#define pb push_back
#define debug(x) cerr<<#x<<"="<<x<<endl
#define pq priority_queue
#define inf 0x3f
#define rep(i,a,b) for (int i=a;i<(b);i++)
#define MP make_pair
#define SZ(x) (int(x.size()))
#define ll long long
#define mod 1000000007
#define ALL(x) x.begin(),x.end()
void inc(int &a,int b) {a=(a+b)%mod;}
void dec(int &a,int b) {a=(a-b+mod)%mod;}
int lowbit(int x) {return x&(-x);}
ll p0w(ll base,ll p) {ll ret=1;while(p>0){if (p%2ll==1ll) ret=ret*base%mod;base=base*base%mod;p/=2ll;}return ret;}
const int maxn = 2e5+10;
int n,m;
vector <int> edge[maxn];
vector <int> ne[maxn + maxn*17*2];//[0,m): paths, [m,m+n): starts at i, [m+n,m+n+n): ends at i
int fa[maxn][17],dep[maxn];
pii path[maxn];
int deg[maxn + maxn*17*2];
void adde(int u,int v) {
// if (u<m or v<m) debug(u),debug(v);
ne[u].pb(v);
deg[v]++;
}
void dfs(int u,int lst) {
fa[u][0] = lst;
dep[u] = lst==-1?0:dep[lst]+1;
rep(i,1,17) {
if (fa[u][i-1]==-1) break;
adde(m+u*17+i-1,m+u*17+i);
adde(m+n*17+u*17+i,m+n*17+u*17+i-1);
adde(m+fa[u][i-1]*17+i-1,m+u*17+i);
adde(m+n*17+u*17+i,m+n*17+fa[u][i-1]*17+i-1);
fa[u][i] = fa[fa[u][i-1]][i-1];
}
for (int v:edge[u]) {
if (v==lst) continue;
dfs(v,u);
}
}
void update(int pathid,int u,int v) { //exclude u,v
bool uu=false,vv=false;
if (dep[u]>dep[v]) uu=true,u = fa[u][0];
else if (dep[u]<dep[v]) vv=true,v=fa[v][0];
if (dep[u]<dep[v]) swap(u,v),swap(uu,vv);
for (int i=16;i>=0;i--) {
if (dep[u] - (1<<i) >= dep[v]) {
adde(m+u*17+i,pathid);
adde(pathid,m+n*17+u*17+i);
u = fa[u][i];
}
}
if (u==v) return;
if (vv==false) v=fa[v][0];
if (uu==false) u=fa[u][0];
if (dep[u]>dep[v]) adde(m+u*17,pathid),adde(pathid,m+n*17+u*17),u=fa[u][0];
if (dep[v]>dep[u]) adde(m+v*17,pathid),adde(pathid,m+n*17+v*17),v=fa[v][0];
for (int i=16;i>=0;i--) if (fa[u][i]!=fa[v][i]) {
adde(m+u*17+i,pathid);
adde(pathid,m+n*17+u*17+i);
u = fa[u][i];
adde(m+v*17+i,pathid);
adde(pathid,m+n*17+v*17+i);
v = fa[v][i];
}
adde(m+u*17,pathid);
adde(pathid,m+n*17+u*17);
adde(m+v*17+1,pathid);
adde(pathid,m+n*17+v*17+1);
}
void solve() {
cin>>n;
rep(i,0,n)
rep(j,0,17) fa[i][j]=-1;
rep(i,1,n) {
int u,v;
cin>>u>>v;
u--,v--;
edge[u].pb(v);
edge[v].pb(u);
}
cin>>m;dfs(0,-1);
rep(i,0,m) {
cin>>path[i].fi>>path[i].se;
path[i].fi--,path[i].se--;
adde(i,m+path[i].fi*17);
adde(m+n*17+path[i].se*17,i);
update(i,path[i].fi,path[i].se);
adde(m+path[i].se*17,i);
adde(i,m+n*17+path[i].fi*17);
}
queue <int> q;
rep(i,0,m+n*17*2) if (deg[i]==0) q.push(i);
while (!q.empty()) {
int u = q.front();
q.pop();
for (int v:ne[u]) {
deg[v]--;
if (deg[v]==0)q.push(v);
}
}
bool ok = true;
rep(i,0,m+n*17*2) {
if (deg[i]!=0) ok=false,deg[i]=0;
while (!ne[i].empty()) ne[i].pop_back();
if (i<n) while (!edge[i].empty()) edge[i].pop_back();
}
if (ok) cout<<"Yes\n";
else cout<<"No\n";
return;
}
int main() {
// freopen("input.txt","r",stdin);
std::ios::sync_with_stdio(false);cin.tie(0);
int _;cin>>_;
while (_--) solve();
return 0;
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
82 ms |
169340 KB |
Output is correct |
2 |
Correct |
86 ms |
169320 KB |
Output is correct |
3 |
Correct |
79 ms |
169404 KB |
Output is correct |
4 |
Correct |
123 ms |
169872 KB |
Output is correct |
5 |
Correct |
186 ms |
170272 KB |
Output is correct |
6 |
Correct |
88 ms |
169548 KB |
Output is correct |
7 |
Correct |
83 ms |
169672 KB |
Output is correct |
8 |
Correct |
85 ms |
169672 KB |
Output is correct |
9 |
Correct |
377 ms |
177720 KB |
Output is correct |
10 |
Correct |
1063 ms |
331772 KB |
Output is correct |
11 |
Correct |
98 ms |
169548 KB |
Output is correct |
12 |
Correct |
179 ms |
170584 KB |
Output is correct |
13 |
Correct |
1196 ms |
336324 KB |
Output is correct |
14 |
Correct |
961 ms |
336224 KB |
Output is correct |
15 |
Correct |
986 ms |
338252 KB |
Output is correct |
16 |
Correct |
1282 ms |
347412 KB |
Output is correct |
17 |
Correct |
1523 ms |
339408 KB |
Output is correct |
18 |
Correct |
1277 ms |
341692 KB |
Output is correct |
19 |
Correct |
1391 ms |
339468 KB |
Output is correct |
20 |
Correct |
1139 ms |
339464 KB |
Output is correct |
21 |
Correct |
952 ms |
339684 KB |
Output is correct |
22 |
Correct |
967 ms |
336548 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
80 ms |
169428 KB |
Output is correct |
2 |
Correct |
92 ms |
169424 KB |
Output is correct |
3 |
Correct |
90 ms |
169560 KB |
Output is correct |
4 |
Correct |
87 ms |
169628 KB |
Output is correct |
5 |
Correct |
94 ms |
169548 KB |
Output is correct |
6 |
Correct |
89 ms |
169648 KB |
Output is correct |
7 |
Correct |
92 ms |
169540 KB |
Output is correct |
8 |
Correct |
86 ms |
169568 KB |
Output is correct |
9 |
Correct |
88 ms |
169544 KB |
Output is correct |
10 |
Correct |
92 ms |
169548 KB |
Output is correct |
11 |
Correct |
86 ms |
169544 KB |
Output is correct |
12 |
Correct |
81 ms |
169452 KB |
Output is correct |
13 |
Correct |
85 ms |
169572 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
80 ms |
169428 KB |
Output is correct |
2 |
Correct |
92 ms |
169424 KB |
Output is correct |
3 |
Correct |
90 ms |
169560 KB |
Output is correct |
4 |
Correct |
87 ms |
169628 KB |
Output is correct |
5 |
Correct |
94 ms |
169548 KB |
Output is correct |
6 |
Correct |
89 ms |
169648 KB |
Output is correct |
7 |
Correct |
92 ms |
169540 KB |
Output is correct |
8 |
Correct |
86 ms |
169568 KB |
Output is correct |
9 |
Correct |
88 ms |
169544 KB |
Output is correct |
10 |
Correct |
92 ms |
169548 KB |
Output is correct |
11 |
Correct |
86 ms |
169544 KB |
Output is correct |
12 |
Correct |
81 ms |
169452 KB |
Output is correct |
13 |
Correct |
85 ms |
169572 KB |
Output is correct |
14 |
Correct |
79 ms |
169404 KB |
Output is correct |
15 |
Correct |
81 ms |
169400 KB |
Output is correct |
16 |
Correct |
87 ms |
169652 KB |
Output is correct |
17 |
Correct |
100 ms |
169700 KB |
Output is correct |
18 |
Correct |
97 ms |
169660 KB |
Output is correct |
19 |
Incorrect |
84 ms |
169392 KB |
Output isn't correct |
20 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
80 ms |
169428 KB |
Output is correct |
2 |
Correct |
92 ms |
169424 KB |
Output is correct |
3 |
Correct |
90 ms |
169560 KB |
Output is correct |
4 |
Correct |
87 ms |
169628 KB |
Output is correct |
5 |
Correct |
94 ms |
169548 KB |
Output is correct |
6 |
Correct |
89 ms |
169648 KB |
Output is correct |
7 |
Correct |
92 ms |
169540 KB |
Output is correct |
8 |
Correct |
86 ms |
169568 KB |
Output is correct |
9 |
Correct |
88 ms |
169544 KB |
Output is correct |
10 |
Correct |
92 ms |
169548 KB |
Output is correct |
11 |
Correct |
86 ms |
169544 KB |
Output is correct |
12 |
Correct |
81 ms |
169452 KB |
Output is correct |
13 |
Correct |
85 ms |
169572 KB |
Output is correct |
14 |
Correct |
79 ms |
169404 KB |
Output is correct |
15 |
Correct |
81 ms |
169400 KB |
Output is correct |
16 |
Correct |
87 ms |
169652 KB |
Output is correct |
17 |
Correct |
100 ms |
169700 KB |
Output is correct |
18 |
Correct |
97 ms |
169660 KB |
Output is correct |
19 |
Incorrect |
84 ms |
169392 KB |
Output isn't correct |
20 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
80 ms |
169428 KB |
Output is correct |
2 |
Correct |
92 ms |
169424 KB |
Output is correct |
3 |
Correct |
90 ms |
169560 KB |
Output is correct |
4 |
Correct |
87 ms |
169628 KB |
Output is correct |
5 |
Correct |
94 ms |
169548 KB |
Output is correct |
6 |
Correct |
89 ms |
169648 KB |
Output is correct |
7 |
Correct |
92 ms |
169540 KB |
Output is correct |
8 |
Correct |
86 ms |
169568 KB |
Output is correct |
9 |
Correct |
88 ms |
169544 KB |
Output is correct |
10 |
Correct |
92 ms |
169548 KB |
Output is correct |
11 |
Correct |
86 ms |
169544 KB |
Output is correct |
12 |
Correct |
81 ms |
169452 KB |
Output is correct |
13 |
Correct |
85 ms |
169572 KB |
Output is correct |
14 |
Correct |
79 ms |
169404 KB |
Output is correct |
15 |
Correct |
81 ms |
169400 KB |
Output is correct |
16 |
Correct |
87 ms |
169652 KB |
Output is correct |
17 |
Correct |
100 ms |
169700 KB |
Output is correct |
18 |
Correct |
97 ms |
169660 KB |
Output is correct |
19 |
Incorrect |
84 ms |
169392 KB |
Output isn't correct |
20 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
82 ms |
169424 KB |
Output is correct |
2 |
Correct |
87 ms |
169420 KB |
Output is correct |
3 |
Correct |
91 ms |
169368 KB |
Output is correct |
4 |
Correct |
90 ms |
169532 KB |
Output is correct |
5 |
Correct |
103 ms |
169536 KB |
Output is correct |
6 |
Correct |
87 ms |
169468 KB |
Output is correct |
7 |
Correct |
83 ms |
169504 KB |
Output is correct |
8 |
Incorrect |
83 ms |
169336 KB |
Output isn't correct |
9 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
82 ms |
169340 KB |
Output is correct |
2 |
Correct |
86 ms |
169320 KB |
Output is correct |
3 |
Correct |
79 ms |
169404 KB |
Output is correct |
4 |
Correct |
123 ms |
169872 KB |
Output is correct |
5 |
Correct |
186 ms |
170272 KB |
Output is correct |
6 |
Correct |
88 ms |
169548 KB |
Output is correct |
7 |
Correct |
83 ms |
169672 KB |
Output is correct |
8 |
Correct |
85 ms |
169672 KB |
Output is correct |
9 |
Correct |
377 ms |
177720 KB |
Output is correct |
10 |
Correct |
1063 ms |
331772 KB |
Output is correct |
11 |
Correct |
98 ms |
169548 KB |
Output is correct |
12 |
Correct |
179 ms |
170584 KB |
Output is correct |
13 |
Correct |
1196 ms |
336324 KB |
Output is correct |
14 |
Correct |
961 ms |
336224 KB |
Output is correct |
15 |
Correct |
986 ms |
338252 KB |
Output is correct |
16 |
Correct |
1282 ms |
347412 KB |
Output is correct |
17 |
Correct |
1523 ms |
339408 KB |
Output is correct |
18 |
Correct |
1277 ms |
341692 KB |
Output is correct |
19 |
Correct |
1391 ms |
339468 KB |
Output is correct |
20 |
Correct |
1139 ms |
339464 KB |
Output is correct |
21 |
Correct |
952 ms |
339684 KB |
Output is correct |
22 |
Correct |
967 ms |
336548 KB |
Output is correct |
23 |
Correct |
80 ms |
169428 KB |
Output is correct |
24 |
Correct |
92 ms |
169424 KB |
Output is correct |
25 |
Correct |
90 ms |
169560 KB |
Output is correct |
26 |
Correct |
87 ms |
169628 KB |
Output is correct |
27 |
Correct |
94 ms |
169548 KB |
Output is correct |
28 |
Correct |
89 ms |
169648 KB |
Output is correct |
29 |
Correct |
92 ms |
169540 KB |
Output is correct |
30 |
Correct |
86 ms |
169568 KB |
Output is correct |
31 |
Correct |
88 ms |
169544 KB |
Output is correct |
32 |
Correct |
92 ms |
169548 KB |
Output is correct |
33 |
Correct |
86 ms |
169544 KB |
Output is correct |
34 |
Correct |
81 ms |
169452 KB |
Output is correct |
35 |
Correct |
85 ms |
169572 KB |
Output is correct |
36 |
Correct |
79 ms |
169404 KB |
Output is correct |
37 |
Correct |
81 ms |
169400 KB |
Output is correct |
38 |
Correct |
87 ms |
169652 KB |
Output is correct |
39 |
Correct |
100 ms |
169700 KB |
Output is correct |
40 |
Correct |
97 ms |
169660 KB |
Output is correct |
41 |
Incorrect |
84 ms |
169392 KB |
Output isn't correct |
42 |
Halted |
0 ms |
0 KB |
- |