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 <vector>
using namespace std;
int n, m, sz, c;
bool ans;
vector <vector <int>> g(2e5);
vector <int> to(2e5, 0);
vector <bool> del(2e5);
void reset() {
ans = true;
for (int i = 1; i <= n; i++) {
g[i].clear();
to[i] = 0;
del[i] = false;
}
}
int get_c(int x) {
if (del[x]) return 0;
del[x] = true;
int mx = 0, sum = 1;
for (int i : g[x]) {
}
return sum;
}
void cd(int x) {
sz = get_c(x);
get_c(x);
}
bool dfs(int x, int y) {
if (del[x] || to[x]) return false;
if (x == y) return true;
del[x] = true;
bool ret = false;
for (int i : g[x]) if (dfs(i, y)) ret = true;
del[x] = false;
return ret;
}
bool bt() {
bool kesz = true;
for (int i = 1; i <= n; i++) if (to[i] && to[i] != i) kesz = false;
if (kesz) return true;
for (int i = 1; i <= n; i++) {
if (!to[i] || to[i] == i) continue;
int a = to[i];
to[i] = 0;
bool jo = dfs(i, a);
to[i] = a;
if (!jo) continue;
swap(to[i], to[a]);
if (bt()) return true;
swap(to[i], to[a]);
}
return false;
}
void solve() {
cin >> n;
reset();
int a, b;
for (int i = 0; i < n - 1; i++) {
cin >> a >> b;
g[a].push_back(b);
g[b].push_back(a);
}
cin >> m;
for (int i = 0; i < m; i++) {
cin >> a >> b;
to[a] = b;
}
cout << (bt() ? "Yes\n" : "No\n");
}
int main() {
ios_base::sync_with_stdio(false);
cin.tie(NULL);
int t;
cin >> t;
while (t--) solve();
}
Compilation message (stderr)
jail.cpp: In function 'int get_c(int)':
jail.cpp:24:11: warning: unused variable 'i' [-Wunused-variable]
24 | for (int i : g[x]) {
| ^
jail.cpp:23:6: warning: unused variable 'mx' [-Wunused-variable]
23 | int mx = 0, sum = 1;
| ^~
# | 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... |