#include <bits/stdc++.h>
#include <ext/pb_ds/assoc_container.hpp>
using namespace std;
namespace x = __gnu_pbds;
template <typename T>
using ordered_set = x::tree<T, x::null_type, less<T>, x::rb_tree_tag, x::tree_order_statistics_node_update>;
template <typename T>
using normal_queue = priority_queue<T, vector<T>, greater<>>;
#define all(x) begin(x), end(x)
#define sz(x) ((int) (x).size())
#define x first
#define y second
using ll = long long;
using ld = long double;
const int N = 1.25e5, M = 5 * N;
vector<int> g[N], g2[M];
char used[M];
bool dfs_cycle(int u) {
if (used[u] == 1) {
return false;
}
used[u] = 1;
for (int v : g2[u]) {
if (used[v] != 2 && !dfs_cycle(v)) {
return false;
}
}
used[u] = 2;
return true;
}
int sz[N];
int dfs_sz(int u = 0, int p = -1) {
int res = 1;
for (int v : g[u]) {
if (v != p) {
res += dfs_sz(v, u);
}
}
return sz[u] = res;
}
int timer, order[N], lnk[N], tout[N], parent[N];
void dfs_hld(int u = 0, int up = 0, int p = -1) {
// cout << u << " " << up << " " << p << endl;
order[u] = timer;
parent[timer] = p == -1 ? -1 : order[p];
lnk[timer] = order[up];
++timer;
sort(all(g[u]), [](int x, int y) {
return sz[x] > sz[y];
});
bool is_first = true;
for (int v : g[u]) {
if (v != p) {
dfs_hld(v, is_first ? up : v, u);
is_first = false;
}
}
tout[order[u]] = timer;
}
bool in(int x0, int y0, int x1, int y1) {
return x0 <= x1 && y1 <= y0;
}
signed main() {
cin.tie(0)->sync_with_stdio(0);
int testcases;
cin >> testcases;
while (testcases--) {
int n, m;
cin >> n;
for (int i = 0; i < 5 * n; ++i) {
g2[i].clear();
}
for (int i = 0; i < n; ++i) {
g[i].clear();
}
for (int i = 1; i < n; ++i) {
int u, v;
cin >> u >> v;
// cout << u << " " << v << endl;
g[--u].push_back(--v);
g[v].push_back(u);
}
timer = 0;
dfs_sz();
dfs_hld();
// cout << timer << endl;
assert(timer == n);
cin >> m;
for (int person = 0; person < m; ++person) {
int u = 4 * n + person;
int s, t;
cin >> s >> t;
s = order[s - 1], t = order[t - 1];
auto use_cut = [&](int l, int r) {
// cout << l << " " << r << endl;
for (int i = l; i <= r; ++i) {
if (i != s) {
g2[u].push_back(n + i);
}
}
for (int i = l; i <= r; ++i) {
if (i != t) {
g2[3 * n + i].push_back(u);
}
}
};
int a = s, b = t;
while (lnk[a] != lnk[b]) {
if (!in(lnk[a], tout[lnk[a]], b, tout[b])) {
use_cut(lnk[a], a);
a = parent[lnk[a]];
} else {
assert(!in(lnk[b], tout[lnk[b]], a, tout[a]));
use_cut(lnk[b], b);
b = parent[lnk[b]];
}
}
use_cut(min(a, b), max(a, b));
// path.clear();
// dfs(s[i], t[i]);
// for (int u : path) {
// if (u != s[i]) {
// g2[4 * n + i].push_back(n + u);
// }
// }
// for (int u : path) {
// if (u != t[i]) {
// g2[3 * n + u].push_back(4 * n + i);
// }
// }
g2[n + s].push_back(u);
g2[u].push_back(3 * n + t);
}
fill(used, used + 5 * n, 0);
bool flag = true;
for (int i = 0; i < 5 * n; ++i) {
// for (int j : g2[i]) {
// cout << i << " -> " << j << endl;
// }
if (!used[i]) {
flag &= dfs_cycle(i);
}
}
cout << (flag ? "Yes\n" : "No\n");
}
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
13 ms |
17920 KB |
Output is correct |
2 |
Correct |
12 ms |
17904 KB |
Output is correct |
3 |
Correct |
10 ms |
17948 KB |
Output is correct |
4 |
Correct |
20 ms |
18004 KB |
Output is correct |
5 |
Correct |
36 ms |
18004 KB |
Output is correct |
6 |
Correct |
11 ms |
18004 KB |
Output is correct |
7 |
Correct |
11 ms |
17944 KB |
Output is correct |
8 |
Correct |
13 ms |
18132 KB |
Output is correct |
9 |
Correct |
225 ms |
46204 KB |
Output is correct |
10 |
Correct |
1594 ms |
513020 KB |
Output is correct |
11 |
Correct |
16 ms |
18004 KB |
Output is correct |
12 |
Correct |
51 ms |
18184 KB |
Output is correct |
13 |
Correct |
254 ms |
111276 KB |
Output is correct |
14 |
Correct |
278 ms |
143964 KB |
Output is correct |
15 |
Runtime error |
2451 ms |
1048576 KB |
Execution killed with signal 9 |
16 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
10 ms |
17936 KB |
Output is correct |
2 |
Correct |
10 ms |
17868 KB |
Output is correct |
3 |
Correct |
11 ms |
18004 KB |
Output is correct |
4 |
Correct |
11 ms |
18004 KB |
Output is correct |
5 |
Correct |
12 ms |
18008 KB |
Output is correct |
6 |
Correct |
11 ms |
18004 KB |
Output is correct |
7 |
Correct |
12 ms |
18004 KB |
Output is correct |
8 |
Correct |
12 ms |
18016 KB |
Output is correct |
9 |
Correct |
11 ms |
17948 KB |
Output is correct |
10 |
Correct |
11 ms |
18004 KB |
Output is correct |
11 |
Correct |
11 ms |
18004 KB |
Output is correct |
12 |
Correct |
10 ms |
17876 KB |
Output is correct |
13 |
Correct |
12 ms |
17876 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
10 ms |
17936 KB |
Output is correct |
2 |
Correct |
10 ms |
17868 KB |
Output is correct |
3 |
Correct |
11 ms |
18004 KB |
Output is correct |
4 |
Correct |
11 ms |
18004 KB |
Output is correct |
5 |
Correct |
12 ms |
18008 KB |
Output is correct |
6 |
Correct |
11 ms |
18004 KB |
Output is correct |
7 |
Correct |
12 ms |
18004 KB |
Output is correct |
8 |
Correct |
12 ms |
18016 KB |
Output is correct |
9 |
Correct |
11 ms |
17948 KB |
Output is correct |
10 |
Correct |
11 ms |
18004 KB |
Output is correct |
11 |
Correct |
11 ms |
18004 KB |
Output is correct |
12 |
Correct |
10 ms |
17876 KB |
Output is correct |
13 |
Correct |
12 ms |
17876 KB |
Output is correct |
14 |
Correct |
10 ms |
17876 KB |
Output is correct |
15 |
Correct |
10 ms |
17876 KB |
Output is correct |
16 |
Correct |
13 ms |
17948 KB |
Output is correct |
17 |
Correct |
12 ms |
17952 KB |
Output is correct |
18 |
Correct |
13 ms |
18004 KB |
Output is correct |
19 |
Correct |
12 ms |
17940 KB |
Output is correct |
20 |
Correct |
11 ms |
18004 KB |
Output is correct |
21 |
Correct |
11 ms |
18004 KB |
Output is correct |
22 |
Correct |
11 ms |
17948 KB |
Output is correct |
23 |
Correct |
11 ms |
18060 KB |
Output is correct |
24 |
Correct |
13 ms |
17876 KB |
Output is correct |
25 |
Correct |
13 ms |
17936 KB |
Output is correct |
26 |
Correct |
10 ms |
17940 KB |
Output is correct |
27 |
Correct |
11 ms |
18004 KB |
Output is correct |
28 |
Correct |
10 ms |
17880 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
10 ms |
17936 KB |
Output is correct |
2 |
Correct |
10 ms |
17868 KB |
Output is correct |
3 |
Correct |
11 ms |
18004 KB |
Output is correct |
4 |
Correct |
11 ms |
18004 KB |
Output is correct |
5 |
Correct |
12 ms |
18008 KB |
Output is correct |
6 |
Correct |
11 ms |
18004 KB |
Output is correct |
7 |
Correct |
12 ms |
18004 KB |
Output is correct |
8 |
Correct |
12 ms |
18016 KB |
Output is correct |
9 |
Correct |
11 ms |
17948 KB |
Output is correct |
10 |
Correct |
11 ms |
18004 KB |
Output is correct |
11 |
Correct |
11 ms |
18004 KB |
Output is correct |
12 |
Correct |
10 ms |
17876 KB |
Output is correct |
13 |
Correct |
12 ms |
17876 KB |
Output is correct |
14 |
Correct |
10 ms |
17876 KB |
Output is correct |
15 |
Correct |
10 ms |
17876 KB |
Output is correct |
16 |
Correct |
13 ms |
17948 KB |
Output is correct |
17 |
Correct |
12 ms |
17952 KB |
Output is correct |
18 |
Correct |
13 ms |
18004 KB |
Output is correct |
19 |
Correct |
12 ms |
17940 KB |
Output is correct |
20 |
Correct |
11 ms |
18004 KB |
Output is correct |
21 |
Correct |
11 ms |
18004 KB |
Output is correct |
22 |
Correct |
11 ms |
17948 KB |
Output is correct |
23 |
Correct |
11 ms |
18060 KB |
Output is correct |
24 |
Correct |
13 ms |
17876 KB |
Output is correct |
25 |
Correct |
13 ms |
17936 KB |
Output is correct |
26 |
Correct |
10 ms |
17940 KB |
Output is correct |
27 |
Correct |
11 ms |
18004 KB |
Output is correct |
28 |
Correct |
10 ms |
17880 KB |
Output is correct |
29 |
Correct |
14 ms |
18260 KB |
Output is correct |
30 |
Correct |
12 ms |
18088 KB |
Output is correct |
31 |
Correct |
13 ms |
18076 KB |
Output is correct |
32 |
Correct |
11 ms |
18004 KB |
Output is correct |
33 |
Correct |
12 ms |
17952 KB |
Output is correct |
34 |
Correct |
12 ms |
18004 KB |
Output is correct |
35 |
Correct |
14 ms |
18104 KB |
Output is correct |
36 |
Correct |
14 ms |
18020 KB |
Output is correct |
37 |
Correct |
11 ms |
17952 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
10 ms |
17936 KB |
Output is correct |
2 |
Correct |
10 ms |
17868 KB |
Output is correct |
3 |
Correct |
11 ms |
18004 KB |
Output is correct |
4 |
Correct |
11 ms |
18004 KB |
Output is correct |
5 |
Correct |
12 ms |
18008 KB |
Output is correct |
6 |
Correct |
11 ms |
18004 KB |
Output is correct |
7 |
Correct |
12 ms |
18004 KB |
Output is correct |
8 |
Correct |
12 ms |
18016 KB |
Output is correct |
9 |
Correct |
11 ms |
17948 KB |
Output is correct |
10 |
Correct |
11 ms |
18004 KB |
Output is correct |
11 |
Correct |
11 ms |
18004 KB |
Output is correct |
12 |
Correct |
10 ms |
17876 KB |
Output is correct |
13 |
Correct |
12 ms |
17876 KB |
Output is correct |
14 |
Correct |
10 ms |
17876 KB |
Output is correct |
15 |
Correct |
10 ms |
17876 KB |
Output is correct |
16 |
Correct |
13 ms |
17948 KB |
Output is correct |
17 |
Correct |
12 ms |
17952 KB |
Output is correct |
18 |
Correct |
13 ms |
18004 KB |
Output is correct |
19 |
Correct |
12 ms |
17940 KB |
Output is correct |
20 |
Correct |
11 ms |
18004 KB |
Output is correct |
21 |
Correct |
11 ms |
18004 KB |
Output is correct |
22 |
Correct |
11 ms |
17948 KB |
Output is correct |
23 |
Correct |
11 ms |
18060 KB |
Output is correct |
24 |
Correct |
13 ms |
17876 KB |
Output is correct |
25 |
Correct |
13 ms |
17936 KB |
Output is correct |
26 |
Correct |
10 ms |
17940 KB |
Output is correct |
27 |
Correct |
11 ms |
18004 KB |
Output is correct |
28 |
Correct |
10 ms |
17880 KB |
Output is correct |
29 |
Correct |
14 ms |
18260 KB |
Output is correct |
30 |
Correct |
12 ms |
18088 KB |
Output is correct |
31 |
Correct |
13 ms |
18076 KB |
Output is correct |
32 |
Correct |
11 ms |
18004 KB |
Output is correct |
33 |
Correct |
12 ms |
17952 KB |
Output is correct |
34 |
Correct |
12 ms |
18004 KB |
Output is correct |
35 |
Correct |
14 ms |
18104 KB |
Output is correct |
36 |
Correct |
14 ms |
18020 KB |
Output is correct |
37 |
Correct |
11 ms |
17952 KB |
Output is correct |
38 |
Correct |
232 ms |
46200 KB |
Output is correct |
39 |
Correct |
1579 ms |
513712 KB |
Output is correct |
40 |
Correct |
101 ms |
27772 KB |
Output is correct |
41 |
Correct |
48 ms |
20000 KB |
Output is correct |
42 |
Correct |
97 ms |
29712 KB |
Output is correct |
43 |
Correct |
39 ms |
19560 KB |
Output is correct |
44 |
Correct |
18 ms |
18500 KB |
Output is correct |
45 |
Correct |
73 ms |
27040 KB |
Output is correct |
46 |
Correct |
68 ms |
27028 KB |
Output is correct |
47 |
Correct |
259 ms |
119160 KB |
Output is correct |
48 |
Correct |
272 ms |
121636 KB |
Output is correct |
49 |
Correct |
71 ms |
31756 KB |
Output is correct |
50 |
Correct |
73 ms |
31732 KB |
Output is correct |
51 |
Correct |
55 ms |
26908 KB |
Output is correct |
52 |
Correct |
51 ms |
27724 KB |
Output is correct |
53 |
Correct |
21 ms |
18600 KB |
Output is correct |
54 |
Correct |
81 ms |
26324 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
9 ms |
17876 KB |
Output is correct |
2 |
Correct |
11 ms |
17904 KB |
Output is correct |
3 |
Correct |
10 ms |
17876 KB |
Output is correct |
4 |
Correct |
10 ms |
17872 KB |
Output is correct |
5 |
Correct |
16 ms |
18004 KB |
Output is correct |
6 |
Correct |
10 ms |
17876 KB |
Output is correct |
7 |
Correct |
11 ms |
17976 KB |
Output is correct |
8 |
Correct |
11 ms |
17956 KB |
Output is correct |
9 |
Correct |
11 ms |
17936 KB |
Output is correct |
10 |
Correct |
11 ms |
17876 KB |
Output is correct |
11 |
Correct |
10 ms |
17876 KB |
Output is correct |
12 |
Correct |
11 ms |
18004 KB |
Output is correct |
13 |
Correct |
33 ms |
18408 KB |
Output is correct |
14 |
Correct |
45 ms |
18732 KB |
Output is correct |
15 |
Correct |
38 ms |
18604 KB |
Output is correct |
16 |
Correct |
98 ms |
28872 KB |
Output is correct |
17 |
Correct |
185 ms |
40132 KB |
Output is correct |
18 |
Correct |
320 ms |
57336 KB |
Output is correct |
19 |
Correct |
128 ms |
31588 KB |
Output is correct |
20 |
Correct |
114 ms |
30924 KB |
Output is correct |
21 |
Correct |
129 ms |
31144 KB |
Output is correct |
22 |
Correct |
179 ms |
42356 KB |
Output is correct |
23 |
Correct |
140 ms |
42008 KB |
Output is correct |
24 |
Correct |
139 ms |
41356 KB |
Output is correct |
25 |
Correct |
145 ms |
41660 KB |
Output is correct |
26 |
Correct |
152 ms |
43324 KB |
Output is correct |
27 |
Correct |
178 ms |
48048 KB |
Output is correct |
28 |
Correct |
184 ms |
49496 KB |
Output is correct |
29 |
Correct |
181 ms |
44632 KB |
Output is correct |
30 |
Correct |
146 ms |
39300 KB |
Output is correct |
31 |
Correct |
159 ms |
39632 KB |
Output is correct |
32 |
Correct |
152 ms |
35656 KB |
Output is correct |
33 |
Correct |
149 ms |
37580 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
13 ms |
17920 KB |
Output is correct |
2 |
Correct |
12 ms |
17904 KB |
Output is correct |
3 |
Correct |
10 ms |
17948 KB |
Output is correct |
4 |
Correct |
20 ms |
18004 KB |
Output is correct |
5 |
Correct |
36 ms |
18004 KB |
Output is correct |
6 |
Correct |
11 ms |
18004 KB |
Output is correct |
7 |
Correct |
11 ms |
17944 KB |
Output is correct |
8 |
Correct |
13 ms |
18132 KB |
Output is correct |
9 |
Correct |
225 ms |
46204 KB |
Output is correct |
10 |
Correct |
1594 ms |
513020 KB |
Output is correct |
11 |
Correct |
16 ms |
18004 KB |
Output is correct |
12 |
Correct |
51 ms |
18184 KB |
Output is correct |
13 |
Correct |
254 ms |
111276 KB |
Output is correct |
14 |
Correct |
278 ms |
143964 KB |
Output is correct |
15 |
Runtime error |
2451 ms |
1048576 KB |
Execution killed with signal 9 |
16 |
Halted |
0 ms |
0 KB |
- |