Submission #557598

# Submission time Handle Problem Language Result Execution time Memory
557598 2022-05-05T14:55:53 Z DanShaders Jail (JOI22_jail) C++17
100 / 100
507 ms 72716 KB
#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) {
	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;
			g[--u].push_back(--v);
			g[v].push_back(u);
		}
		timer = 0;
		dfs_sz();
		dfs_hld();
		assert(timer == n);

		cin >> m;


		for (int i = 2 * n - 1; i > 0; --i) {
			g2[i / 2].push_back(i);
			g2[2 * n + i].push_back(2 * n + i / 2);
		}

		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) {
				int x = n + l + (l == s ? 1 : 0), y = n + r - (r == s ? 1 : 0);
				while (x <= y) {
					if (x & 1)	g2[u].push_back(x);
					if (!(y & 1))	g2[u].push_back(y);
					x = (x + 1) / 2;
					y = (y - 1) / 2;
				}
				// for (int i = l; i <= r; ++i) {
				// 	if (i != s) {
				// 		g2[u].push_back(n + i);
				// 	}
				// }

				x = n + l + (l == t ? 1 : 0), y = n + r - (r == t ? 1 : 0);
				while (x <= y) {
					if (x & 1)	g2[2 * n + x].push_back(u);
					if (!(y & 1))	g2[2 * n + y].push_back(u);
					x = (x + 1) / 2;
					y = (y - 1) / 2;
				}
				// 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));
			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) {
			if (!used[i]) {
				flag &= dfs_cycle(i);
			}
		}
		cout << (flag ? "Yes\n" : "No\n");
	}
}
# Verdict Execution time Memory Grader output
1 Correct 10 ms 17876 KB Output is correct
2 Correct 10 ms 17924 KB Output is correct
3 Correct 10 ms 17876 KB Output is correct
4 Correct 26 ms 18084 KB Output is correct
5 Correct 35 ms 18012 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 18004 KB Output is correct
9 Correct 47 ms 19816 KB Output is correct
10 Correct 76 ms 47520 KB Output is correct
11 Correct 16 ms 18080 KB Output is correct
12 Correct 47 ms 18236 KB Output is correct
13 Correct 125 ms 52356 KB Output is correct
14 Correct 145 ms 52344 KB Output is correct
15 Correct 235 ms 57928 KB Output is correct
16 Correct 448 ms 72716 KB Output is correct
17 Correct 172 ms 62820 KB Output is correct
18 Correct 116 ms 57456 KB Output is correct
19 Correct 156 ms 61468 KB Output is correct
20 Correct 154 ms 61480 KB Output is correct
21 Correct 182 ms 61856 KB Output is correct
22 Correct 114 ms 52988 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 10 ms 17876 KB Output is correct
2 Correct 11 ms 17876 KB Output is correct
3 Correct 10 ms 17996 KB Output is correct
4 Correct 12 ms 17900 KB Output is correct
5 Correct 11 ms 18004 KB Output is correct
6 Correct 11 ms 17940 KB Output is correct
7 Correct 12 ms 17932 KB Output is correct
8 Correct 11 ms 18004 KB Output is correct
9 Correct 13 ms 18000 KB Output is correct
10 Correct 11 ms 18004 KB Output is correct
11 Correct 13 ms 17956 KB Output is correct
12 Correct 10 ms 17876 KB Output is correct
13 Correct 10 ms 18004 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 10 ms 17876 KB Output is correct
2 Correct 11 ms 17876 KB Output is correct
3 Correct 10 ms 17996 KB Output is correct
4 Correct 12 ms 17900 KB Output is correct
5 Correct 11 ms 18004 KB Output is correct
6 Correct 11 ms 17940 KB Output is correct
7 Correct 12 ms 17932 KB Output is correct
8 Correct 11 ms 18004 KB Output is correct
9 Correct 13 ms 18000 KB Output is correct
10 Correct 11 ms 18004 KB Output is correct
11 Correct 13 ms 17956 KB Output is correct
12 Correct 10 ms 17876 KB Output is correct
13 Correct 10 ms 18004 KB Output is correct
14 Correct 9 ms 17876 KB Output is correct
15 Correct 9 ms 17876 KB Output is correct
16 Correct 10 ms 18004 KB Output is correct
17 Correct 12 ms 17984 KB Output is correct
18 Correct 11 ms 17896 KB Output is correct
19 Correct 10 ms 17908 KB Output is correct
20 Correct 11 ms 18012 KB Output is correct
21 Correct 11 ms 18008 KB Output is correct
22 Correct 11 ms 18024 KB Output is correct
23 Correct 10 ms 17876 KB Output is correct
24 Correct 10 ms 17876 KB Output is correct
25 Correct 12 ms 18004 KB Output is correct
26 Correct 11 ms 17876 KB Output is correct
27 Correct 11 ms 18004 KB Output is correct
28 Correct 11 ms 17876 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 10 ms 17876 KB Output is correct
2 Correct 11 ms 17876 KB Output is correct
3 Correct 10 ms 17996 KB Output is correct
4 Correct 12 ms 17900 KB Output is correct
5 Correct 11 ms 18004 KB Output is correct
6 Correct 11 ms 17940 KB Output is correct
7 Correct 12 ms 17932 KB Output is correct
8 Correct 11 ms 18004 KB Output is correct
9 Correct 13 ms 18000 KB Output is correct
10 Correct 11 ms 18004 KB Output is correct
11 Correct 13 ms 17956 KB Output is correct
12 Correct 10 ms 17876 KB Output is correct
13 Correct 10 ms 18004 KB Output is correct
14 Correct 9 ms 17876 KB Output is correct
15 Correct 9 ms 17876 KB Output is correct
16 Correct 10 ms 18004 KB Output is correct
17 Correct 12 ms 17984 KB Output is correct
18 Correct 11 ms 17896 KB Output is correct
19 Correct 10 ms 17908 KB Output is correct
20 Correct 11 ms 18012 KB Output is correct
21 Correct 11 ms 18008 KB Output is correct
22 Correct 11 ms 18024 KB Output is correct
23 Correct 10 ms 17876 KB Output is correct
24 Correct 10 ms 17876 KB Output is correct
25 Correct 12 ms 18004 KB Output is correct
26 Correct 11 ms 17876 KB Output is correct
27 Correct 11 ms 18004 KB Output is correct
28 Correct 11 ms 17876 KB Output is correct
29 Correct 12 ms 18036 KB Output is correct
30 Correct 11 ms 18004 KB Output is correct
31 Correct 11 ms 18004 KB Output is correct
32 Correct 11 ms 18004 KB Output is correct
33 Correct 13 ms 17888 KB Output is correct
34 Correct 12 ms 18004 KB Output is correct
35 Correct 12 ms 17924 KB Output is correct
36 Correct 12 ms 17904 KB Output is correct
37 Correct 11 ms 18004 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 10 ms 17876 KB Output is correct
2 Correct 11 ms 17876 KB Output is correct
3 Correct 10 ms 17996 KB Output is correct
4 Correct 12 ms 17900 KB Output is correct
5 Correct 11 ms 18004 KB Output is correct
6 Correct 11 ms 17940 KB Output is correct
7 Correct 12 ms 17932 KB Output is correct
8 Correct 11 ms 18004 KB Output is correct
9 Correct 13 ms 18000 KB Output is correct
10 Correct 11 ms 18004 KB Output is correct
11 Correct 13 ms 17956 KB Output is correct
12 Correct 10 ms 17876 KB Output is correct
13 Correct 10 ms 18004 KB Output is correct
14 Correct 9 ms 17876 KB Output is correct
15 Correct 9 ms 17876 KB Output is correct
16 Correct 10 ms 18004 KB Output is correct
17 Correct 12 ms 17984 KB Output is correct
18 Correct 11 ms 17896 KB Output is correct
19 Correct 10 ms 17908 KB Output is correct
20 Correct 11 ms 18012 KB Output is correct
21 Correct 11 ms 18008 KB Output is correct
22 Correct 11 ms 18024 KB Output is correct
23 Correct 10 ms 17876 KB Output is correct
24 Correct 10 ms 17876 KB Output is correct
25 Correct 12 ms 18004 KB Output is correct
26 Correct 11 ms 17876 KB Output is correct
27 Correct 11 ms 18004 KB Output is correct
28 Correct 11 ms 17876 KB Output is correct
29 Correct 12 ms 18036 KB Output is correct
30 Correct 11 ms 18004 KB Output is correct
31 Correct 11 ms 18004 KB Output is correct
32 Correct 11 ms 18004 KB Output is correct
33 Correct 13 ms 17888 KB Output is correct
34 Correct 12 ms 18004 KB Output is correct
35 Correct 12 ms 17924 KB Output is correct
36 Correct 12 ms 17904 KB Output is correct
37 Correct 11 ms 18004 KB Output is correct
38 Correct 50 ms 19644 KB Output is correct
39 Correct 71 ms 47244 KB Output is correct
40 Correct 54 ms 19328 KB Output is correct
41 Correct 56 ms 19312 KB Output is correct
42 Correct 46 ms 19740 KB Output is correct
43 Correct 41 ms 19096 KB Output is correct
44 Correct 21 ms 18260 KB Output is correct
45 Correct 88 ms 35964 KB Output is correct
46 Correct 87 ms 36068 KB Output is correct
47 Correct 76 ms 41552 KB Output is correct
48 Correct 75 ms 41592 KB Output is correct
49 Correct 84 ms 36232 KB Output is correct
50 Correct 88 ms 36180 KB Output is correct
51 Correct 72 ms 37820 KB Output is correct
52 Correct 77 ms 37764 KB Output is correct
53 Correct 26 ms 19532 KB Output is correct
54 Correct 94 ms 36020 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 10 ms 17876 KB Output is correct
2 Correct 9 ms 17876 KB Output is correct
3 Correct 10 ms 17892 KB Output is correct
4 Correct 10 ms 17876 KB Output is correct
5 Correct 17 ms 17972 KB Output is correct
6 Correct 11 ms 18000 KB Output is correct
7 Correct 11 ms 17876 KB Output is correct
8 Correct 10 ms 17932 KB Output is correct
9 Correct 10 ms 17932 KB Output is correct
10 Correct 10 ms 17884 KB Output is correct
11 Correct 10 ms 17876 KB Output is correct
12 Correct 12 ms 18012 KB Output is correct
13 Correct 34 ms 17992 KB Output is correct
14 Correct 51 ms 18000 KB Output is correct
15 Correct 41 ms 17892 KB Output is correct
16 Correct 126 ms 37648 KB Output is correct
17 Correct 241 ms 45632 KB Output is correct
18 Correct 379 ms 55432 KB Output is correct
19 Correct 164 ms 39284 KB Output is correct
20 Correct 157 ms 39264 KB Output is correct
21 Correct 179 ms 39160 KB Output is correct
22 Correct 221 ms 45640 KB Output is correct
23 Correct 173 ms 45632 KB Output is correct
24 Correct 196 ms 45640 KB Output is correct
25 Correct 182 ms 45792 KB Output is correct
26 Correct 187 ms 45812 KB Output is correct
27 Correct 252 ms 50084 KB Output is correct
28 Correct 239 ms 55752 KB Output is correct
29 Correct 223 ms 49604 KB Output is correct
30 Correct 173 ms 43896 KB Output is correct
31 Correct 207 ms 45880 KB Output is correct
32 Correct 224 ms 45512 KB Output is correct
33 Correct 175 ms 45756 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 10 ms 17876 KB Output is correct
2 Correct 10 ms 17924 KB Output is correct
3 Correct 10 ms 17876 KB Output is correct
4 Correct 26 ms 18084 KB Output is correct
5 Correct 35 ms 18012 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 18004 KB Output is correct
9 Correct 47 ms 19816 KB Output is correct
10 Correct 76 ms 47520 KB Output is correct
11 Correct 16 ms 18080 KB Output is correct
12 Correct 47 ms 18236 KB Output is correct
13 Correct 125 ms 52356 KB Output is correct
14 Correct 145 ms 52344 KB Output is correct
15 Correct 235 ms 57928 KB Output is correct
16 Correct 448 ms 72716 KB Output is correct
17 Correct 172 ms 62820 KB Output is correct
18 Correct 116 ms 57456 KB Output is correct
19 Correct 156 ms 61468 KB Output is correct
20 Correct 154 ms 61480 KB Output is correct
21 Correct 182 ms 61856 KB Output is correct
22 Correct 114 ms 52988 KB Output is correct
23 Correct 10 ms 17876 KB Output is correct
24 Correct 11 ms 17876 KB Output is correct
25 Correct 10 ms 17996 KB Output is correct
26 Correct 12 ms 17900 KB Output is correct
27 Correct 11 ms 18004 KB Output is correct
28 Correct 11 ms 17940 KB Output is correct
29 Correct 12 ms 17932 KB Output is correct
30 Correct 11 ms 18004 KB Output is correct
31 Correct 13 ms 18000 KB Output is correct
32 Correct 11 ms 18004 KB Output is correct
33 Correct 13 ms 17956 KB Output is correct
34 Correct 10 ms 17876 KB Output is correct
35 Correct 10 ms 18004 KB Output is correct
36 Correct 9 ms 17876 KB Output is correct
37 Correct 9 ms 17876 KB Output is correct
38 Correct 10 ms 18004 KB Output is correct
39 Correct 12 ms 17984 KB Output is correct
40 Correct 11 ms 17896 KB Output is correct
41 Correct 10 ms 17908 KB Output is correct
42 Correct 11 ms 18012 KB Output is correct
43 Correct 11 ms 18008 KB Output is correct
44 Correct 11 ms 18024 KB Output is correct
45 Correct 10 ms 17876 KB Output is correct
46 Correct 10 ms 17876 KB Output is correct
47 Correct 12 ms 18004 KB Output is correct
48 Correct 11 ms 17876 KB Output is correct
49 Correct 11 ms 18004 KB Output is correct
50 Correct 11 ms 17876 KB Output is correct
51 Correct 12 ms 18036 KB Output is correct
52 Correct 11 ms 18004 KB Output is correct
53 Correct 11 ms 18004 KB Output is correct
54 Correct 11 ms 18004 KB Output is correct
55 Correct 13 ms 17888 KB Output is correct
56 Correct 12 ms 18004 KB Output is correct
57 Correct 12 ms 17924 KB Output is correct
58 Correct 12 ms 17904 KB Output is correct
59 Correct 11 ms 18004 KB Output is correct
60 Correct 50 ms 19644 KB Output is correct
61 Correct 71 ms 47244 KB Output is correct
62 Correct 54 ms 19328 KB Output is correct
63 Correct 56 ms 19312 KB Output is correct
64 Correct 46 ms 19740 KB Output is correct
65 Correct 41 ms 19096 KB Output is correct
66 Correct 21 ms 18260 KB Output is correct
67 Correct 88 ms 35964 KB Output is correct
68 Correct 87 ms 36068 KB Output is correct
69 Correct 76 ms 41552 KB Output is correct
70 Correct 75 ms 41592 KB Output is correct
71 Correct 84 ms 36232 KB Output is correct
72 Correct 88 ms 36180 KB Output is correct
73 Correct 72 ms 37820 KB Output is correct
74 Correct 77 ms 37764 KB Output is correct
75 Correct 26 ms 19532 KB Output is correct
76 Correct 94 ms 36020 KB Output is correct
77 Correct 10 ms 17876 KB Output is correct
78 Correct 9 ms 17876 KB Output is correct
79 Correct 10 ms 17892 KB Output is correct
80 Correct 10 ms 17876 KB Output is correct
81 Correct 17 ms 17972 KB Output is correct
82 Correct 11 ms 18000 KB Output is correct
83 Correct 11 ms 17876 KB Output is correct
84 Correct 10 ms 17932 KB Output is correct
85 Correct 10 ms 17932 KB Output is correct
86 Correct 10 ms 17884 KB Output is correct
87 Correct 10 ms 17876 KB Output is correct
88 Correct 12 ms 18012 KB Output is correct
89 Correct 34 ms 17992 KB Output is correct
90 Correct 51 ms 18000 KB Output is correct
91 Correct 41 ms 17892 KB Output is correct
92 Correct 126 ms 37648 KB Output is correct
93 Correct 241 ms 45632 KB Output is correct
94 Correct 379 ms 55432 KB Output is correct
95 Correct 164 ms 39284 KB Output is correct
96 Correct 157 ms 39264 KB Output is correct
97 Correct 179 ms 39160 KB Output is correct
98 Correct 221 ms 45640 KB Output is correct
99 Correct 173 ms 45632 KB Output is correct
100 Correct 196 ms 45640 KB Output is correct
101 Correct 182 ms 45792 KB Output is correct
102 Correct 187 ms 45812 KB Output is correct
103 Correct 252 ms 50084 KB Output is correct
104 Correct 239 ms 55752 KB Output is correct
105 Correct 223 ms 49604 KB Output is correct
106 Correct 173 ms 43896 KB Output is correct
107 Correct 207 ms 45880 KB Output is correct
108 Correct 224 ms 45512 KB Output is correct
109 Correct 175 ms 45756 KB Output is correct
110 Correct 49 ms 18964 KB Output is correct
111 Correct 37 ms 18484 KB Output is correct
112 Correct 286 ms 55152 KB Output is correct
113 Correct 159 ms 47048 KB Output is correct
114 Correct 229 ms 52104 KB Output is correct
115 Correct 73 ms 37572 KB Output is correct
116 Correct 177 ms 42040 KB Output is correct
117 Correct 507 ms 68344 KB Output is correct
118 Correct 99 ms 37456 KB Output is correct
119 Correct 103 ms 37340 KB Output is correct
120 Correct 21 ms 20000 KB Output is correct
121 Correct 219 ms 43320 KB Output is correct
122 Correct 227 ms 43452 KB Output is correct
123 Correct 168 ms 49680 KB Output is correct
124 Correct 158 ms 49544 KB Output is correct
125 Correct 179 ms 50336 KB Output is correct
126 Correct 428 ms 69836 KB Output is correct
127 Correct 221 ms 58536 KB Output is correct
128 Correct 198 ms 58856 KB Output is correct
129 Correct 203 ms 58704 KB Output is correct
130 Correct 202 ms 58568 KB Output is correct