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 <iostream>
#include <algorithm>
#include <vector>
#include <cassert>
#include <cmath>
#include <set>
#include <map>
#include <deque>
#include <queue>
#include <iomanip>
using namespace std;
#define pb push_back
#define mp make_pair
#define sz(a) ((int) (a).size())
#define all(a) (a).begin(), (a).end()
using ll = long long;
const int N = 120121;
const int K = 20;
int n;
vector<int> g[N];
int m;
int s[N], t[N];
int up[N][K];
int tin[N], tout[N], ttt;
vector<int> g1[N];
int used[N];
bool bad;
void dfs(int v, int p) {
up[v][0] = p;
for (int i = 1; i < K; ++i) {
up[v][i] = up[up[v][i - 1]][i - 1];
}
tin[v] = ttt++;
for (int u : g[v]) {
if (u != p) dfs(u, v);
}
tout[v] = ttt;
}
bool anc(int v, int u) {
return tin[v] <= tin[u] && tout[v] >= tout[u];
}
int get_lca(int v, int u) {
if (anc(v, u)) return v;
if (anc(u, v)) return u;
for (int i = K - 1; i >= 0; --i) {
if (!anc(up[v][i], u)) {
v = up[v][i];
}
}
return up[v][0];
}
bool on_path(int a, int b, int v) {
int c = get_lca(a, b);
return anc(c, v) && (anc(v, a) || anc(v, b));
}
void dfs1(int v) {
used[v] = 1;
for (int u : g1[v]) {
if (used[u] == 1) bad = 1;
if (used[u] == 0) dfs1(u);
}
used[v] = 2;
}
void solve() {
cin >> n;
for (int i = 1; i <= n; ++i) {
g[i].clear();
}
for (int i = 0; i < n - 1; ++i) {
int a, b;
cin >> a >> b;
g[a].pb(b);
g[b].pb(a);
}
cin >> m;
for (int i = 0; i < m; ++i) {
cin >> s[i] >> t[i];
}
ttt = 0;
dfs(1, 1);
for (int i = 0; i < m; ++i) {
g1[i].clear();
}
for (int i = 0; i < m; ++i) {
for (int j = 0; j < m; ++j) {
if (i == j) continue;
if (on_path(s[i], t[i], s[j])) g1[j].pb(i);
if (on_path(s[i], t[i], t[j])) g1[i].pb(j);
}
}
for (int i = 0; i < m; ++i) {
used[i] = 0;
}
bad = 0;
for (int i = 0; i < m; ++i) {
if (used[i] == 0) dfs1(i);
}
cout << (bad ? "No" : "Yes") << "\n";
}
int main() {
ios::sync_with_stdio(false);
cin.tie(0);
int q;
cin >> q;
while (q--) {
solve();
}
return 0;
}
# | 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... |