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;
const int N = 120005;
int n, m, p[N], s[N], t[N], used[N];
vector <int> g[N], path[N], prisoner[N];
void dfs(int v) {
for (auto i: g[v]) {
if (i != p[v]) {
p[i] = v;
dfs(i);
}
}
}
bool find_cycle(int v) {
used[v] = 1;
for (auto i: prisoner[v]) {
if (used[i] == 1) return true;
if (!used[i] and find_cycle(i)) return true;
}
used[v] = 2;
return false;
}
void solve() {
cin >> n;
for (int i = 1; i <= n; i ++) {
g[i].clear();
path[i].clear();
prisoner[i].clear();
}
for (int i = 1; i < n; i ++) {
int a, b;
cin >> a >> b;
g[a].push_back(b);
g[b].push_back(a);
}
cin >> m;
for (int i = 0; i < m; i ++) {
cin >> s[i] >> t[i];
p[s[i]] = 0;
dfs(s[i]);
for (int j = t[i]; j; j = p[j]) {
path[i].push_back(j);
}
for (int j = 0; j < i; j ++) {
int f1 = 1, f2 = 1;
for (auto k: path[i]) {
if (k == s[j]) f1 = 0;
if (k == t[j]) f2 = 0;
}
for (auto k: path[j]) {
if (k == t[i]) f1 = 0;
if (k == s[i]) f2 = 0;
}
if (!f1 and !f2) {
cout << "No\n";
return;
}
if (f1 and !f2) {
prisoner[i].push_back(j);
}
if (!f1 and f2) {
prisoner[j].push_back(i);
}
}
}
memset(used, 0, sizeof(used)); /*
for (int i = 0; i < m; i ++) {
cout << i << ":";
for (auto j: prisoner[i]) {
cout << " " << j;
}
cout << endl;
}*/
for (int i = 0; i < m; i ++) {
if (!used[i] and find_cycle(i)) {
cout << "No\n";
return;
}
}
cout << "Yes\n";
}
int main() {
ios_base::sync_with_stdio(0);
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... |