#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];
vector <int> ls[501];
bool dfs (int pos, int par, int c) {
if (pos == r[c]) {
dd[pos].push_back(c);
ls[c].push_back(pos);
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);
ls[c].push_back(pos);
}
return f;
}
int main () {
ios::sync_with_stdio(0); cin.tie(0);
int t = 1; cin >> t;
while (t--) {
cin >> n;
for (int i = 1; i <= n; i++) {
adj[i].clear();
dd[i].clear();
}
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(); ls[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;
}
if (flag3) {
cout << "No\n";
continue;
}
for (int i = 1; i <= m; i++) sort(ls[i].begin(), ls[i].end());
for (int i = 1; i <= m; i++) {
for (int j = 1; j <= m; j++) {
if (i == j) continue;
bool flag = 0;
flag |= binary_search(ls[i].begin(), ls[i].end(), l[j]);
flag |= binary_search(ls[j].begin(), ls[j].end(), r[i]);
if (flag) adj2[i].push_back(j);
}
}
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 |
1 ms |
5980 KB |
Output is correct |
3 |
Correct |
2 ms |
5980 KB |
Output is correct |
4 |
Correct |
8 ms |
5980 KB |
Output is correct |
5 |
Correct |
14 ms |
5980 KB |
Output is correct |
6 |
Correct |
3 ms |
5980 KB |
Output is correct |
7 |
Correct |
2 ms |
5980 KB |
Output is correct |
8 |
Correct |
8 ms |
6312 KB |
Output is correct |
9 |
Correct |
550 ms |
33428 KB |
Output is correct |
10 |
Correct |
3308 ms |
494500 KB |
Output is correct |
11 |
Correct |
7 ms |
5980 KB |
Output is correct |
12 |
Correct |
104 ms |
6144 KB |
Output is correct |
13 |
Runtime error |
30 ms |
19500 KB |
Execution killed with signal 6 |
14 |
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 |
5976 KB |
Output is correct |
4 |
Correct |
2 ms |
5976 KB |
Output is correct |
5 |
Correct |
2 ms |
5976 KB |
Output is correct |
6 |
Correct |
2 ms |
5976 KB |
Output is correct |
7 |
Correct |
2 ms |
5980 KB |
Output is correct |
8 |
Correct |
3 ms |
5980 KB |
Output is correct |
9 |
Correct |
2 ms |
5980 KB |
Output is correct |
10 |
Correct |
2 ms |
5980 KB |
Output is correct |
11 |
Correct |
2 ms |
5980 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 |
5980 KB |
Output is correct |
2 |
Correct |
2 ms |
5980 KB |
Output is correct |
3 |
Correct |
2 ms |
5976 KB |
Output is correct |
4 |
Correct |
2 ms |
5976 KB |
Output is correct |
5 |
Correct |
2 ms |
5976 KB |
Output is correct |
6 |
Correct |
2 ms |
5976 KB |
Output is correct |
7 |
Correct |
2 ms |
5980 KB |
Output is correct |
8 |
Correct |
3 ms |
5980 KB |
Output is correct |
9 |
Correct |
2 ms |
5980 KB |
Output is correct |
10 |
Correct |
2 ms |
5980 KB |
Output is correct |
11 |
Correct |
2 ms |
5980 KB |
Output is correct |
12 |
Correct |
2 ms |
5980 KB |
Output is correct |
13 |
Correct |
2 ms |
5980 KB |
Output is correct |
14 |
Correct |
2 ms |
5980 KB |
Output is correct |
15 |
Correct |
2 ms |
5980 KB |
Output is correct |
16 |
Correct |
2 ms |
6080 KB |
Output is correct |
17 |
Correct |
2 ms |
5980 KB |
Output is correct |
18 |
Correct |
3 ms |
5980 KB |
Output is correct |
19 |
Correct |
2 ms |
5980 KB |
Output is correct |
20 |
Correct |
3 ms |
5980 KB |
Output is correct |
21 |
Correct |
3 ms |
5980 KB |
Output is correct |
22 |
Correct |
3 ms |
5980 KB |
Output is correct |
23 |
Correct |
2 ms |
5980 KB |
Output is correct |
24 |
Correct |
2 ms |
5980 KB |
Output is correct |
25 |
Correct |
3 ms |
5980 KB |
Output is correct |
26 |
Correct |
2 ms |
5976 KB |
Output is correct |
27 |
Correct |
3 ms |
5976 KB |
Output is correct |
28 |
Correct |
2 ms |
5980 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
2 ms |
5980 KB |
Output is correct |
2 |
Correct |
2 ms |
5980 KB |
Output is correct |
3 |
Correct |
2 ms |
5976 KB |
Output is correct |
4 |
Correct |
2 ms |
5976 KB |
Output is correct |
5 |
Correct |
2 ms |
5976 KB |
Output is correct |
6 |
Correct |
2 ms |
5976 KB |
Output is correct |
7 |
Correct |
2 ms |
5980 KB |
Output is correct |
8 |
Correct |
3 ms |
5980 KB |
Output is correct |
9 |
Correct |
2 ms |
5980 KB |
Output is correct |
10 |
Correct |
2 ms |
5980 KB |
Output is correct |
11 |
Correct |
2 ms |
5980 KB |
Output is correct |
12 |
Correct |
2 ms |
5980 KB |
Output is correct |
13 |
Correct |
2 ms |
5980 KB |
Output is correct |
14 |
Correct |
2 ms |
5980 KB |
Output is correct |
15 |
Correct |
2 ms |
5980 KB |
Output is correct |
16 |
Correct |
2 ms |
6080 KB |
Output is correct |
17 |
Correct |
2 ms |
5980 KB |
Output is correct |
18 |
Correct |
3 ms |
5980 KB |
Output is correct |
19 |
Correct |
2 ms |
5980 KB |
Output is correct |
20 |
Correct |
3 ms |
5980 KB |
Output is correct |
21 |
Correct |
3 ms |
5980 KB |
Output is correct |
22 |
Correct |
3 ms |
5980 KB |
Output is correct |
23 |
Correct |
2 ms |
5980 KB |
Output is correct |
24 |
Correct |
2 ms |
5980 KB |
Output is correct |
25 |
Correct |
3 ms |
5980 KB |
Output is correct |
26 |
Correct |
2 ms |
5976 KB |
Output is correct |
27 |
Correct |
3 ms |
5976 KB |
Output is correct |
28 |
Correct |
2 ms |
5980 KB |
Output is correct |
29 |
Correct |
11 ms |
6212 KB |
Output is correct |
30 |
Correct |
4 ms |
5980 KB |
Output is correct |
31 |
Correct |
5 ms |
6236 KB |
Output is correct |
32 |
Correct |
5 ms |
6232 KB |
Output is correct |
33 |
Correct |
3 ms |
5980 KB |
Output is correct |
34 |
Correct |
6 ms |
5980 KB |
Output is correct |
35 |
Correct |
10 ms |
6236 KB |
Output is correct |
36 |
Correct |
7 ms |
5976 KB |
Output is correct |
37 |
Correct |
7 ms |
5980 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
2 ms |
5980 KB |
Output is correct |
2 |
Correct |
2 ms |
5980 KB |
Output is correct |
3 |
Correct |
2 ms |
5976 KB |
Output is correct |
4 |
Correct |
2 ms |
5976 KB |
Output is correct |
5 |
Correct |
2 ms |
5976 KB |
Output is correct |
6 |
Correct |
2 ms |
5976 KB |
Output is correct |
7 |
Correct |
2 ms |
5980 KB |
Output is correct |
8 |
Correct |
3 ms |
5980 KB |
Output is correct |
9 |
Correct |
2 ms |
5980 KB |
Output is correct |
10 |
Correct |
2 ms |
5980 KB |
Output is correct |
11 |
Correct |
2 ms |
5980 KB |
Output is correct |
12 |
Correct |
2 ms |
5980 KB |
Output is correct |
13 |
Correct |
2 ms |
5980 KB |
Output is correct |
14 |
Correct |
2 ms |
5980 KB |
Output is correct |
15 |
Correct |
2 ms |
5980 KB |
Output is correct |
16 |
Correct |
2 ms |
6080 KB |
Output is correct |
17 |
Correct |
2 ms |
5980 KB |
Output is correct |
18 |
Correct |
3 ms |
5980 KB |
Output is correct |
19 |
Correct |
2 ms |
5980 KB |
Output is correct |
20 |
Correct |
3 ms |
5980 KB |
Output is correct |
21 |
Correct |
3 ms |
5980 KB |
Output is correct |
22 |
Correct |
3 ms |
5980 KB |
Output is correct |
23 |
Correct |
2 ms |
5980 KB |
Output is correct |
24 |
Correct |
2 ms |
5980 KB |
Output is correct |
25 |
Correct |
3 ms |
5980 KB |
Output is correct |
26 |
Correct |
2 ms |
5976 KB |
Output is correct |
27 |
Correct |
3 ms |
5976 KB |
Output is correct |
28 |
Correct |
2 ms |
5980 KB |
Output is correct |
29 |
Correct |
11 ms |
6212 KB |
Output is correct |
30 |
Correct |
4 ms |
5980 KB |
Output is correct |
31 |
Correct |
5 ms |
6236 KB |
Output is correct |
32 |
Correct |
5 ms |
6232 KB |
Output is correct |
33 |
Correct |
3 ms |
5980 KB |
Output is correct |
34 |
Correct |
6 ms |
5980 KB |
Output is correct |
35 |
Correct |
10 ms |
6236 KB |
Output is correct |
36 |
Correct |
7 ms |
5976 KB |
Output is correct |
37 |
Correct |
7 ms |
5980 KB |
Output is correct |
38 |
Correct |
609 ms |
34388 KB |
Output is correct |
39 |
Correct |
3268 ms |
496076 KB |
Output is correct |
40 |
Correct |
744 ms |
18188 KB |
Output is correct |
41 |
Correct |
683 ms |
7912 KB |
Output is correct |
42 |
Correct |
378 ms |
17492 KB |
Output is correct |
43 |
Correct |
43 ms |
7512 KB |
Output is correct |
44 |
Correct |
105 ms |
6492 KB |
Output is correct |
45 |
Correct |
1679 ms |
12248 KB |
Output is correct |
46 |
Correct |
1736 ms |
12468 KB |
Output is correct |
47 |
Correct |
1079 ms |
103112 KB |
Output is correct |
48 |
Correct |
1103 ms |
105552 KB |
Output is correct |
49 |
Correct |
1129 ms |
16860 KB |
Output is correct |
50 |
Correct |
1213 ms |
16968 KB |
Output is correct |
51 |
Correct |
182 ms |
12380 KB |
Output is correct |
52 |
Correct |
196 ms |
12560 KB |
Output is correct |
53 |
Correct |
124 ms |
6720 KB |
Output is correct |
54 |
Correct |
1278 ms |
11636 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
2 ms |
5980 KB |
Output is correct |
2 |
Correct |
1 ms |
5980 KB |
Output is correct |
3 |
Correct |
2 ms |
5980 KB |
Output is correct |
4 |
Correct |
1 ms |
5980 KB |
Output is correct |
5 |
Correct |
7 ms |
6132 KB |
Output is correct |
6 |
Correct |
2 ms |
5980 KB |
Output is correct |
7 |
Correct |
2 ms |
5980 KB |
Output is correct |
8 |
Correct |
2 ms |
5980 KB |
Output is correct |
9 |
Correct |
2 ms |
5980 KB |
Output is correct |
10 |
Correct |
2 ms |
5980 KB |
Output is correct |
11 |
Correct |
2 ms |
6100 KB |
Output is correct |
12 |
Correct |
7 ms |
5980 KB |
Output is correct |
13 |
Correct |
38 ms |
6644 KB |
Output is correct |
14 |
Correct |
57 ms |
6896 KB |
Output is correct |
15 |
Correct |
53 ms |
6740 KB |
Output is correct |
16 |
Runtime error |
1160 ms |
1048576 KB |
Execution killed with signal 9 |
17 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
2 ms |
5980 KB |
Output is correct |
2 |
Correct |
1 ms |
5980 KB |
Output is correct |
3 |
Correct |
2 ms |
5980 KB |
Output is correct |
4 |
Correct |
8 ms |
5980 KB |
Output is correct |
5 |
Correct |
14 ms |
5980 KB |
Output is correct |
6 |
Correct |
3 ms |
5980 KB |
Output is correct |
7 |
Correct |
2 ms |
5980 KB |
Output is correct |
8 |
Correct |
8 ms |
6312 KB |
Output is correct |
9 |
Correct |
550 ms |
33428 KB |
Output is correct |
10 |
Correct |
3308 ms |
494500 KB |
Output is correct |
11 |
Correct |
7 ms |
5980 KB |
Output is correct |
12 |
Correct |
104 ms |
6144 KB |
Output is correct |
13 |
Runtime error |
30 ms |
19500 KB |
Execution killed with signal 6 |
14 |
Halted |
0 ms |
0 KB |
- |