#include <bits/stdc++.h>
using namespace std;
#define ll long long
const int N = 120'005;
vector<int> adj[N];
int st[N], en[N], h[N], anc[N][17], t;
void dfs(int i, int par, int d = 0) {
anc[i][0] = par;
for (int j = 1; j < 17; j++) {
anc[i][j] = anc[anc[i][j - 1]][j - 1];
}
h[i] = d;
st[i] = t++;
for (auto nxt : adj[i]) {
if (nxt == par) continue;
dfs(nxt, i, d + 1);
}
en[i] = t - 1;
}
int getlca(int u, int v) {
if (st[u] <= st[v] && en[v] <= en[u]) {
return u;
}
for (int j = 16; j >= 0; j--) {
int nxtu = anc[u][j];
if (!(st[nxtu] <= st[v] && en[v] <= en[nxtu])) {
u = nxtu;
}
}
return anc[u][0];
}
struct path {
int s, e, lca;
} a[N];
int m;
bool vis[N], instack[N];
bool dfs2(int i) {
vis[i] = 1;
instack[i] = 1;
bool ok = 1;
// cout << i + 1 << '\n';
for (int j = 0; j < m; j++) {
if (j == i) continue;
if (st[a[i].lca] <= st[a[j].s] && en[a[j].s] <= en[a[i].lca] &&
st[a[j].s] <= st[a[i].s] && en[a[i].s] <= en[a[j].s]) {
if (instack[j]) {
ok = 0;
break;
}
if (vis[j]) continue;
if (!dfs2(j)) {
ok = 0;
break;
}
}
}
for (int j = 0; j < m; j++) {
if (j == i) continue;
if (st[a[i].lca] <= st[a[j].s] && en[a[j].s] <= en[a[i].lca] &&
st[a[j].s] <= st[a[i].e] && en[a[i].e] <= en[a[j].s]) {
if (instack[j]) {
ok = 0;
break;
}
if (vis[j]) continue;
if (!dfs2(j)) {
ok = 0;
break;
}
}
}
instack[i] = 0;
return ok;
}
int main() {
ios_base::sync_with_stdio(0), cin.tie(0);
int T;
cin >> T;
while (T--) {
int n;
cin >> n;
for (int i = 0; i < n; i++) {
adj[i].clear();
vis[i] = 0;
instack[i] = 0;
}
for (int i = 1, u, v; i < n; i++) {
cin >> u >> v;
u--, v--;
adj[u].push_back(v);
adj[v].push_back(u);
}
t = 0;
dfs(0, 0);
cin >> m;
for (int i = 0; i < m; i++) {
cin >> a[i].s >> a[i].e;
a[i].s--, a[i].e--;
a[i].lca = getlca(a[i].s, a[i].e);
// cout << a[i].s + 1 << ' ' << a[i].e + 1 << ' ' << a[i].lca + 1 << '\n';
}
auto check = [&] (int i, int x) {
if (st[a[i].lca] <= st[x] && en[x] <= en[a[i].lca] &&
((st[x] <= st[a[i].s] && en[a[i].s] <= en[x]) ||
(st[x] <= st[a[i].e] && en[a[i].e] <= en[x]))) {
return 1;
}
return 0;
};
bool ok = 1;
for (int i = 0; i < m; i++) {
for (int j = 0; j < m; j++) {
if (i == j) continue;
if (check(i, a[j].s) && check(i, a[j].e)) {
ok = 0;
break;
}
}
}
for (int i = 0; i < m; i++) {
if (vis[i]) continue;
if (!dfs2(i)) {
ok = 0;
}
}
cout << (ok? "Yes\n" : "No\n");
}
return 0;
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
2 ms |
7772 KB |
Output is correct |
2 |
Correct |
2 ms |
7772 KB |
Output is correct |
3 |
Correct |
2 ms |
7972 KB |
Output is correct |
4 |
Incorrect |
11 ms |
8284 KB |
Output isn't correct |
5 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
2 ms |
7768 KB |
Output is correct |
2 |
Correct |
2 ms |
7772 KB |
Output is correct |
3 |
Incorrect |
2 ms |
8028 KB |
Output isn't correct |
4 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
2 ms |
7768 KB |
Output is correct |
2 |
Correct |
2 ms |
7772 KB |
Output is correct |
3 |
Incorrect |
2 ms |
8028 KB |
Output isn't correct |
4 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
2 ms |
7768 KB |
Output is correct |
2 |
Correct |
2 ms |
7772 KB |
Output is correct |
3 |
Incorrect |
2 ms |
8028 KB |
Output isn't correct |
4 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
2 ms |
7768 KB |
Output is correct |
2 |
Correct |
2 ms |
7772 KB |
Output is correct |
3 |
Incorrect |
2 ms |
8028 KB |
Output isn't correct |
4 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
2 ms |
7768 KB |
Output is correct |
2 |
Correct |
2 ms |
7772 KB |
Output is correct |
3 |
Correct |
2 ms |
7968 KB |
Output is correct |
4 |
Correct |
2 ms |
7952 KB |
Output is correct |
5 |
Incorrect |
6 ms |
8028 KB |
Output isn't correct |
6 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
2 ms |
7772 KB |
Output is correct |
2 |
Correct |
2 ms |
7772 KB |
Output is correct |
3 |
Correct |
2 ms |
7972 KB |
Output is correct |
4 |
Incorrect |
11 ms |
8284 KB |
Output isn't correct |
5 |
Halted |
0 ms |
0 KB |
- |