#include <bits/stdc++.h>
using namespace std;
const int MAXN = 120025;
vector <int> adj[MAXN];
vector <int> adj2[501];
int n, m;
int l[501], r[501], deg[501];
vector <int> dd[MAXN];
bool dfs (int pos, int par, int c) {
if (pos == r[c]) {
dd[pos].push_back(c);
return 1;
}
bool f = 0;
for (auto i : adj[pos]) {
if (i != par) {
f |= dfs(i, pos, c);
}
}
if (f) dd[pos].push_back(c);
return f;
}
void reset () {
for (int i = 1; i <= n; i++) {
adj[i].clear();
dd[i].clear();
}
}
int main () {
int t = 1; cin >> t;
while (t--) {
cin >> n;
reset();
for (int i = 1; i < n; i++) {
int a, b;
cin >> a >> b;
adj[a].push_back(b);
adj[b].push_back(a);
}
cin >> m;
for (int i = 1; i <= m; i++) {
deg[i] = 0; adj2[i].clear();
}
for (int i = 1; i <= m; i++) {
cin >> l[i] >> r[i];
dfs(l[i], -1, i);
}
for (int i = 1; i <= n; i++) sort(dd[i].begin(), dd[i].end());
bool flag3 = 0;
for (int i = 1; i <= m; i++) {
set <int> y;
for (auto j : dd[l[i]]) y.insert(j);
bool u2 = 0;
for (auto j : dd[r[i]]) u2 |= y.count(j) && j != i;
flag3 |= u2;
u2 = 0;
for (auto j : dd[l[i]]) if (j != i) u2 |= binary_search(dd[l[j]].begin(), dd[l[j]].end(), i);
for (auto j : dd[r[i]]) if (j != i) u2 |= binary_search(dd[r[j]].begin(), dd[r[j]].end(), i);
flag3 |= u2;
}
/*for (int i = 1; i <= n; i++) {
for (auto j : dd[i]) cout << j << " ";
cout << '\n';
}*/
if (flag3) {
cout << "No\n";
continue;
}
for (int i = 1; i <= m; i++) {
for (auto j : dd[r[i]]) {
if (j != i) adj2[i].push_back(j);
}
}/*
cout << '\n' << '\n';
for (int i = 1; i <= m; i++) {
for (auto j : adj2[i]) cout << j << " ";
cout << '\n';
}*/
for (int i = 1; i <= m; i++) {
for (auto j : adj2[i]) {
deg[j]++;
}
}
vector <int> d;
queue <int> cur;
for (int i = 1; i <= m; i++) {
if (deg[i] == 0) {
cur.push(i);
}
}
while (!cur.empty()) {
auto j = cur.front();
cur.pop();
d.push_back(j);
for (auto z : adj2[j]) {
deg[z]--;
if (deg[z] == 0) cur.push(z);
}
}
if ((int)d.size() != m) {
cout << "No\n";
} else {
cout << "Yes\n";
}
}
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
2 ms |
5980 KB |
Output is correct |
2 |
Correct |
2 ms |
5980 KB |
Output is correct |
3 |
Correct |
1 ms |
5976 KB |
Output is correct |
4 |
Correct |
18 ms |
5980 KB |
Output is correct |
5 |
Correct |
37 ms |
6608 KB |
Output is correct |
6 |
Correct |
3 ms |
5980 KB |
Output is correct |
7 |
Correct |
3 ms |
5980 KB |
Output is correct |
8 |
Correct |
9 ms |
6244 KB |
Output is correct |
9 |
Correct |
505 ms |
20372 KB |
Output is correct |
10 |
Correct |
2479 ms |
263316 KB |
Output is correct |
11 |
Correct |
12 ms |
6236 KB |
Output is correct |
12 |
Correct |
122 ms |
6876 KB |
Output is correct |
13 |
Runtime error |
66 ms |
21072 KB |
Execution killed with signal 6 |
14 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
2 ms |
5976 KB |
Output is correct |
2 |
Correct |
3 ms |
5980 KB |
Output is correct |
3 |
Correct |
3 ms |
5980 KB |
Output is correct |
4 |
Correct |
3 ms |
5980 KB |
Output is correct |
5 |
Correct |
4 ms |
5976 KB |
Output is correct |
6 |
Correct |
3 ms |
5980 KB |
Output is correct |
7 |
Correct |
3 ms |
5980 KB |
Output is correct |
8 |
Correct |
3 ms |
6232 KB |
Output is correct |
9 |
Correct |
3 ms |
5980 KB |
Output is correct |
10 |
Correct |
3 ms |
5980 KB |
Output is correct |
11 |
Correct |
3 ms |
6388 KB |
Output is correct |
12 |
Correct |
2 ms |
5980 KB |
Output is correct |
13 |
Correct |
2 ms |
5980 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
2 ms |
5976 KB |
Output is correct |
2 |
Correct |
3 ms |
5980 KB |
Output is correct |
3 |
Correct |
3 ms |
5980 KB |
Output is correct |
4 |
Correct |
3 ms |
5980 KB |
Output is correct |
5 |
Correct |
4 ms |
5976 KB |
Output is correct |
6 |
Correct |
3 ms |
5980 KB |
Output is correct |
7 |
Correct |
3 ms |
5980 KB |
Output is correct |
8 |
Correct |
3 ms |
6232 KB |
Output is correct |
9 |
Correct |
3 ms |
5980 KB |
Output is correct |
10 |
Correct |
3 ms |
5980 KB |
Output is correct |
11 |
Correct |
3 ms |
6388 KB |
Output is correct |
12 |
Correct |
2 ms |
5980 KB |
Output is correct |
13 |
Correct |
2 ms |
5980 KB |
Output is correct |
14 |
Correct |
1 ms |
5980 KB |
Output is correct |
15 |
Correct |
2 ms |
5980 KB |
Output is correct |
16 |
Correct |
3 ms |
5980 KB |
Output is correct |
17 |
Correct |
3 ms |
6144 KB |
Output is correct |
18 |
Correct |
3 ms |
5976 KB |
Output is correct |
19 |
Correct |
2 ms |
6060 KB |
Output is correct |
20 |
Correct |
5 ms |
6236 KB |
Output is correct |
21 |
Correct |
3 ms |
5980 KB |
Output is correct |
22 |
Correct |
3 ms |
6064 KB |
Output is correct |
23 |
Correct |
2 ms |
5980 KB |
Output is correct |
24 |
Incorrect |
2 ms |
5980 KB |
Output isn't correct |
25 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
2 ms |
5976 KB |
Output is correct |
2 |
Correct |
3 ms |
5980 KB |
Output is correct |
3 |
Correct |
3 ms |
5980 KB |
Output is correct |
4 |
Correct |
3 ms |
5980 KB |
Output is correct |
5 |
Correct |
4 ms |
5976 KB |
Output is correct |
6 |
Correct |
3 ms |
5980 KB |
Output is correct |
7 |
Correct |
3 ms |
5980 KB |
Output is correct |
8 |
Correct |
3 ms |
6232 KB |
Output is correct |
9 |
Correct |
3 ms |
5980 KB |
Output is correct |
10 |
Correct |
3 ms |
5980 KB |
Output is correct |
11 |
Correct |
3 ms |
6388 KB |
Output is correct |
12 |
Correct |
2 ms |
5980 KB |
Output is correct |
13 |
Correct |
2 ms |
5980 KB |
Output is correct |
14 |
Correct |
1 ms |
5980 KB |
Output is correct |
15 |
Correct |
2 ms |
5980 KB |
Output is correct |
16 |
Correct |
3 ms |
5980 KB |
Output is correct |
17 |
Correct |
3 ms |
6144 KB |
Output is correct |
18 |
Correct |
3 ms |
5976 KB |
Output is correct |
19 |
Correct |
2 ms |
6060 KB |
Output is correct |
20 |
Correct |
5 ms |
6236 KB |
Output is correct |
21 |
Correct |
3 ms |
5980 KB |
Output is correct |
22 |
Correct |
3 ms |
6064 KB |
Output is correct |
23 |
Correct |
2 ms |
5980 KB |
Output is correct |
24 |
Incorrect |
2 ms |
5980 KB |
Output isn't correct |
25 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
2 ms |
5976 KB |
Output is correct |
2 |
Correct |
3 ms |
5980 KB |
Output is correct |
3 |
Correct |
3 ms |
5980 KB |
Output is correct |
4 |
Correct |
3 ms |
5980 KB |
Output is correct |
5 |
Correct |
4 ms |
5976 KB |
Output is correct |
6 |
Correct |
3 ms |
5980 KB |
Output is correct |
7 |
Correct |
3 ms |
5980 KB |
Output is correct |
8 |
Correct |
3 ms |
6232 KB |
Output is correct |
9 |
Correct |
3 ms |
5980 KB |
Output is correct |
10 |
Correct |
3 ms |
5980 KB |
Output is correct |
11 |
Correct |
3 ms |
6388 KB |
Output is correct |
12 |
Correct |
2 ms |
5980 KB |
Output is correct |
13 |
Correct |
2 ms |
5980 KB |
Output is correct |
14 |
Correct |
1 ms |
5980 KB |
Output is correct |
15 |
Correct |
2 ms |
5980 KB |
Output is correct |
16 |
Correct |
3 ms |
5980 KB |
Output is correct |
17 |
Correct |
3 ms |
6144 KB |
Output is correct |
18 |
Correct |
3 ms |
5976 KB |
Output is correct |
19 |
Correct |
2 ms |
6060 KB |
Output is correct |
20 |
Correct |
5 ms |
6236 KB |
Output is correct |
21 |
Correct |
3 ms |
5980 KB |
Output is correct |
22 |
Correct |
3 ms |
6064 KB |
Output is correct |
23 |
Correct |
2 ms |
5980 KB |
Output is correct |
24 |
Incorrect |
2 ms |
5980 KB |
Output isn't correct |
25 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
2 ms |
5980 KB |
Output is correct |
2 |
Correct |
2 ms |
5980 KB |
Output is correct |
3 |
Correct |
2 ms |
5980 KB |
Output is correct |
4 |
Correct |
2 ms |
5980 KB |
Output is correct |
5 |
Correct |
12 ms |
5980 KB |
Output is correct |
6 |
Correct |
2 ms |
5980 KB |
Output is correct |
7 |
Correct |
3 ms |
5976 KB |
Output is correct |
8 |
Correct |
2 ms |
5980 KB |
Output is correct |
9 |
Correct |
2 ms |
5980 KB |
Output is correct |
10 |
Incorrect |
2 ms |
5980 KB |
Output isn't correct |
11 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
2 ms |
5980 KB |
Output is correct |
2 |
Correct |
2 ms |
5980 KB |
Output is correct |
3 |
Correct |
1 ms |
5976 KB |
Output is correct |
4 |
Correct |
18 ms |
5980 KB |
Output is correct |
5 |
Correct |
37 ms |
6608 KB |
Output is correct |
6 |
Correct |
3 ms |
5980 KB |
Output is correct |
7 |
Correct |
3 ms |
5980 KB |
Output is correct |
8 |
Correct |
9 ms |
6244 KB |
Output is correct |
9 |
Correct |
505 ms |
20372 KB |
Output is correct |
10 |
Correct |
2479 ms |
263316 KB |
Output is correct |
11 |
Correct |
12 ms |
6236 KB |
Output is correct |
12 |
Correct |
122 ms |
6876 KB |
Output is correct |
13 |
Runtime error |
66 ms |
21072 KB |
Execution killed with signal 6 |
14 |
Halted |
0 ms |
0 KB |
- |