#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 mx1[MAXN], mx2[MAXN];
void solve () {
cin >> m;
vector <pair <int, int>> gg, ff, xx;
for (int i = 0; i < m; i++) {
int l, r;
cin >> l >> r;
if (r < l) ff.push_back({r, l}), swap(l, r);
else gg.push_back({l, r});
xx.push_back({l, r});
}
for (int i = 0; i <= n; i++) mx1[i] = mx2[i] = 0;
sort(xx.begin(), xx.end(), [] (pair <int, int> a, pair <int, int> b) {
return a.first == b.first ? a.second > b.second : a.first < b.first;
});
reverse(xx.begin(), xx.end());
set <int> dd;
bool flag = 0;
for (auto [j, k] : xx) {
//cout << j << " " << k << '\n';
if (!dd.empty()) flag |= *(dd.begin()) <= k;
dd.insert(k);
}
if (flag) {
cout << "No\n";
return;
}
for (auto [j, k] : gg) {
mx1[j] = max(mx1[j], k);
}
for (auto [j, k] : ff) {
mx2[j] = max(mx2[j], k);
}
for (int i = n - 1; i >= 1; i--) {
mx1[i] = max(mx1[i], mx1[i - 1]);
mx2[i] = max(mx2[i], mx2[i - 1]);
}
for (auto [j, k] : gg) {
flag |= mx2[j] >= k;
}
for (auto [j, k] : ff) {
flag |= mx1[j] >= k;
}
cout << (flag ? "No\n" : "Yes\n");
}
int main () {
ios::sync_with_stdio(0); cin.tie(0);
int t = 1; cin >> t;
while (t--) {
cin >> n;
bool flag6 = 1;
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;
flag6 &= a == i && b == i + 1;
adj[a].push_back(b);
adj[b].push_back(a);
}
//if (flag6) {
/// solve();
// continue;
//}
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";
}
}
}
Compilation message
jail.cpp: In function 'void solve()':
jail.cpp:46:12: warning: structured bindings only available with '-std=c++17' or '-std=gnu++17'
46 | for (auto [j, k] : xx) {
| ^
jail.cpp:55:12: warning: structured bindings only available with '-std=c++17' or '-std=gnu++17'
55 | for (auto [j, k] : gg) {
| ^
jail.cpp:58:12: warning: structured bindings only available with '-std=c++17' or '-std=gnu++17'
58 | for (auto [j, k] : ff) {
| ^
jail.cpp:65:12: warning: structured bindings only available with '-std=c++17' or '-std=gnu++17'
65 | for (auto [j, k] : gg) {
| ^
jail.cpp:68:12: warning: structured bindings only available with '-std=c++17' or '-std=gnu++17'
68 | for (auto [j, k] : ff) {
| ^
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
6744 KB |
Output is correct |
2 |
Correct |
1 ms |
6748 KB |
Output is correct |
3 |
Correct |
1 ms |
6748 KB |
Output is correct |
4 |
Correct |
8 ms |
7256 KB |
Output is correct |
5 |
Correct |
14 ms |
6916 KB |
Output is correct |
6 |
Correct |
2 ms |
6748 KB |
Output is correct |
7 |
Correct |
2 ms |
6748 KB |
Output is correct |
8 |
Correct |
10 ms |
7004 KB |
Output is correct |
9 |
Correct |
603 ms |
33872 KB |
Output is correct |
10 |
Correct |
3317 ms |
495376 KB |
Output is correct |
11 |
Correct |
7 ms |
6748 KB |
Output is correct |
12 |
Correct |
109 ms |
7008 KB |
Output is correct |
13 |
Runtime error |
32 ms |
21072 KB |
Execution killed with signal 6 |
14 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
2 ms |
6744 KB |
Output is correct |
2 |
Correct |
1 ms |
6748 KB |
Output is correct |
3 |
Correct |
3 ms |
6748 KB |
Output is correct |
4 |
Correct |
2 ms |
6920 KB |
Output is correct |
5 |
Correct |
2 ms |
6748 KB |
Output is correct |
6 |
Correct |
2 ms |
6748 KB |
Output is correct |
7 |
Correct |
3 ms |
6748 KB |
Output is correct |
8 |
Correct |
2 ms |
6748 KB |
Output is correct |
9 |
Correct |
2 ms |
6748 KB |
Output is correct |
10 |
Correct |
2 ms |
6748 KB |
Output is correct |
11 |
Correct |
2 ms |
6748 KB |
Output is correct |
12 |
Correct |
2 ms |
6748 KB |
Output is correct |
13 |
Correct |
2 ms |
6748 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
2 ms |
6744 KB |
Output is correct |
2 |
Correct |
1 ms |
6748 KB |
Output is correct |
3 |
Correct |
3 ms |
6748 KB |
Output is correct |
4 |
Correct |
2 ms |
6920 KB |
Output is correct |
5 |
Correct |
2 ms |
6748 KB |
Output is correct |
6 |
Correct |
2 ms |
6748 KB |
Output is correct |
7 |
Correct |
3 ms |
6748 KB |
Output is correct |
8 |
Correct |
2 ms |
6748 KB |
Output is correct |
9 |
Correct |
2 ms |
6748 KB |
Output is correct |
10 |
Correct |
2 ms |
6748 KB |
Output is correct |
11 |
Correct |
2 ms |
6748 KB |
Output is correct |
12 |
Correct |
2 ms |
6748 KB |
Output is correct |
13 |
Correct |
2 ms |
6748 KB |
Output is correct |
14 |
Correct |
1 ms |
6748 KB |
Output is correct |
15 |
Correct |
1 ms |
6748 KB |
Output is correct |
16 |
Correct |
2 ms |
6748 KB |
Output is correct |
17 |
Correct |
2 ms |
6748 KB |
Output is correct |
18 |
Correct |
2 ms |
6748 KB |
Output is correct |
19 |
Correct |
2 ms |
6748 KB |
Output is correct |
20 |
Correct |
2 ms |
6748 KB |
Output is correct |
21 |
Correct |
2 ms |
6748 KB |
Output is correct |
22 |
Correct |
2 ms |
6748 KB |
Output is correct |
23 |
Correct |
2 ms |
6748 KB |
Output is correct |
24 |
Correct |
2 ms |
6872 KB |
Output is correct |
25 |
Correct |
3 ms |
6748 KB |
Output is correct |
26 |
Correct |
2 ms |
6748 KB |
Output is correct |
27 |
Correct |
2 ms |
6748 KB |
Output is correct |
28 |
Correct |
2 ms |
6872 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
2 ms |
6744 KB |
Output is correct |
2 |
Correct |
1 ms |
6748 KB |
Output is correct |
3 |
Correct |
3 ms |
6748 KB |
Output is correct |
4 |
Correct |
2 ms |
6920 KB |
Output is correct |
5 |
Correct |
2 ms |
6748 KB |
Output is correct |
6 |
Correct |
2 ms |
6748 KB |
Output is correct |
7 |
Correct |
3 ms |
6748 KB |
Output is correct |
8 |
Correct |
2 ms |
6748 KB |
Output is correct |
9 |
Correct |
2 ms |
6748 KB |
Output is correct |
10 |
Correct |
2 ms |
6748 KB |
Output is correct |
11 |
Correct |
2 ms |
6748 KB |
Output is correct |
12 |
Correct |
2 ms |
6748 KB |
Output is correct |
13 |
Correct |
2 ms |
6748 KB |
Output is correct |
14 |
Correct |
1 ms |
6748 KB |
Output is correct |
15 |
Correct |
1 ms |
6748 KB |
Output is correct |
16 |
Correct |
2 ms |
6748 KB |
Output is correct |
17 |
Correct |
2 ms |
6748 KB |
Output is correct |
18 |
Correct |
2 ms |
6748 KB |
Output is correct |
19 |
Correct |
2 ms |
6748 KB |
Output is correct |
20 |
Correct |
2 ms |
6748 KB |
Output is correct |
21 |
Correct |
2 ms |
6748 KB |
Output is correct |
22 |
Correct |
2 ms |
6748 KB |
Output is correct |
23 |
Correct |
2 ms |
6748 KB |
Output is correct |
24 |
Correct |
2 ms |
6872 KB |
Output is correct |
25 |
Correct |
3 ms |
6748 KB |
Output is correct |
26 |
Correct |
2 ms |
6748 KB |
Output is correct |
27 |
Correct |
2 ms |
6748 KB |
Output is correct |
28 |
Correct |
2 ms |
6872 KB |
Output is correct |
29 |
Correct |
9 ms |
7004 KB |
Output is correct |
30 |
Correct |
4 ms |
6748 KB |
Output is correct |
31 |
Correct |
5 ms |
7000 KB |
Output is correct |
32 |
Correct |
5 ms |
6972 KB |
Output is correct |
33 |
Correct |
2 ms |
6748 KB |
Output is correct |
34 |
Correct |
6 ms |
6748 KB |
Output is correct |
35 |
Correct |
10 ms |
7004 KB |
Output is correct |
36 |
Correct |
7 ms |
6748 KB |
Output is correct |
37 |
Correct |
8 ms |
6748 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
2 ms |
6744 KB |
Output is correct |
2 |
Correct |
1 ms |
6748 KB |
Output is correct |
3 |
Correct |
3 ms |
6748 KB |
Output is correct |
4 |
Correct |
2 ms |
6920 KB |
Output is correct |
5 |
Correct |
2 ms |
6748 KB |
Output is correct |
6 |
Correct |
2 ms |
6748 KB |
Output is correct |
7 |
Correct |
3 ms |
6748 KB |
Output is correct |
8 |
Correct |
2 ms |
6748 KB |
Output is correct |
9 |
Correct |
2 ms |
6748 KB |
Output is correct |
10 |
Correct |
2 ms |
6748 KB |
Output is correct |
11 |
Correct |
2 ms |
6748 KB |
Output is correct |
12 |
Correct |
2 ms |
6748 KB |
Output is correct |
13 |
Correct |
2 ms |
6748 KB |
Output is correct |
14 |
Correct |
1 ms |
6748 KB |
Output is correct |
15 |
Correct |
1 ms |
6748 KB |
Output is correct |
16 |
Correct |
2 ms |
6748 KB |
Output is correct |
17 |
Correct |
2 ms |
6748 KB |
Output is correct |
18 |
Correct |
2 ms |
6748 KB |
Output is correct |
19 |
Correct |
2 ms |
6748 KB |
Output is correct |
20 |
Correct |
2 ms |
6748 KB |
Output is correct |
21 |
Correct |
2 ms |
6748 KB |
Output is correct |
22 |
Correct |
2 ms |
6748 KB |
Output is correct |
23 |
Correct |
2 ms |
6748 KB |
Output is correct |
24 |
Correct |
2 ms |
6872 KB |
Output is correct |
25 |
Correct |
3 ms |
6748 KB |
Output is correct |
26 |
Correct |
2 ms |
6748 KB |
Output is correct |
27 |
Correct |
2 ms |
6748 KB |
Output is correct |
28 |
Correct |
2 ms |
6872 KB |
Output is correct |
29 |
Correct |
9 ms |
7004 KB |
Output is correct |
30 |
Correct |
4 ms |
6748 KB |
Output is correct |
31 |
Correct |
5 ms |
7000 KB |
Output is correct |
32 |
Correct |
5 ms |
6972 KB |
Output is correct |
33 |
Correct |
2 ms |
6748 KB |
Output is correct |
34 |
Correct |
6 ms |
6748 KB |
Output is correct |
35 |
Correct |
10 ms |
7004 KB |
Output is correct |
36 |
Correct |
7 ms |
6748 KB |
Output is correct |
37 |
Correct |
8 ms |
6748 KB |
Output is correct |
38 |
Correct |
556 ms |
33996 KB |
Output is correct |
39 |
Correct |
3204 ms |
495524 KB |
Output is correct |
40 |
Correct |
754 ms |
18004 KB |
Output is correct |
41 |
Correct |
705 ms |
7504 KB |
Output is correct |
42 |
Correct |
364 ms |
17232 KB |
Output is correct |
43 |
Correct |
44 ms |
7260 KB |
Output is correct |
44 |
Correct |
109 ms |
7000 KB |
Output is correct |
45 |
Correct |
1638 ms |
11612 KB |
Output is correct |
46 |
Correct |
1655 ms |
12112 KB |
Output is correct |
47 |
Correct |
1117 ms |
102052 KB |
Output is correct |
48 |
Correct |
1143 ms |
104980 KB |
Output is correct |
49 |
Correct |
1136 ms |
16212 KB |
Output is correct |
50 |
Correct |
1188 ms |
16420 KB |
Output is correct |
51 |
Correct |
184 ms |
12180 KB |
Output is correct |
52 |
Correct |
207 ms |
12112 KB |
Output is correct |
53 |
Correct |
126 ms |
7268 KB |
Output is correct |
54 |
Correct |
1275 ms |
10788 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
2 ms |
6744 KB |
Output is correct |
2 |
Correct |
1 ms |
6840 KB |
Output is correct |
3 |
Correct |
2 ms |
6744 KB |
Output is correct |
4 |
Correct |
1 ms |
6748 KB |
Output is correct |
5 |
Correct |
7 ms |
6748 KB |
Output is correct |
6 |
Correct |
2 ms |
6744 KB |
Output is correct |
7 |
Correct |
2 ms |
6748 KB |
Output is correct |
8 |
Correct |
2 ms |
6748 KB |
Output is correct |
9 |
Correct |
1 ms |
6748 KB |
Output is correct |
10 |
Correct |
2 ms |
6748 KB |
Output is correct |
11 |
Correct |
1 ms |
6744 KB |
Output is correct |
12 |
Correct |
6 ms |
6748 KB |
Output is correct |
13 |
Correct |
39 ms |
6932 KB |
Output is correct |
14 |
Correct |
60 ms |
6904 KB |
Output is correct |
15 |
Correct |
53 ms |
6748 KB |
Output is correct |
16 |
Runtime error |
1155 ms |
1048576 KB |
Execution killed with signal 9 |
17 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
6744 KB |
Output is correct |
2 |
Correct |
1 ms |
6748 KB |
Output is correct |
3 |
Correct |
1 ms |
6748 KB |
Output is correct |
4 |
Correct |
8 ms |
7256 KB |
Output is correct |
5 |
Correct |
14 ms |
6916 KB |
Output is correct |
6 |
Correct |
2 ms |
6748 KB |
Output is correct |
7 |
Correct |
2 ms |
6748 KB |
Output is correct |
8 |
Correct |
10 ms |
7004 KB |
Output is correct |
9 |
Correct |
603 ms |
33872 KB |
Output is correct |
10 |
Correct |
3317 ms |
495376 KB |
Output is correct |
11 |
Correct |
7 ms |
6748 KB |
Output is correct |
12 |
Correct |
109 ms |
7008 KB |
Output is correct |
13 |
Runtime error |
32 ms |
21072 KB |
Execution killed with signal 6 |
14 |
Halted |
0 ms |
0 KB |
- |