#include <bits/stdc++.h>
using namespace std;
#define int long long
#define all(x) x.begin(), x.end()
#define size(x) (int)x.size()
template<class S, class T>
bool chmin(S &a, const T &b) {
return a > b && (a = b) == b;
}
template<class S, class T>
bool chmax(S &a, const T &b) {
return a < b && (a = b) == b;
}
const int inf = 1e9 + 7;
const int INF = 1e18 + 7;
const int mod = 998244353;
void Main() {
int n;
cin >> n;
vector<int> g[n];
for (int i = 1; i < n; ++i) {
int a, b;
cin >> a >> b;
a--, b--;
g[a].push_back(b);
g[b].push_back(a);
}
int root = -1, position = -1;
vector<int> pos(n);
vector<bool> used(n, false);
for (int i = 0; i < n; ++i) {
if (size(g[i]) == 1) {
root = i;
break;
}
}
function<void(int, int)> dfs = [&](int v, int p) {
pos[v] = ++position;
for (int to : g[v]) {
if (to != p) {
dfs(to, v);
}
}
};
dfs(root, -1);
bool flag = true;
vector<pair<int, int>> v;
int m;
cin >> m;
while (m--) {
int s, t;
cin >> s >> t;
s--, t--;
v.push_back({pos[t], pos[s]});
used[pos[s]] = true;
}
sort(all(v));
while (!v.empty()) {
int s = v.back().second, t = v.back().first;
v.pop_back();
used[s] = false;
if (s < t) {
while (s <= t) {
if (used[s]) {
flag = false;
break;
}
s++;
}
} else {
while (s >= t) {
if (used[s]) {
flag = false;
break;
}
s--;
}
}
used[t] = true;
}
cout << (flag ? "Yes" : "No") << '\n';
}
signed main() {
cin.tie(nullptr)->sync_with_stdio(false);
int q;
cin >> q;
while (q--) {
Main();
}
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
348 KB |
Output is correct |
2 |
Incorrect |
0 ms |
348 KB |
Output isn't correct |
3 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
348 KB |
Output is correct |
2 |
Correct |
0 ms |
348 KB |
Output is correct |
3 |
Incorrect |
1 ms |
348 KB |
Output isn't correct |
4 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
348 KB |
Output is correct |
2 |
Correct |
0 ms |
348 KB |
Output is correct |
3 |
Incorrect |
1 ms |
348 KB |
Output isn't correct |
4 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
348 KB |
Output is correct |
2 |
Correct |
0 ms |
348 KB |
Output is correct |
3 |
Incorrect |
1 ms |
348 KB |
Output isn't correct |
4 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
348 KB |
Output is correct |
2 |
Correct |
0 ms |
348 KB |
Output is correct |
3 |
Incorrect |
1 ms |
348 KB |
Output isn't correct |
4 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
344 KB |
Output is correct |
2 |
Correct |
0 ms |
348 KB |
Output is correct |
3 |
Incorrect |
0 ms |
348 KB |
Output isn't correct |
4 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
348 KB |
Output is correct |
2 |
Incorrect |
0 ms |
348 KB |
Output isn't correct |
3 |
Halted |
0 ms |
0 KB |
- |