Submission #564193

# Submission time Handle Problem Language Result Execution time Memory
564193 2022-05-18T17:26:45 Z cheissmart Jail (JOI22_jail) C++14
100 / 100
1227 ms 364672 KB
#include <bits/stdc++.h>
#define IO_OP ios::sync_with_stdio(0), cin.tie(0)
#define F first
#define S second
#define V vector
#define PB push_back
#define EB emplace_back
#define MP make_pair
#define SZ(v) int((v).size())
#define ALL(v) (v).begin(), (v).end()

using namespace std;

typedef long long ll;
typedef pair<int, int> pi;
typedef V<int> vi;

const int INF = 1e9 + 7;

bool solve() {
	int n; cin >> n;
	V<vi> G(n);
	for(int i = 0; i < n - 1; i++) {
		int u, v;
		cin >> u >> v, u--, v--;
		G[u].PB(v);
		G[v].PB(u);
	}
	int m; cin >> m;
	vi sid(n, -1), tid(n, -1), s(m), t(m);
	for(int i = 0; i < m; i++) {
		cin >> s[i] >> t[i], s[i]--, t[i]--;
		sid[s[i]] = i, tid[t[i]] = i;
	}

	int cnt = m;

	vi d(n);
	V<vi> p(n, vi(20, -1)), idt(n, vi(20)), ids(n, vi(20));
	function<void(int, int)> dfs = [&] (int u, int pa) {
		p[u][0] = pa;
		if(p[u][0] != -1) {
			idt[u][0] = tid[u];
			ids[u][0] = sid[u];
		}
		for(int v:G[u]) if(v != pa) {
			d[v] = d[u] + 1;
			dfs(v, u);
		}
	};
	dfs(0, -1);
	V<pi> es;
	for(int j = 1; j < 20; j++) {
		for(int i = 0; i < n; i++) if(p[i][j - 1] != -1) {
			p[i][j] = p[p[i][j - 1]][j - 1];
			if(p[i][j] != -1) {
				ids[i][j] = cnt++;
				idt[i][j] = cnt++;
				es.EB(idt[i][j], idt[i][j - 1]), es.EB(idt[i][j], idt[p[i][j - 1]][j - 1]);
				es.EB(ids[i][j - 1], ids[i][j]), es.EB(ids[p[i][j - 1]][j - 1], ids[i][j]);
			}			
		}
	}

	auto up = [&] (int u, int step) {
		for(int i = 0; i < 20; i++) if(step >> i & 1)
			u = p[u][i];
		return u;
	};

	auto lca = [&] (int u, int v) {
		if(d[u] > d[v]) swap(u, v);
		for(int i = 0; i < 20; i++)
			if((d[v] - d[u]) >> i & 1)
				v = p[v][i];
		if(u == v) return u;
		for(int i = 19; i >= 0; i--)
			if(p[u][i] != p[v][i])
				u = p[u][i], v = p[v][i];
		assert(u != v && p[u][0] == p[v][0] && d[u] == d[v]);
		return p[u][0];
	};

	for(int i = 0; i < m; i++) {
		{
			int u = s[i], v = t[i], l = lca(u, v);
			if(l == v)
				l = v = up(u, d[u] - d[v] - 1);
			else
				v = p[v][0];
			for(int j = 0; j < 20; j++) if((d[u] - d[l]) >> j & 1) {
				es.EB(i, idt[u][j]);
				u = p[u][j];
			}
			assert(u == l);
			for(int j = 0; j < 20; j++) if((d[v] - d[l]) >> j & 1) {
				es.EB(i, idt[v][j]);
				v = p[v][j];
			}
			assert(v == l);
			es.EB(i, tid[l]);
		}
		{
			int u = s[i], v = t[i], l = lca(u, v);
			if(l == u)
				l = u = up(v, d[v] - d[u] - 1);
			else
				u = p[u][0];
			for(int j = 0; j < 20; j++) if((d[u] - d[l]) >> j & 1) {
				es.EB(ids[u][j], i);
				u = p[u][j];
			}
			assert(u == l);
			for(int j = 0; j < 20; j++) if((d[v] - d[l]) >> j & 1) {
				es.EB(ids[v][j], i);
				v = p[v][j];
			}
			assert(v == l);
			es.EB(sid[l], i);
		}
	}

	V<vi> g(cnt);
	for(auto[u, v]:es) {
		if(u != -1 && v != -1) {
			g[u].PB(v);
		}
	}
	vi vis(cnt);

	function<bool(int)> dfs2 = [&] (int u) {
		vis[u] = 1;
		for(int v:g[u]) {
			if(!vis[v]) {
				if(!dfs2(v))
					return false;
			} else if(vis[v] == 1)
				return false;
		}
		vis[u] = 2;
		return true;
	};
	for(int i = 0; i < cnt; i++) if(vis[i] == 0) {
		if(!dfs2(i))
			return false;
	}
	return true;
}

