#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;
deque<pair<int, int>> l, r;
int m;
cin >> m;
while (m--) {
int s, t;
cin >> s >> t;
s--, t--;
if (pos[t] > pos[s]) {
r.push_back({pos[t], pos[s]});
} else {
l.push_back({pos[t], pos[s]});
}
used[pos[s]] = true;
}
sort(all(l)), sort(all(r));
while (!l.empty()) {
int s = l.front().second, t = l.front().first;
l.pop_front();
used[s] = false;
while (s > t) {
if (used[s]) {
flag = false;
break;
}
s--;
}
used[t] = true;
}
while (!r.empty()) {
int s = r.back().second, t = r.back().first;
r.pop_back();
used[s] = false;
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 |
344 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 |
Incorrect |
1 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 |
Incorrect |
1 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 |
Incorrect |
1 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 |
Incorrect |
1 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 |
0 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 |
Incorrect |
0 ms |
348 KB |
Output isn't correct |
3 |
Halted |
0 ms |
0 KB |
- |