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;
typedef long long ll;
typedef long double ld;
typedef pair<ll,ll> pl;
typedef pair<int,int> pii;
typedef tuple<int,int,int> tt;
#define all(a) a.begin(), a.end()
#define filter(a) a.erase(unique(all(a)), a.end())
const int mn = 1e5 + 5;
int up[mn][18], depth[mn], vis[mn];
vector<int> adj[mn], graph[mn];
void dfs (int u, int p, int d) {
up[u][0] = p, depth[u] = d;
for (int i = 1; i < 18; i++)
up[u][i] = up[up[u][i - 1]][i - 1];
for (int v : adj[u])
if (v != p) dfs(v, u, d + 1);
}
int goUp (int a, int k) {
for (int i = 0; i < 18; i++)
if (k & (1 << i)) a = up[a][i];
return a;
}
int lca (int a, int b) {
if (depth[a] < depth[b]) swap(a, b);
a = goUp(a, depth[a] - depth[b]);
if (a == b) return a;
for (int i = 17; i >= 0; i--)
if (up[a][i] != up[b][i]) a = up[a][i], b = up[b][i];
return up[a][0];
}
int distance (int a, int b) {
return depth[a] + depth[b] - 2 * depth[lca(a, b)];
}
bool inPath (int node, int a, int b) {
return distance(a, node) + distance(node, b) == distance(a, b);
}
bool detectCycle (int u) {
if (vis[u] == 1) return 1;
if (vis[u] == 2) return 0;
vis[u] = 1;
for (int v : graph[u])
if (detectCycle(v)) return vis[u] = 2, 1;
vis[u] = 2;
return 0;
}
void solve() {
int n; cin >> n;
for (int i = 1; i <= n; i++) adj[i].clear();
for (int i = 1; i < n; i++) {
int a, b; cin >> a >> b;
adj[a].push_back(b);
adj[b].push_back(a);
}
dfs(1, 1, 1);
int m; cin >> m;
for (int i = 0; i < m; i++) {
graph[i].clear();
vis[i] = 0;
}
vector<pii> paths(m);
for (int i = 0; i < m; i++)
cin >> paths[i].first >> paths[i].second;
for (int i = 0; i < m; i++) {
vis[i] = 0;
for (int j = 0; j < m; j++) {
if (i == j) continue;
if (inPath(paths[i].first, paths[j].first, paths[j].second) &&
inPath(paths[i].second, paths[j].first, paths[j].second)) return cout << "No\n", void();
if (!inPath(paths[j].first, paths[i].first, paths[i].second) &&
!inPath(paths[i].second, paths[j].first, paths[j].second)) graph[i].push_back(j);
}
}
for (int i = 0; i < m; i++)
if (!vis[i] && detectCycle(i)) return cout << "No\n", void();
cout << "Yes\n";
}
int main()
{
ios::sync_with_stdio(0);
cin.tie(0);
int TC; cin >> TC;
while (TC--) 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... |