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;
#define int long long
#define fr first
#define sc second
#define eb emplace_back
#define nl "\n"
/* const int logn = 20; */
/* int anc[N][logn + 1], depth[N]; */
/* void dfs(int cur = 1, int par = 0) { */
/* depth[cur] = depth[par] + 1; */
/* anc[cur][0] = par; */
/* for(auto u : adj[cur]) { */
/* if(u == par) continue; */
/* dfs(u, cur); */
/* } */
/* } */
/* void precompute() { */
/* dfs(); */
/* for(int i = 1; i <= logn; ++i) { */
/* for(int j = 1; j <= n; ++j) { */
/* if(depth[j] <= (1LL << i)) */
/* anc[i][j] = 0; */
/* else */
/* anc[i][j] = anc[anc[i][j - 1]][j - 1]; */
/* } */
/* } */
/* } */
/* int lca(int a, int b) { */
/* if(a == b) return a; */
/* if(depth[a] > depth[b]) swap(a, b); */
/* for(int j = logn; j >= 0; --j) { */
/* if(anc[b][j] != 0 && depth[anc[b][j]] >= depth[a]) */
/* b = anc[b][j]; */
/* } */
/* if(a == b) return a; */
/* for(int j = logn; j >= 0; --j) { */
/* if(anc[a][j] != 0 && and[a][j] != anc[b][j]) { */
/* a = anc[a][j]; */
/* b = anc[b][j]; */
/* } */
/* } */
/* return anc[a][0]; */
/* } */
/* void mod(int a, int b) { */
/* int l = lca(a, b); */
/* } */
const int N = 120001;
int n, m;
vector<int> adj[N];
int s[N], t[N];
int from[N];
int use[N];
void dfs(int cur, int par) {
from[cur] = par;
for(auto u : adj[cur]) {
if(u != par)
dfs(u, cur);
}
}
void mod(int a, int b, int v = 1) {
dfs(a, 0);
while(b != 0) {
use[b] += v;
b = from[b];
}
}
void solve() {
cin >> n;
for(int i = 1; i <= n; ++i) {
use[i] = 0;
adj[i].clear();
}
for(int i = 1; i < n; ++i) {
int a, b; cin >> a >> b;
adj[a].eb(b); adj[b].eb(a);
}
cin >> m;
for(int i = 1; i <= m; ++i) {
cin >> s[i] >> t[i];
}
for(int i = 1; i <= m; ++i) {
mod(s[i], t[i]);
}
set<pair<int, int>> st;
for(int i = 1; i <= m; ++i) {
st.insert({use[t[i]], i});
}
while(!st.empty()) {
int a = (*st.begin()).sc;
if(use[t[a]] > 1) {
cout << "No" << nl;
return;
}
mod(s[a], t[a], -1);
st.erase(st.begin());
}
cout << "Yes" << nl;
}
signed main() {
ios::sync_with_stdio(0);
cin.tie(0);
int t = 1;
cin >> t;
for(int i = 1; i <= t; ++i) {
/* cout << "Case #" << i << nl; */
solve();
/* cout << nl; */
}
}
# | 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... |