#include <iostream>
#include <vector>
using namespace std;
int n, m, sz, c;
bool ans;
vector <vector <int>> g(2e5);
vector <int> dep(2e5, 0);
vector <vector <int>> lift(2e5, vector <int>(20, 0));
vector <int> v1(2e5, 0);
vector <int> v2(2e5, 0);
vector <vector <int>> g2(2e5);
vector <bool> be(2e5);
vector <bool> ki(2e5);
//vector <bool> del(2e5);
void reset() {
ans = true;
for (int i = 1; i <= n; i++) {
g[i].clear();
dep[i] = 0;
fill(lift[i].begin(), lift[i].end(), 0);
g2[i].clear();
be[i] = ki[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);
}*/
void dfs(int x) {
for (int i : g[x]) {
if (i == lift[x][0]) continue;
dep[i] = dep[x] + 1;
lift[i][0] = x;
dfs(i);
}
}
int lca(int a, int b) {
if (dep[a] < dep[b]) swap(a, b);
for (int j = 19; j >= 0; j--) {
if (dep[lift[a][j]] >= dep[b]) a = lift[a][j];
}
if (a == b) return a;
for (int j = 19; j >= 0; j--) {
if (lift[a][j] == lift[b][j]) continue;
a = lift[a][j];
b = lift[b][j];
}
return lift[a][0];
}
int par(int a, int k) {
if (k < 0) return 0;
for (int j = 0; j < 20; j++) {
if (k & (1 << j)) a = lift[a][j];
}
return a;
}
bool on_path(int a, int b, int c) {
int l = lca(a, b);
if (dep[c] < dep[l]) return false;
if (par(a, dep[a] - dep[c]) == c) return true;
if (par(b, dep[b] - dep[c]) == c) return true;
return false;
}
void tp(int x) {
if (be[x] && !ki[x]) ans = false;
if (be[x]) return;
be[x] = true;
for (int i : g2[x]) tp(i);
ki[x] = true;
}
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);
}
dfs(1);
cin >> m;
for (int i = 0; i < m; i++) {
cin >> v1[i] >> v2[i];
for (int j = 0; j < i; j++) {
if (on_path(v1[i], v2[i], v1[j])) g2[j].push_back(i);
if (on_path(v1[i], v2[i], v2[j])) g2[i].push_back(j);
swap(i, j);
if (on_path(v1[i], v2[i], v1[j])) g2[j].push_back(i);
if (on_path(v1[i], v2[i], v2[j])) g2[i].push_back(j);
swap(i, j);
}
}
for (int i = 0; i < m; i++) tp(i);
cout << (ans ? "Yes\n" : "No\n");
}
int main() {
ios_base::sync_with_stdio(false);
cin.tie(NULL);
int t;
cin >> t;
while (t--) solve();
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
19 ms |
35676 KB |
Output is correct |
2 |
Correct |
18 ms |
35664 KB |
Output is correct |
3 |
Correct |
20 ms |
35660 KB |
Output is correct |
4 |
Incorrect |
23 ms |
35676 KB |
Output isn't correct |
5 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
19 ms |
35728 KB |
Output is correct |
2 |
Correct |
17 ms |
35672 KB |
Output is correct |
3 |
Incorrect |
19 ms |
35672 KB |
Output isn't correct |
4 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
19 ms |
35728 KB |
Output is correct |
2 |
Correct |
17 ms |
35672 KB |
Output is correct |
3 |
Incorrect |
19 ms |
35672 KB |
Output isn't correct |
4 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
19 ms |
35728 KB |
Output is correct |
2 |
Correct |
17 ms |
35672 KB |
Output is correct |
3 |
Incorrect |
19 ms |
35672 KB |
Output isn't correct |
4 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
19 ms |
35728 KB |
Output is correct |
2 |
Correct |
17 ms |
35672 KB |
Output is correct |
3 |
Incorrect |
19 ms |
35672 KB |
Output isn't correct |
4 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
17 ms |
35672 KB |
Output is correct |
2 |
Incorrect |
18 ms |
35676 KB |
Output isn't correct |
3 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
19 ms |
35676 KB |
Output is correct |
2 |
Correct |
18 ms |
35664 KB |
Output is correct |
3 |
Correct |
20 ms |
35660 KB |
Output is correct |
4 |
Incorrect |
23 ms |
35676 KB |
Output isn't correct |
5 |
Halted |
0 ms |
0 KB |
- |