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;
using ll = long long;
const int MAXN = 120'001;
vector<int> g[MAXN], g2[MAXN];
int tin[MAXN], tout[MAXN], s[MAXN], e[MAXN], par[MAXN], col[MAXN], hv[MAXN], timer;
bool ok;
void dfs(int u, int p = -1) {
tin[u] = timer++;
par[u] = p;
for (int v : g[u]) {
if (v != p) dfs(v, u);
}
tout[u] = timer++;
}
void dfs2(int u) {
col[u] = 1;
for (int v : g2[u]) {
if (col[v] == 0) dfs2(v);
else if (col[v] == 1) ok = false;
}
col[u] = 2;
}
bool is_ancestor(int u, int v) {
return tin[u] <= tin[v] && tout[v] <= tout[u];
}
void solve() {
int n; cin >> n;
ok = true;
fill(s, s+n+1, 0);
fill(e, e+n+1, 0);
fill(col, col+n+1, 0);
fill(hv, hv+n+1, 0);
timer = 1;
for (int i = 1; i <= n; i++) {
g[i].clear();
g2[i].clear();
}
for (int i = 0; i < n-1; i++) {
int u, v; cin >> u >> v;
g[u].emplace_back(v);
g[v].emplace_back(u);
}
dfs(1);
int m; cin >> m;
vector<pair<int, int>> edges(m);
for (int i = 0; i < m; i++) {
cin >> edges[i].first >> edges[i].second;
}
sort(edges.begin(), edges.end());
for (int i = 1; i < m; i++) {
ok &= edges[i-1].second < edges[i].second;
}
cout << (ok?"Yes\n":"No\n");
return;
auto calc = [&](int u, int v, vector<int> &indices, bool add_lca) {
while (!is_ancestor(u, v)) {
indices.emplace_back(u);
u = par[u];
}
if (add_lca) indices.emplace_back(u);
};
vector<int> cnt(m+1, 0);
for (int i = 1; i <= m; i++) {
vector<int> indices;
{
auto [u, v] = edges[i];
calc(u, v, indices, false);
calc(v, u, indices, true);
}
vector<int> to_rem;
for (int u : indices) {
if (e[u] && e[u] != i) {
to_rem.emplace_back(e[u]);
cnt[e[u]]++;
g2[i].emplace_back(e[u]);
}
if (s[u] && s[u] != i) {
to_rem.emplace_back(s[u]);
cnt[s[u]]++;
g2[s[u]].emplace_back(i);
}
}
for (int i : to_rem) {
if (cnt[i] == 2) ok = false;
else cnt[i] = 0;
}
}
for (int i = 1; i <= m; i++) {
if (col[i] == 0) dfs2(i);
}
cout << (ok?"Yes\n":"No\n");
}
int main() {
ios::sync_with_stdio(0); cin.tie(0);
int t; cin >> t;
while (t--) {
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... |