This submission is migrated from previous version of oj.uz, which used different machine for grading. This submission may have different result if resubmitted.
#include<bits/stdc++.h>
using namespace std;
const long long inf = (long long) 1e18 + 10;
const int inf1 = (int) 1e9 + 10;
#define int long long
#define dbl long double
#define endl '\n'
#define sc second
#define fr first
#define mp make_pair
#define pb push_back
#define all(x) x.begin(), x.end()
const int maxn = 2e5+10;
int n, m, pp[maxn][25], pai[maxn], gr[maxn], h[maxn], s[maxn], t[maxn], st[maxn], ed[maxn];
vector<int> g[maxn], gts[maxn];
void dfslca(int u, int ant) {
pai[u] = ant;
pp[u][0] = ant;
for(int i = 1; i <= 20; i++) {
pp[u][i] = pp[pp[u][i-1]][i-1];
}
for(auto v : g[u]) if(v != ant) {
h[v] = h[u]+1;
dfslca(v,u);
}
}
int lca(int u, int v) {
if(h[u] < h[v]) swap(u,v);
for(int i = 20; i >= 0; i--) {
if(h[pp[u][i]] >= h[v]) {
u = pp[u][i];
}
}
if(u == v) return u;
for(int i = 20; i >= 0; i--) {
if(pp[u][i] != pp[v][i]) {
u = pp[u][i];
v = pp[v][i];
}
}
return pp[u][0];
}
void solve() {
cin >> n;
for(int i = 1; i <= n; i++) {
gr[i] = 0;
g[i].clear();
gts[i].clear();
st[i] = 0;
ed[i] = 0;
}
for(int i = 0; i < n-1; i++) {
int u,v; cin >> u >> v;
g[u].pb(v);
g[v].pb(u);
}
dfslca(1,1);
cin >> m;
for(int i = 1; i <= m; i++) {
cin >> s[i] >> t[i];
st[s[i]] = i;
ed[t[i]] = i;
}
for(int i = 1; i <= m; i++) {
int lc = lca(s[i],t[i]);
// cout << s[i] << " "<< t[i] << " " << lc << endl;
int v;
v = s[i];
while(true) {
if(st[v] != 0 && v != s[i]) {
gts[st[v]].pb(i);
gr[i]++;
}
if(ed[v] != 0 && v != t[i]) {
gts[i].pb(ed[v]);
gr[ed[v]]++;
}
if(v == lc) break;
v = pai[v];
}
v = t[i];
while(true) {
if(st[v] != 0 && v != s[i]) {
gts[st[v]].pb(i);
gr[i]++;
}
if(ed[v] != 0 && v != t[i]) {
gts[i].pb(ed[v]);
gr[ed[v]]++;
}
if(v == lc) break;
v = pai[v];
}
}
queue<int> q;
for(int i = 1; i <= m; i++) {
if(gr[i] == 0) q.push(i);
}
while(q.size()) {
int u = q.front();
q.pop();
for(auto v : gts[u]) {
if(--gr[v] == 0) q.push(v);
}
}
string ans = "Yes";
for(int i = 1; i <= m; i++) {
if(gr[i] != 0) ans = "No";
}
cout << ans << endl;
}
int32_t main() {
ios::sync_with_stdio(false); cin.tie(0);
// freopen("in.in", "r", stdin);
// freopen("out.out", "w", stdout);
int tt = 1;
cin >> tt;
while(tt--) {
solve();
}
}
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |