Submission #544592

# Submission time Handle Problem Language Result Execution time Memory
544592 2022-04-02T13:16:14 Z sidon Jail (JOI22_jail) C++17
100 / 100
3028 ms 429608 KB
#include <bits/stdc++.h>
using namespace std;

const int Z = 1.2e5, B = 17;

int N, M;
vector<array<int, 3>> g[Z][B+1][2];
int p[Z][B], d[Z];

void dfs(int u) {
	for(const auto &[v, j, k] : g[u][B][0]) if(v != p[u][0])
		p[v][0] = u, d[v] = d[u] + 1, dfs(v);
}

int lca(int u, int v) {
	if(d[u] < d[v]) swap(u, v);
	for(int i = B; i--; )
		if((d[u] - d[v]) & (1<<i)) u = p[u][i];
	if(u == v) return u;

	for(int i = B; i--; )
		if(p[u][i] != p[v][i]) u = p[u][i], v = p[v][i];
	return p[u][0];
}

int getWithout(int u, int v) {
	if(d[u] < d[v] && lca(u, v) == u) {
		for(int i = B; i--; ) if((d[v] - d[u] - 1) & (1<<i)) v = p[v][i];
		return v;
	}
	return p[u][0];
}

int cur, ord[Z][B+1][2];
bool vis[Z][B+1][2];

void tarjan(int i, int j, int k) {
	vis[i][j][k] = 1;
	for(const auto &[x, y, z] : g[i][j][k])
		if(!vis[x][y][z]) tarjan(x, y, z);
	ord[i][j][k] = --cur;
}

int main() {
	ios::sync_with_stdio(0), cin.tie(0);
	int T; cin >> T;
	while(T--) {
		cin >> N;
		for(int i = 0; i < N; ++i)
			for(int j = 0; j <= B; ++j)
				for(int k = 0; k < 2; ++k)
					g[i][j][k].clear();
		for(int i = 1; i < N; ++i) {
			int u, v; cin >> u >> v;
			--u, --v;
			g[u][B][0].push_back({v, B, 0});
			g[v][B][0].push_back({u, B, 0});
		}

		dfs(0);
		for(int i = 1; i < B; ++i) {
			for(int u = 0; u < N; ++u) {
				p[u][i] = p[p[u][i-1]][i-1];
				for(int w : {u, p[u][i-1]}) {
					g[u][i][0].push_back({w, i-1, 0});
					g[w][i-1][1].push_back({u, i, 1});
				}
			}
		}
		for(int i = 0; i < N; ++i) g[i][B][0].clear();

		cin >> M;
		for(int i = 0; i < M; ++i) {
			int s, t; cin >> s >> t;
			--s, --t;

			g[i][B][0].push_back({s, 0, 1});
			g[t][0][0].push_back({i, B, 0});

			auto jump = [&](int &w, int j, int k) {
				if(!k) g[w][j][1].push_back({i, B, 0});
				if(k)  g[i][B][0].push_back({w, j, 0});
				w = p[w][j];
			};

			for(int k : {0, 1}) {
				int u = s, v = t;
				if(k) v = getWithout(v, u);
				else  u = getWithout(u, v);
				if(d[u] < d[v]) swap(u, v);

				for(int j = B; j--; ) if((d[u] - d[v]) & (1<<j)) {
					jump(u, j, k);
				}
				if(u != v) {
					for(int j = B; j--; ) if(p[u][j] != p[v][j]) {
						jump(u, j, k);
						jump(v, j, k);
					}
					jump(u, 0, k);
					jump(v, 0, k);
				}
				jump(u, 0, k);
			}
		}

		for(int i = 0; i < N; ++i)
			for(int j = 0; j <= B; ++j)
				vis[i][j][0] = vis[i][j][1] = 0;

		cur = 1<<30;
		for(int i = 0; i < N; ++i)
			for(int j = 0; j <= B; ++j)
				for(int k = 0; k < 2; ++k)
					if(!vis[i][j][k]) tarjan(i, j, k);

		bool ans = 1;

		for(int i = 0; i < N; ++i)
			for(int j = 0; j <= B; ++j)
				for(int k = 0; k < 2; ++k)
					for(const auto &[x, y, z] : g[i][j][k])
						if(ord[x][y][z] < ord[i][j][k]) ans = 0;

		cout << (ans ? "Yes" : "No") << '\n';
	}
}
# Verdict Execution time Memory Grader output
1 Correct 51 ms 101788 KB Output is correct
2 Correct 58 ms 101760 KB Output is correct
3 Correct 53 ms 101724 KB Output is correct
4 Correct 142 ms 101980 KB Output is correct
5 Correct 242 ms 101992 KB Output is correct
6 Correct 80 ms 102092 KB Output is correct
7 Correct 67 ms 102100 KB Output is correct
8 Correct 60 ms 102132 KB Output is correct
9 Correct 543 ms 112012 KB Output is correct
10 Correct 1168 ms 264044 KB Output is correct
11 Correct 90 ms 101776 KB Output is correct
12 Correct 278 ms 101988 KB Output is correct
13 Correct 1304 ms 280596 KB Output is correct
14 Correct 1219 ms 281244 KB Output is correct
15 Correct 2250 ms 334444 KB Output is correct
16 Correct 3028 ms 429608 KB Output is correct
17 Correct 1650 ms 314376 KB Output is correct
18 Correct 1223 ms 272260 KB Output is correct
19 Correct 1700 ms 307848 KB Output is correct
20 Correct 1605 ms 307884 KB Output is correct
21 Correct 1676 ms 342580 KB Output is correct
22 Correct 1018 ms 268500 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 71 ms 101756 KB Output is correct
2 Correct 58 ms 101776 KB Output is correct
3 Correct 77 ms 102180 KB Output is correct
4 Correct 65 ms 102196 KB Output is correct
5 Correct 64 ms 102220 KB Output is correct
6 Correct 76 ms 102176 KB Output is correct
7 Correct 71 ms 102144 KB Output is correct
8 Correct 83 ms 102148 KB Output is correct
9 Correct 67 ms 102252 KB Output is correct
10 Correct 68 ms 102212 KB Output is correct
11 Correct 67 ms 102244 KB Output is correct
12 Correct 72 ms 102104 KB Output is correct
13 Correct 84 ms 102056 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 71 ms 101756 KB Output is correct
2 Correct 58 ms 101776 KB Output is correct
3 Correct 77 ms 102180 KB Output is correct
4 Correct 65 ms 102196 KB Output is correct
5 Correct 64 ms 102220 KB Output is correct
6 Correct 76 ms 102176 KB Output is correct
7 Correct 71 ms 102144 KB Output is correct
8 Correct 83 ms 102148 KB Output is correct
9 Correct 67 ms 102252 KB Output is correct
10 Correct 68 ms 102212 KB Output is correct
11 Correct 67 ms 102244 KB Output is correct
12 Correct 72 ms 102104 KB Output is correct
13 Correct 84 ms 102056 KB Output is correct
14 Correct 57 ms 101772 KB Output is correct
15 Correct 73 ms 101756 KB Output is correct
16 Correct 78 ms 102116 KB Output is correct
17 Correct 98 ms 102240 KB Output is correct
18 Correct 67 ms 102284 KB Output is correct
19 Correct 78 ms 101788 KB Output is correct
20 Correct 63 ms 102268 KB Output is correct
21 Correct 74 ms 102268 KB Output is correct
22 Correct 71 ms 102284 KB Output is correct
23 Correct 56 ms 101716 KB Output is correct
24 Correct 56 ms 101980 KB Output is correct
25 Correct 103 ms 102200 KB Output is correct
26 Correct 89 ms 102124 KB Output is correct
27 Correct 66 ms 102240 KB Output is correct
28 Correct 61 ms 101772 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 71 ms 101756 KB Output is correct
2 Correct 58 ms 101776 KB Output is correct
3 Correct 77 ms 102180 KB Output is correct
4 Correct 65 ms 102196 KB Output is correct
5 Correct 64 ms 102220 KB Output is correct
6 Correct 76 ms 102176 KB Output is correct
7 Correct 71 ms 102144 KB Output is correct
8 Correct 83 ms 102148 KB Output is correct
9 Correct 67 ms 102252 KB Output is correct
10 Correct 68 ms 102212 KB Output is correct
11 Correct 67 ms 102244 KB Output is correct
12 Correct 72 ms 102104 KB Output is correct
13 Correct 84 ms 102056 KB Output is correct
14 Correct 57 ms 101772 KB Output is correct
15 Correct 73 ms 101756 KB Output is correct
16 Correct 78 ms 102116 KB Output is correct
17 Correct 98 ms 102240 KB Output is correct
18 Correct 67 ms 102284 KB Output is correct
19 Correct 78 ms 101788 KB Output is correct
20 Correct 63 ms 102268 KB Output is correct
21 Correct 74 ms 102268 KB Output is correct
22 Correct 71 ms 102284 KB Output is correct
23 Correct 56 ms 101716 KB Output is correct
24 Correct 56 ms 101980 KB Output is correct
25 Correct 103 ms 102200 KB Output is correct
26 Correct 89 ms 102124 KB Output is correct
27 Correct 66 ms 102240 KB Output is correct
28 Correct 61 ms 101772 KB Output is correct
29 Correct 72 ms 102280 KB Output is correct
30 Correct 70 ms 102384 KB Output is correct
31 Correct 64 ms 102280 KB Output is correct
32 Correct 97 ms 102376 KB Output is correct
33 Correct 83 ms 102324 KB Output is correct
34 Correct 82 ms 102092 KB Output is correct
35 Correct 71 ms 102252 KB Output is correct
36 Correct 72 ms 102240 KB Output is correct
37 Correct 99 ms 102148 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 71 ms 101756 KB Output is correct
2 Correct 58 ms 101776 KB Output is correct
3 Correct 77 ms 102180 KB Output is correct
4 Correct 65 ms 102196 KB Output is correct
5 Correct 64 ms 102220 KB Output is correct
6 Correct 76 ms 102176 KB Output is correct
7 Correct 71 ms 102144 KB Output is correct
8 Correct 83 ms 102148 KB Output is correct
9 Correct 67 ms 102252 KB Output is correct
10 Correct 68 ms 102212 KB Output is correct
11 Correct 67 ms 102244 KB Output is correct
12 Correct 72 ms 102104 KB Output is correct
13 Correct 84 ms 102056 KB Output is correct
14 Correct 57 ms 101772 KB Output is correct
15 Correct 73 ms 101756 KB Output is correct
16 Correct 78 ms 102116 KB Output is correct
17 Correct 98 ms 102240 KB Output is correct
18 Correct 67 ms 102284 KB Output is correct
19 Correct 78 ms 101788 KB Output is correct
20 Correct 63 ms 102268 KB Output is correct
21 Correct 74 ms 102268 KB Output is correct
22 Correct 71 ms 102284 KB Output is correct
23 Correct 56 ms 101716 KB Output is correct
24 Correct 56 ms 101980 KB Output is correct
25 Correct 103 ms 102200 KB Output is correct
26 Correct 89 ms 102124 KB Output is correct
27 Correct 66 ms 102240 KB Output is correct
28 Correct 61 ms 101772 KB Output is correct
29 Correct 72 ms 102280 KB Output is correct
30 Correct 70 ms 102384 KB Output is correct
31 Correct 64 ms 102280 KB Output is correct
32 Correct 97 ms 102376 KB Output is correct
33 Correct 83 ms 102324 KB Output is correct
34 Correct 82 ms 102092 KB Output is correct
35 Correct 71 ms 102252 KB Output is correct
36 Correct 72 ms 102240 KB Output is correct
37 Correct 99 ms 102148 KB Output is correct
38 Correct 649 ms 113212 KB Output is correct
39 Correct 1330 ms 265204 KB Output is correct
40 Correct 570 ms 118112 KB Output is correct
41 Correct 542 ms 113444 KB Output is correct
42 Correct 425 ms 115068 KB Output is correct
43 Correct 556 ms 116940 KB Output is correct
44 Correct 159 ms 104004 KB Output is correct
45 Correct 1219 ms 282076 KB Output is correct
46 Correct 1227 ms 282056 KB Output is correct
47 Correct 1437 ms 283852 KB Output is correct
48 Correct 1521 ms 283848 KB Output is correct
49 Correct 1049 ms 284532 KB Output is correct
50 Correct 1166 ms 284416 KB Output is correct
51 Correct 1199 ms 285956 KB Output is correct
52 Correct 1166 ms 285912 KB Output is correct
53 Correct 176 ms 115600 KB Output is correct
54 Correct 1236 ms 283664 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 69 ms 101788 KB Output is correct
2 Correct 72 ms 101756 KB Output is correct
3 Correct 70 ms 101704 KB Output is correct
4 Correct 57 ms 101752 KB Output is correct
5 Correct 95 ms 101952 KB Output is correct
6 Correct 63 ms 102120 KB Output is correct
7 Correct 65 ms 102148 KB Output is correct
8 Correct 60 ms 101796 KB Output is correct
9 Correct 68 ms 101796 KB Output is correct
10 Correct 61 ms 101924 KB Output is correct
11 Correct 63 ms 101752 KB Output is correct
12 Correct 70 ms 102276 KB Output is correct
13 Correct 174 ms 102548 KB Output is correct
14 Correct 251 ms 102768 KB Output is correct
15 Correct 233 ms 102732 KB Output is correct
16 Correct 1066 ms 283920 KB Output is correct
17 Correct 1398 ms 302444 KB Output is correct
18 Correct 1816 ms 337008 KB Output is correct
19 Correct 1145 ms 286388 KB Output is correct
20 Correct 1102 ms 285912 KB Output is correct
21 Correct 1150 ms 285696 KB Output is correct
22 Correct 1227 ms 299468 KB Output is correct
23 Correct 1206 ms 294460 KB Output is correct
24 Correct 1167 ms 297776 KB Output is correct
25 Correct 1250 ms 298460 KB Output is correct
26 Correct 1345 ms 298088 KB Output is correct
27 Correct 1033 ms 291976 KB Output is correct
28 Correct 1231 ms 303504 KB Output is correct
29 Correct 1132 ms 295436 KB Output is correct
30 Correct 1109 ms 291932 KB Output is correct
31 Correct 1082 ms 297344 KB Output is correct
32 Correct 1014 ms 288432 KB Output is correct
33 Correct 939 ms 294128 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 51 ms 101788 KB Output is correct
2 Correct 58 ms 101760 KB Output is correct
3 Correct 53 ms 101724 KB Output is correct
4 Correct 142 ms 101980 KB Output is correct
5 Correct 242 ms 101992 KB Output is correct
6 Correct 80 ms 102092 KB Output is correct
7 Correct 67 ms 102100 KB Output is correct
8 Correct 60 ms 102132 KB Output is correct
9 Correct 543 ms 112012 KB Output is correct
10 Correct 1168 ms 264044 KB Output is correct
11 Correct 90 ms 101776 KB Output is correct
12 Correct 278 ms 101988 KB Output is correct
13 Correct 1304 ms 280596 KB Output is correct
14 Correct 1219 ms 281244 KB Output is correct
15 Correct 2250 ms 334444 KB Output is correct
16 Correct 3028 ms 429608 KB Output is correct
17 Correct 1650 ms 314376 KB Output is correct
18 Correct 1223 ms 272260 KB Output is correct
19 Correct 1700 ms 307848 KB Output is correct
20 Correct 1605 ms 307884 KB Output is correct
21 Correct 1676 ms 342580 KB Output is correct
22 Correct 1018 ms 268500 KB Output is correct
23 Correct 71 ms 101756 KB Output is correct
24 Correct 58 ms 101776 KB Output is correct
25 Correct 77 ms 102180 KB Output is correct
26 Correct 65 ms 102196 KB Output is correct
27 Correct 64 ms 102220 KB Output is correct
28 Correct 76 ms 102176 KB Output is correct
29 Correct 71 ms 102144 KB Output is correct
30 Correct 83 ms 102148 KB Output is correct
31 Correct 67 ms 102252 KB Output is correct
32 Correct 68 ms 102212 KB Output is correct
33 Correct 67 ms 102244 KB Output is correct
34 Correct 72 ms 102104 KB Output is correct
35 Correct 84 ms 102056 KB Output is correct
36 Correct 57 ms 101772 KB Output is correct
37 Correct 73 ms 101756 KB Output is correct
38 Correct 78 ms 102116 KB Output is correct
39 Correct 98 ms 102240 KB Output is correct
40 Correct 67 ms 102284 KB Output is correct
41 Correct 78 ms 101788 KB Output is correct
42 Correct 63 ms 102268 KB Output is correct
43 Correct 74 ms 102268 KB Output is correct
44 Correct 71 ms 102284 KB Output is correct
45 Correct 56 ms 101716 KB Output is correct
46 Correct 56 ms 101980 KB Output is correct
47 Correct 103 ms 102200 KB Output is correct
48 Correct 89 ms 102124 KB Output is correct
49 Correct 66 ms 102240 KB Output is correct
50 Correct 61 ms 101772 KB Output is correct
51 Correct 72 ms 102280 KB Output is correct
52 Correct 70 ms 102384 KB Output is correct
53 Correct 64 ms 102280 KB Output is correct
54 Correct 97 ms 102376 KB Output is correct
55 Correct 83 ms 102324 KB Output is correct
56 Correct 82 ms 102092 KB Output is correct
57 Correct 71 ms 102252 KB Output is correct
58 Correct 72 ms 102240 KB Output is correct
59 Correct 99 ms 102148 KB Output is correct
60 Correct 649 ms 113212 KB Output is correct
61 Correct 1330 ms 265204 KB Output is correct
62 Correct 570 ms 118112 KB Output is correct
63 Correct 542 ms 113444 KB Output is correct
64 Correct 425 ms 115068 KB Output is correct
65 Correct 556 ms 116940 KB Output is correct
66 Correct 159 ms 104004 KB Output is correct
67 Correct 1219 ms 282076 KB Output is correct
68 Correct 1227 ms 282056 KB Output is correct
69 Correct 1437 ms 283852 KB Output is correct
70 Correct 1521 ms 283848 KB Output is correct
71 Correct 1049 ms 284532 KB Output is correct
72 Correct 1166 ms 284416 KB Output is correct
73 Correct 1199 ms 285956 KB Output is correct
74 Correct 1166 ms 285912 KB Output is correct
75 Correct 176 ms 115600 KB Output is correct
76 Correct 1236 ms 283664 KB Output is correct
77 Correct 69 ms 101788 KB Output is correct
78 Correct 72 ms 101756 KB Output is correct
79 Correct 70 ms 101704 KB Output is correct
80 Correct 57 ms 101752 KB Output is correct
81 Correct 95 ms 101952 KB Output is correct
82 Correct 63 ms 102120 KB Output is correct
83 Correct 65 ms 102148 KB Output is correct
84 Correct 60 ms 101796 KB Output is correct
85 Correct 68 ms 101796 KB Output is correct
86 Correct 61 ms 101924 KB Output is correct
87 Correct 63 ms 101752 KB Output is correct
88 Correct 70 ms 102276 KB Output is correct
89 Correct 174 ms 102548 KB Output is correct
90 Correct 251 ms 102768 KB Output is correct
91 Correct 233 ms 102732 KB Output is correct
92 Correct 1066 ms 283920 KB Output is correct
93 Correct 1398 ms 302444 KB Output is correct
94 Correct 1816 ms 337008 KB Output is correct
95 Correct 1145 ms 286388 KB Output is correct
96 Correct 1102 ms 285912 KB Output is correct
97 Correct 1150 ms 285696 KB Output is correct
98 Correct 1227 ms 299468 KB Output is correct
99 Correct 1206 ms 294460 KB Output is correct
100 Correct 1167 ms 297776 KB Output is correct
101 Correct 1250 ms 298460 KB Output is correct
102 Correct 1345 ms 298088 KB Output is correct
103 Correct 1033 ms 291976 KB Output is correct
104 Correct 1231 ms 303504 KB Output is correct
105 Correct 1132 ms 295436 KB Output is correct
106 Correct 1109 ms 291932 KB Output is correct
107 Correct 1082 ms 297344 KB Output is correct
108 Correct 1014 ms 288432 KB Output is correct
109 Correct 939 ms 294128 KB Output is correct
110 Correct 281 ms 102680 KB Output is correct
111 Correct 234 ms 102304 KB Output is correct
112 Correct 2192 ms 355056 KB Output is correct
113 Correct 1603 ms 291988 KB Output is correct
114 Correct 1462 ms 330332 KB Output is correct
115 Correct 683 ms 279964 KB Output is correct
116 Correct 1261 ms 287604 KB Output is correct
117 Correct 2028 ms 348584 KB Output is correct
118 Correct 1180 ms 283416 KB Output is correct
119 Correct 1142 ms 283536 KB Output is correct
120 Correct 179 ms 117840 KB Output is correct
121 Correct 1496 ms 292968 KB Output is correct
122 Correct 1541 ms 293028 KB Output is correct
123 Correct 1738 ms 292632 KB Output is correct
124 Correct 1752 ms 292508 KB Output is correct
125 Correct 1797 ms 293876 KB Output is correct
126 Correct 2729 ms 423184 KB Output is correct
127 Correct 2159 ms 377272 KB Output is correct
128 Correct 1687 ms 365952 KB Output is correct
129 Correct 1824 ms 343696 KB Output is correct
130 Correct 1783 ms 369652 KB Output is correct