#include<bits/stdc++.h>
using namespace std;
const int LG = 18;
const int N = 1.2e5;
int n;
int bp[LG][N];
vector<int> g[N * LG];
vector<int> tr[N];
int tin[N], tout[N];
int tim = 0;
void build(int u, int p) {
tin[u] = tim++;
bp[0][u] = p;
for (int k = 1; bp[k - 1][u] != -1; ++k) {
bp[k][u] = bp[k - 1][bp[k - 1][u]];
g[u + n * k].push_back(u + n * (k - 1));
g[u + n * k].push_back(bp[k - 1][u] + n * (k - 1));
}
for (auto to: tr[u]) {
if (to != p) build(to, u);
}
tout[u] = tim++;
}
bool is_par(int u, int v) {
return tin[u] <= tin[v] && tout[v] <= tout[u];
}
int used[N * LG];
bool dfs(int u) {
bool ok = true;
used[u] = 1;
for (auto to : g[u]) {
// cout << u << ' ' << to << endl;
if (!used[to]) ok &= dfs(to);
else if (used[to] == 1) ok = false;
}
used[u] = 2;
return ok;
}
int main() {
cin.tie(nullptr)->sync_with_stdio(false);
int tests;
cin >> tests;
while (tests--) {
cin >> n;
for (int i = 0; i < n; ++i) tr[i].clear();
for (int i = 0; i < n; ++i) for (int j = 0; j < LG; ++j) bp[j][i] = -1;
for (int i = 0; i < n - 1; ++i) {
int u, v;
cin >> u >> v;
--u, --v;
tr[u].push_back(v);
tr[v].push_back(u);
}
build(0, -1);
int m;
cin >> m;
for (int i = 0; i < n * LG + m; ++i) g[i].clear(), used[i] = 0;
for (int i = 0; i < m; ++i) {
int s, t;
cin >> s >> t;
--s, --t;
g[t].push_back(i + n * LG);
if (is_par(t, s)) {
for (int k = LG - 1; k >= 0; --k) {
if (bp[k][s] != -1 && !is_par(bp[k][s], t)) {
g[i + n * LG].push_back(s + n * k);
s = bp[k][s];
}
}
} else {
t = bp[0][t];
if (s == t) {
g[i + n * LG].push_back(s);
} else {
if (tin[s] < tin[t]) swap(s, t);
for (int k = LG - 1; k >= 0; --k) {
if (bp[k][s] != -1 && !is_par(bp[k][s], t)) {
g[i + n * LG].push_back(s + n * k);
s = bp[k][s];
}
if (bp[k][t] != -1 && !is_par(bp[k][t], s)) {
g[i + n * LG].push_back(t + n * k);
t = bp[k][t];
}
}
// cout << s << ' ' << t << endl;
g[i + n * LG].push_back(s);
s = bp[0][s];
// cout << s << ' ' << t << endl;
g[i + n * LG].push_back(s);
if (t != s) g[i + n * LG].push_back(t);
}
}
}
bool ans = true;
for (int i = 0; i < n * LG + m; ++i) if (!used[i]) ans &= dfs(i);
cout << (ans ? "Yes\n" : "No\n");
}
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
25 ms |
61784 KB |
Output is correct |
2 |
Incorrect |
13 ms |
62016 KB |
Output isn't correct |
3 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
14 ms |
61784 KB |
Output is correct |
2 |
Incorrect |
13 ms |
61788 KB |
Output isn't correct |
3 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
14 ms |
61784 KB |
Output is correct |
2 |
Incorrect |
13 ms |
61788 KB |
Output isn't correct |
3 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
14 ms |
61784 KB |
Output is correct |
2 |
Incorrect |
13 ms |
61788 KB |
Output isn't correct |
3 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
14 ms |
61784 KB |
Output is correct |
2 |
Incorrect |
13 ms |
61788 KB |
Output isn't correct |
3 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
13 ms |
61784 KB |
Output is correct |
2 |
Correct |
13 ms |
62040 KB |
Output is correct |
3 |
Incorrect |
14 ms |
61836 KB |
Output isn't correct |
4 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
25 ms |
61784 KB |
Output is correct |
2 |
Incorrect |
13 ms |
62016 KB |
Output isn't correct |
3 |
Halted |
0 ms |
0 KB |
- |