signed main()
{
	IO_OP;

	int t;
	cin >> t;
	while(t--)
		cout << (solve() ? "Yes" : "No") << '\n';

}

Compilation message

jail.cpp: In function 'bool solve()':
jail.cpp:124:10: warning: structured bindings only available with '-std=c++17' or '-std=gnu++17'
  124 |  for(auto[u, v]:es) {
      |          ^
# Verdict Execution time Memory Grader output
1 Correct 1 ms 340 KB Output is correct
2 Correct 1 ms 212 KB Output is correct
3 Correct 1 ms 320 KB Output is correct
4 Correct 70 ms 808 KB Output is correct
5 Correct 147 ms 1116 KB Output is correct
6 Correct 8 ms 684 KB Output is correct
7 Correct 8 ms 724 KB Output is correct
8 Correct 12 ms 736 KB Output is correct
9 Correct 335 ms 15984 KB Output is correct
10 Correct 729 ms 324080 KB Output is correct
11 Correct 22 ms 468 KB Output is correct
12 Correct 183 ms 1500 KB Output is correct
13 Correct 771 ms 336100 KB Output is correct
14 Correct 579 ms 336220 KB Output is correct
15 Correct 796 ms 341004 KB Output is correct
16 Correct 1227 ms 364672 KB Output is correct
17 Correct 756 ms 346332 KB Output is correct
18 Correct 662 ms 338868 KB Output is correct
19 Correct 696 ms 344460 KB Output is correct
20 Correct 622 ms 344524 KB Output is correct
21 Correct 589 ms 344048 KB Output is correct
22 Correct 483 ms 333160 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1 ms 212 KB Output is correct
2 Correct 1 ms 212 KB Output is correct
3 Correct 11 ms 684 KB Output is correct
4 Correct 6 ms 596 KB Output is correct
5 Correct 6 ms 592 KB Output is correct
6 Correct 8 ms 592 KB Output is correct
7 Correct 7 ms 612 KB Output is correct
8 Correct 6 ms 636 KB Output is correct
9 Correct 6 ms 596 KB Output is correct
10 Correct 6 ms 596 KB Output is correct
11 Correct 5 ms 596 KB Output is correct
12 Correct 2 ms 332 KB Output is correct
13 Correct 3 ms 468 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1 ms 212 KB Output is correct
2 Correct 1 ms 212 KB Output is correct
3 Correct 11 ms 684 KB Output is correct
4 Correct 6 ms 596 KB Output is correct
5 Correct 6 ms 592 KB Output is correct
6 Correct 8 ms 592 KB Output is correct
7 Correct 7 ms 612 KB Output is correct
8 Correct 6 ms 636 KB Output is correct
9 Correct 6 ms 596 KB Output is correct
10 Correct 6 ms 596 KB Output is correct
11 Correct 5 ms 596 KB Output is correct
12 Correct 2 ms 332 KB Output is correct
13 Correct 3 ms 468 KB Output is correct
14 Correct 1 ms 212 KB Output is correct
15 Correct 1 ms 212 KB Output is correct
16 Correct 8 ms 724 KB Output is correct
17 Correct 4 ms 468 KB Output is correct
18 Correct 7 ms 596 KB Output is correct
19 Correct 1 ms 340 KB Output is correct
20 Correct 7 ms 724 KB Output is correct
21 Correct 7 ms 596 KB Output is correct
22 Correct 6 ms 468 KB Output is correct
23 Correct 1 ms 316 KB Output is correct
24 Correct 1 ms 340 KB Output is correct
25 Correct 8 ms 716 KB Output is correct
26 Correct 2 ms 468 KB Output is correct
27 Correct 5 ms 468 KB Output is correct
28 Correct 1 ms 340 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1 ms 212 KB Output is correct
2 Correct 1 ms 212 KB Output is correct
3 Correct 11 ms 684 KB Output is correct
4 Correct 6 ms 596 KB Output is correct
5 Correct 6 ms 592 KB Output is correct
6 Correct 8 ms 592 KB Output is correct
7 Correct 7 ms 612 KB Output is correct
8 Correct 6 ms 636 KB Output is correct
9 Correct 6 ms 596 KB Output is correct
10 Correct 6 ms 596 KB Output is correct
11 Correct 5 ms 596 KB Output is correct
12 Correct 2 ms 332 KB Output is correct
13 Correct 3 ms 468 KB Output is correct
14 Correct 1 ms 212 KB Output is correct
15 Correct 1 ms 212 KB Output is correct
16 Correct 8 ms 724 KB Output is correct
17 Correct 4 ms 468 KB Output is correct
18 Correct 7 ms 596 KB Output is correct
19 Correct 1 ms 340 KB Output is correct
20 Correct 7 ms 724 KB Output is correct
21 Correct 7 ms 596 KB Output is correct
22 Correct 6 ms 468 KB Output is correct
23 Correct 1 ms 316 KB Output is correct
24 Correct 1 ms 340 KB Output is correct
25 Correct 8 ms 716 KB Output is correct
26 Correct 2 ms 468 KB Output is correct
27 Correct 5 ms 468 KB Output is correct
28 Correct 1 ms 340 KB Output is correct
29 Correct 10 ms 720 KB Output is correct
30 Correct 5 ms 596 KB Output is correct
31 Correct 8 ms 596 KB Output is correct
32 Correct 9 ms 688 KB Output is correct
33 Correct 7 ms 596 KB Output is correct
34 Correct 5 ms 548 KB Output is correct
35 Correct 9 ms 608 KB Output is correct
36 Correct 4 ms 468 KB Output is correct
37 Correct 4 ms 468 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1 ms 212 KB Output is correct
2 Correct 1 ms 212 KB Output is correct
3 Correct 11 ms 684 KB Output is correct
4 Correct 6 ms 596 KB Output is correct
5 Correct 6 ms 592 KB Output is correct
6 Correct 8 ms 592 KB Output is correct
7 Correct 7 ms 612 KB Output is correct
8 Correct 6 ms 636 KB Output is correct
9 Correct 6 ms 596 KB Output is correct
10 Correct 6 ms 596 KB Output is correct
11 Correct 5 ms 596 KB Output is correct
12 Correct 2 ms 332 KB Output is correct
13 Correct 3 ms 468 KB Output is correct
14 Correct 1 ms 212 KB Output is correct
15 Correct 1 ms 212 KB Output is correct
16 Correct 8 ms 724 KB Output is correct
17 Correct 4 ms 468 KB Output is correct
18 Correct 7 ms 596 KB Output is correct
19 Correct 1 ms 340 KB Output is correct
20 Correct 7 ms 724 KB Output is correct
21 Correct 7 ms 596 KB Output is correct
22 Correct 6 ms 468 KB Output is correct
23 Correct 1 ms 316 KB Output is correct
24 Correct 1 ms 340 KB Output is correct
25 Correct 8 ms 716 KB Output is correct
26 Correct 2 ms 468 KB Output is correct
27 Correct 5 ms 468 KB Output is correct
28 Correct 1 ms 340 KB Output is correct
29 Correct 10 ms 720 KB Output is correct
30 Correct 5 ms 596 KB Output is correct
31 Correct 8 ms 596 KB Output is correct
32 Correct 9 ms 688 KB Output is correct
33 Correct 7 ms 596 KB Output is correct
34 Correct 5 ms 548 KB Output is correct
35 Correct 9 ms 608 KB Output is correct
36 Correct 4 ms 468 KB Output is correct
37 Correct 4 ms 468 KB Output is correct
38 Correct 407 ms 15348 KB Output is correct
39 Correct 705 ms 323848 KB Output is correct
40 Correct 206 ms 13148 KB Output is correct
41 Correct 131 ms 6812 KB Output is correct
42 Correct 125 ms 7888 KB Output is correct
43 Correct 308 ms 14644 KB Output is correct
44 Correct 25 ms 1568 KB Output is correct
45 Correct 280 ms 141032 KB Output is correct
46 Correct 359 ms 141612 KB Output is correct
47 Correct 687 ms 299696 KB Output is correct
48 Correct 688 ms 299732 KB Output is correct
49 Correct 361 ms 217612 KB Output is correct
50 Correct 444 ms 217772 KB Output is correct
51 Correct 585 ms 270424 KB Output is correct
52 Correct 469 ms 271656 KB Output is correct
53 Correct 37 ms 8396 KB Output is correct
54 Correct 394 ms 122116 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1 ms 212 KB Output is correct
2 Correct 1 ms 212 KB Output is correct
3 Correct 1 ms 212 KB Output is correct
4 Correct 1 ms 212 KB Output is correct
5 Correct 29 ms 480 KB Output is correct
6 Correct 2 ms 340 KB Output is correct
7 Correct 2 ms 468 KB Output is correct
8 Correct 1 ms 212 KB Output is correct
9 Correct 1 ms 212 KB Output is correct
10 Correct 1 ms 340 KB Output is correct
11 Correct 1 ms 340 KB Output is correct
12 Correct 5 ms 468 KB Output is correct
13 Correct 66 ms 972 KB Output is correct
14 Correct 111 ms 1168 KB Output is correct
15 Correct 91 ms 1048 KB Output is correct
16 Correct 266 ms 97428 KB Output is correct
17 Correct 386 ms 109792 KB Output is correct
18 Correct 573 ms 125452 KB Output is correct
19 Correct 359 ms 100052 KB Output is correct
20 Correct 291 ms 97124 KB Output is correct
21 Correct 369 ms 97684 KB Output is correct
22 Correct 332 ms 107508 KB Output is correct
23 Correct 348 ms 107044 KB Output is correct
24 Correct 363 ms 105992 KB Output is correct
25 Correct 339 ms 107340 KB Output is correct
26 Correct 397 ms 108504 KB Output is correct
27 Correct 363 ms 79692 KB Output is correct
28 Correct 341 ms 85656 KB Output is correct
29 Correct 376 ms 82948 KB Output is correct
30 Correct 275 ms 79404 KB Output is correct
31 Correct 307 ms 80344 KB Output is correct
32 Correct 297 ms 77108 KB Output is correct
33 Correct 287 ms 80196 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1 ms 340 KB Output is correct
2 Correct 1 ms 212 KB Output is correct
3 Correct 1 ms 320 KB Output is correct
4 Correct 70 ms 808 KB Output is correct
5 Correct 147 ms 1116 KB Output is correct
6 Correct 8 ms 684 KB Output is correct
7 Correct 8 ms 724 KB Output is correct
8 Correct 12 ms 736 KB Output is correct
9 Correct 335 ms 15984 KB Output is correct
10 Correct 729 ms 324080 KB Output is correct
11 Correct 22 ms 468 KB Output is correct
12 Correct 183 ms 1500 KB Output is correct
13 Correct 771 ms 336100 KB Output is correct
14 Correct 579 ms 336220 KB Output is correct
15 Correct 796 ms 341004 KB Output is correct
16 Correct 1227 ms 364672 KB Output is correct
17 Correct 756 ms 346332 KB Output is correct
18 Correct 662 ms 338868 KB Output is correct
19 Correct 696 ms 344460 KB Output is correct
20 Correct 622 ms 344524 KB Output is correct
21 Correct 589 ms 344048 KB Output is correct
22 Correct 483 ms 333160 KB Output is correct
23 Correct 1 ms 212 KB Output is correct
24 Correct 1 ms 212 KB Output is correct
25 Correct 11 ms 684 KB Output is correct
26 Correct 6 ms 596 KB Output is correct
27 Correct 6 ms 592 KB Output is correct
28 Correct 8 ms 592 KB Output is correct
29 Correct 7 ms 612 KB Output is correct
30 Correct 6 ms 636 KB Output is correct
31 Correct 6 ms 596 KB Output is correct
32 Correct 6 ms 596 KB Output is correct
33 Correct 5 ms 596 KB Output is correct
34 Correct 2 ms 332 KB Output is correct
35 Correct 3 ms 468 KB Output is correct
36 Correct 1 ms 212 KB Output is correct
37 Correct 1 ms 212 KB Output is correct
38 Correct 8 ms 724 KB Output is correct
39 Correct 4 ms 468 KB Output is correct
40 Correct 7 ms 596 KB Output is correct
41 Correct 1 ms 340 KB Output is correct
42 Correct 7 ms 724 KB Output is correct
43 Correct 7 ms 596 KB Output is correct
44 Correct 6 ms 468 KB Output is correct
45 Correct 1 ms 316 KB Output is correct
46 Correct 1 ms 340 KB Output is correct
47 Correct 8 ms 716 KB Output is correct
48 Correct 2 ms 468 KB Output is correct
49 Correct 5 ms 468 KB Output is correct
50 Correct 1 ms 340 KB Output is correct
51 Correct 10 ms 720 KB Output is correct
52 Correct 5 ms 596 KB Output is correct
53 Correct 8 ms 596 KB Output is correct
54 Correct 9 ms 688 KB Output is correct
55 Correct 7 ms 596 KB Output is correct
56 Correct 5 ms 548 KB Output is correct
57 Correct 9 ms 608 KB Output is correct
58 Correct 4 ms 468 KB Output is correct
59 Correct 4 ms 468 KB Output is correct
60 Correct 407 ms 15348 KB Output is correct
61 Correct 705 ms 323848 KB Output is correct
62 Correct 206 ms 13148 KB Output is correct
63 Correct 131 ms 6812 KB Output is correct
64 Correct 125 ms 7888 KB Output is correct
65 Correct 308 ms 14644 KB Output is correct
66 Correct 25 ms 1568 KB Output is correct
67 Correct 280 ms 141032 KB Output is correct
68 Correct 359 ms 141612 KB Output is correct
69 Correct 687 ms 299696 KB Output is correct
70 Correct 688 ms 299732 KB Output is correct
71 Correct 361 ms 217612 KB Output is correct
72 Correct 444 ms 217772 KB Output is correct
73 Correct 585 ms 270424 KB Output is correct
74 Correct 469 ms 271656 KB Output is correct
75 Correct 37 ms 8396 KB Output is correct
76 Correct 394 ms 122116 KB Output is correct
77 Correct 1 ms 212 KB Output is correct
78 Correct 1 ms 212 KB Output is correct
79 Correct 1 ms 212 KB Output is correct
80 Correct 1 ms 212 KB Output is correct
81 Correct 29 ms 480 KB Output is correct
82 Correct 2 ms 340 KB Output is correct
83 Correct 2 ms 468 KB Output is correct
84 Correct 1 ms 212 KB Output is correct
85 Correct 1 ms 212 KB Output is correct
86 Correct 1 ms 340 KB Output is correct
87 Correct 1 ms 340 KB Output is correct
88 Correct 5 ms 468 KB Output is correct
89 Correct 66 ms 972 KB Output is correct
90 Correct 111 ms 1168 KB Output is correct
91 Correct 91 ms 1048 KB Output is correct
92 Correct 266 ms 97428 KB Output is correct
93 Correct 386 ms 109792 KB Output is correct
94 Correct 573 ms 125452 KB Output is correct
95 Correct 359 ms 100052 KB Output is correct
96 Correct 291 ms 97124 KB Output is correct
97 Correct 369 ms 97684 KB Output is correct
98 Correct 332 ms 107508 KB Output is correct
99 Correct 348 ms 107044 KB Output is correct
100 Correct 363 ms 105992 KB Output is correct
101 Correct 339 ms 107340 KB Output is correct
102 Correct 397 ms 108504 KB Output is correct
103 Correct 363 ms 79692 KB Output is correct
104 Correct 341 ms 85656 KB Output is correct
105 Correct 376 ms 82948 KB Output is correct
106 Correct 275 ms 79404 KB Output is correct
107 Correct 307 ms 80344 KB Output is correct
108 Correct 297 ms 77108 KB Output is correct
109 Correct 287 ms 80196 KB Output is correct
110 Correct 112 ms 1492 KB Output is correct
111 Correct 94 ms 980 KB Output is correct
112 Correct 894 ms 324452 KB Output is correct
113 Correct 1040 ms 307236 KB Output is correct
114 Correct 550 ms 195680 KB Output is correct
115 Correct 80 ms 52476 KB Output is correct
116 Correct 482 ms 126600 KB Output is correct
117 Correct 791 ms 151848 KB Output is correct
118 Correct 370 ms 117644 KB Output is correct
119 Correct 335 ms 117760 KB Output is correct
120 Correct 31 ms 11852 KB Output is correct
121 Correct 645 ms 151824 KB Output is correct
122 Correct 651 ms 145188 KB Output is correct
123 Correct 1121 ms 317840 KB Output is correct
124 Correct 724 ms 317696 KB Output is correct
125 Correct 1008 ms 318952 KB Output is correct
126 Correct 1214 ms 355340 KB Output is correct
127 Correct 744 ms 320324 KB Output is correct
128 Correct 679 ms 320064 KB Output is correct
129 Correct 830 ms 320476 KB Output is correct
130 Correct 686 ms 320556 KB Output is correct