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;
#ifdef Nero
#include "Deb.h"
#else
#define debug(...)
#endif
const int LOG = 18;
const int N = 120005;
int dep[N];
int vis[N];
int pr[LOG][N];
int a[N], b[N];
vector<int> g[N], t[N];
void dfs(int v, int p) {
for (int u : g[v]) {
if (u == p) continue;
dep[u] = dep[v] + 1;
pr[0][u] = v;
for (int j = 1; j < LOG; ++j) {
pr[j][u] = pr[j - 1][pr[j - 1][u]];
}
dfs(u, v);
}
}
int lca(int u, int v) {
if (dep[u] < dep[v]) swap(u, v);
for (int j = 0; j < LOG; ++j) {
if ((dep[u] - dep[v]) >> j & 1) {
u = pr[j][u];
}
}
if (u == v) return v;
for (int j = LOG - 1; j >= 0; --j) {
if (pr[j][u] != pr[j][v]) {
u = pr[j][u], v = pr[j][v];
}
}
return pr[0][v];
}
int onPath(int x, int y, int z) {
int lc1 = lca(x, y);
int lc2 = lca(x, z);
int lc3 = lca(y, z);
return (lc1 ^ lc2 ^ lc3) == z;
}
bool dfs2(int v) {
if (vis[v] == 1) return true;
if (vis[v] == 2) return false;
vis[v] = 1;
bool ret = false;
for (int u : t[v]) {
ret |= dfs2(u);
}
vis[v] = 2;
return ret;
}
int main() {
ios::sync_with_stdio(false);
cin.tie(nullptr);
int tt;
cin >> tt;
while (tt--) {
int n;
cin >> n;
for(int i = 0; i < n - 1; ++i) {
int u, v;
cin >> u >> v;
g[u].push_back(v);
g[v].push_back(u);
}
int m;
cin >> m;
for (int i = 1; i <= m; ++i) {
cin >> a[i] >> b[i];
}
dfs(1, 0);
for (int i = 1; i <= m; ++i) {
for (int j = 1; j <= m; ++j) {
if (i == j) continue;
if (onPath(a[i], b[i], a[j])) {
t[j].push_back(i);
}
if (onPath(a[i], b[i], b[j])) {
t[i].push_back(j);
}
}
}
bool cycle = false;
for (int i = 1; i <= m; ++i) {
cycle |= dfs2(i);
}
cout << (cycle ? "No" : "Yes") << '\n';
fill(vis + 1, vis + 1 + m, 0);
fill(t + 1, t + 1 + m, vector<int>());
fill(g + 1, g + 1 + n, vector<int>());
}
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... |