Submission #885006

# Submission time Handle Problem Language Result Execution time Memory
885006 2023-12-08T19:33:31 Z MinaRagy06 Jail (JOI22_jail) C++17
100 / 100
519 ms 48068 KB
#include <bits/stdc++.h>
using namespace std;
#define ll long long
#define md ((l + r) >> 1)

const int N = 120'005;
vector<int> adj[N];
int st[N], en[N], h[N], anc[N][17], t;
void dfs(int i, int par, int d = 0) {
	anc[i][0] = par;
	for (int j = 1; j < 17; j++) {
		anc[i][j] = anc[anc[i][j - 1]][j - 1];
	}
	h[i] = d;
	st[i] = t++;
	for (auto nxt : adj[i]) {
		if (nxt == par) continue;
		dfs(nxt, i, d + 1);
	}
	en[i] = t - 1;
}
int getlca(int u, int v) {
	if (st[u] <= st[v] && en[v] <= en[u]) {
		return u;
	}
	for (int j = 16; j >= 0; j--) {
		int nxtu = anc[u][j];
		if (!(st[nxtu] <= st[v] && en[v] <= en[nxtu])) {
			u = nxtu;
		}
	}
	return anc[u][0];
}
struct path {
	int s, e, lca;
} a[N];
const int inf = 1e9;
int n, m;
struct segtree {
	array<int, 2> seg[1 << 18];
	void build(int i, int l, int r) {
		if (l == r) {
			seg[i] = {-inf, -inf};
			return;
		}
		build(i << 1, l, md);
		build(i << 1 | 1, md + 1, r);
		seg[i] = max(seg[i << 1], seg[i << 1 | 1]);
	}
	void assign(int i, int l, int r, int x, array<int, 2> v) {
		if (l == r) {
			seg[i] = v;
			return;
		}
		if (x <= md) {
			assign(i << 1, l, md, x, v);
		} else {
			assign(i << 1 | 1, md + 1, r, x, v);
		}
		seg[i] = max(seg[i << 1], seg[i << 1 | 1]);
	}
	void erase(int i, int l, int r, int x) {
		if (l == r) {
			seg[i] = {-inf, -inf};
			return;
		}
		if (x <= md) {
			erase(i << 1, l, md, x);
		} else {
			erase(i << 1 | 1, md + 1, r, x);
		}
		seg[i] = max(seg[i << 1], seg[i << 1 | 1]);
	}
	array<int, 2> query(int i, int l, int r, int s, int e) {
		if (s <= l && r <= e) {
			return seg[i];
		}
		if (r < s || e < l) {
			return {-inf, -inf};
		}
		return max(query(i << 1, l, md, s, e), query(i << 1 | 1, md + 1, r, s, e));
	}
	void assign(int i, array<int, 2> v) {
		assign(1, 0, n - 1, i, v);
	}
	void erase(int i) {
		erase(1, 0, n - 1, i);
	}
	array<int, 2> query(int l, int r) {
		array<int, 2> ret = query(1, 0, n - 1, l, r);
		return ret;
	}
	void init() {
		build(1, 0, n - 1);
	}
} seg1, seg2, seg3;
bool vis[N], instack[N];
void assign(int i) {
	vis[i] = 0;
	seg1.assign(st[a[i].s], {en[a[i].s], i});
	seg2.assign(st[a[i].s], {-st[a[i].lca], i});
	seg3.assign(st[a[i].e], {-st[a[i].lca], i});
}
void rem(int i) {
	vis[i] = 1;
	seg1.erase(st[a[i].s]);
	seg2.erase(st[a[i].s]);
	seg3.erase(st[a[i].e]);
}
bool dfs2(int i) {
	instack[i] = 1;
	bool ok = 1;
	rem(i);
	auto go = [&] (int j) {
		if (instack[j]) {
			ok = 0;
			return;
		}
		assign(i);
		bool ret = dfs2(j);
		rem(i);
		if (!ret) {
			ok = 0;
		}
	};
	array<int, 2> val;
	val = seg1.query(st[a[i].lca], st[a[i].s]);
	while (ok && val[0] >= st[a[i].s]) {
		int j = val[1];
		go(j);
		val = seg1.query(st[a[i].lca], st[a[i].s]);
	}
	
	val = seg1.query(st[a[i].lca], st[a[i].e]);
	while (ok && val[0] >= st[a[i].e]) {
		int j = val[1];
		go(j);
		val = seg1.query(st[a[i].lca], st[a[i].e]);
	}

	val = seg2.query(st[a[i].e], en[a[i].e]);
	val[0] *= -1;
	while (ok && val[0] <= st[a[i].e]) {
		int j = val[1];
		go(j);
		val = seg2.query(st[a[i].e], en[a[i].e]);
		val[0] *= -1;
	}
	
	val = seg3.query(st[a[i].e], en[a[i].e]);
	val[0] *= -1;
	while (ok && val[0] <= st[a[i].e]) {
		int j = val[1];
		go(j);
		val = seg3.query(st[a[i].e], en[a[i].e]);
		val[0] *= -1;
	}
	instack[i] = 0;
	return ok;
// 	for (int j = 0; j < m; j++) {
// 		if (j == i) continue;
// 		if (vis[j]) continue;
// 		bool gud = 0;
// 		gud |= st[a[i].lca] <= st[a[j].s] && st[a[j].s] <= st[a[i].s] && en[a[j].s] >= st[a[i].s]; //segtree 1
// 		gud |= st[a[i].lca] <= st[a[j].s] && st[a[j].s] <= st[a[i].e] && en[a[j].s] >= st[a[i].e]; //segtree 1
// 		gud |= st[a[i].e] <= st[a[j].s] && st[a[j].s] <= en[a[i].e] && st[a[j].lca] <= st[a[i].e]; //segtree 2
// 		gud |= st[a[i].e] <= st[a[j].e] && st[a[j].e] <= en[a[i].e] && st[a[j].lca] <= st[a[i].e]; //segtree 3
// 		if (gud) {
// 			if (instack[j]) {
// 				ok = 0;
// 				break;
// 			}
// 			if (!dfs2(j)) {
// 				ok = 0;
// 				break;
// 			}
// 		}
// 	}
}
int main() {
	ios_base::sync_with_stdio(0), cin.tie(0);
	int T;
	cin >> T;
	while (T--) {
		cin >> n;
		for (int i = 0; i < n; i++) {
			adj[i].clear();
			vis[i] = 0;
			instack[i] = 0;
		}
		for (int i = 1, u, v; i < n; i++) {
			cin >> u >> v;
			u--, v--;
			adj[u].push_back(v);
			adj[v].push_back(u);
		}
		t = 0;
		dfs(0, 0);
		cin >> m;
		for (int i = 0; i < m; i++) {
			cin >> a[i].s >> a[i].e;
			a[i].s--, a[i].e--;
			a[i].lca = getlca(a[i].s, a[i].e);
// 			cout << a[i].s + 1 << ' ' << a[i].e + 1 << ' ' << a[i].lca + 1 << '\n';
		}
		seg1.init();
		seg2.init();
		seg3.init();
		for (int i = 0; i < m; i++) {
			assign(i);
		}
		bool ok = 1;
		for (int i = 0; i < m; i++) {
			if (vis[i]) continue;
			if (!dfs2(i)) {
				ok = 0;
			}
		}
		cout << (ok? "Yes\n" : "No\n");
	}
	return 0;
}

# Verdict Execution time Memory Grader output
1 Correct 2 ms 13916 KB Output is correct
2 Correct 2 ms 13916 KB Output is correct
3 Correct 3 ms 13916 KB Output is correct
4 Correct 15 ms 14424 KB Output is correct
5 Correct 24 ms 14684 KB Output is correct
6 Correct 3 ms 14172 KB Output is correct
7 Correct 3 ms 14172 KB Output is correct
8 Correct 6 ms 14196 KB Output is correct
9 Correct 41 ms 15740 KB Output is correct
10 Correct 40 ms 33108 KB Output is correct
11 Correct 10 ms 14172 KB Output is correct
12 Correct 64 ms 15116 KB Output is correct
13 Correct 124 ms 33620 KB Output is correct
14 Correct 123 ms 33620 KB Output is correct
15 Correct 226 ms 33740 KB Output is correct
16 Correct 462 ms 34504 KB Output is correct
17 Correct 142 ms 33848 KB Output is correct
18 Correct 245 ms 47652 KB Output is correct
19 Correct 137 ms 33616 KB Output is correct
20 Correct 156 ms 33872 KB Output is correct
21 Correct 161 ms 33796 KB Output is correct
22 Correct 109 ms 33760 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 2 ms 13916 KB Output is correct
2 Correct 2 ms 13916 KB Output is correct
3 Correct 4 ms 14172 KB Output is correct
4 Correct 4 ms 14148 KB Output is correct
5 Correct 3 ms 14168 KB Output is correct
6 Correct 3 ms 14172 KB Output is correct
7 Correct 3 ms 14172 KB Output is correct
8 Correct 3 ms 14172 KB Output is correct
9 Correct 4 ms 14172 KB Output is correct
10 Correct 3 ms 14172 KB Output is correct
11 Correct 3 ms 14172 KB Output is correct
12 Correct 3 ms 14172 KB Output is correct
13 Correct 3 ms 14172 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 2 ms 13916 KB Output is correct
2 Correct 2 ms 13916 KB Output is correct
3 Correct 4 ms 14172 KB Output is correct
4 Correct 4 ms 14148 KB Output is correct
5 Correct 3 ms 14168 KB Output is correct
6 Correct 3 ms 14172 KB Output is correct
7 Correct 3 ms 14172 KB Output is correct
8 Correct 3 ms 14172 KB Output is correct
9 Correct 4 ms 14172 KB Output is correct
10 Correct 3 ms 14172 KB Output is correct
11 Correct 3 ms 14172 KB Output is correct
12 Correct 3 ms 14172 KB Output is correct
13 Correct 3 ms 14172 KB Output is correct
14 Correct 3 ms 13912 KB Output is correct
15 Correct 3 ms 14144 KB Output is correct
16 Correct 3 ms 14172 KB Output is correct
17 Correct 4 ms 14172 KB Output is correct
18 Correct 4 ms 14172 KB Output is correct
19 Correct 3 ms 13916 KB Output is correct
20 Correct 3 ms 14172 KB Output is correct
21 Correct 3 ms 14172 KB Output is correct
22 Correct 3 ms 14092 KB Output is correct
23 Correct 3 ms 14172 KB Output is correct
24 Correct 3 ms 14164 KB Output is correct
25 Correct 3 ms 14172 KB Output is correct
26 Correct 3 ms 14172 KB Output is correct
27 Correct 3 ms 14172 KB Output is correct
28 Correct 3 ms 14172 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 2 ms 13916 KB Output is correct
2 Correct 2 ms 13916 KB Output is correct
3 Correct 4 ms 14172 KB Output is correct
4 Correct 4 ms 14148 KB Output is correct
5 Correct 3 ms 14168 KB Output is correct
6 Correct 3 ms 14172 KB Output is correct
7 Correct 3 ms 14172 KB Output is correct
8 Correct 3 ms 14172 KB Output is correct
9 Correct 4 ms 14172 KB Output is correct
10 Correct 3 ms 14172 KB Output is correct
11 Correct 3 ms 14172 KB Output is correct
12 Correct 3 ms 14172 KB Output is correct
13 Correct 3 ms 14172 KB Output is correct
14 Correct 3 ms 13912 KB Output is correct
15 Correct 3 ms 14144 KB Output is correct
16 Correct 3 ms 14172 KB Output is correct
17 Correct 4 ms 14172 KB Output is correct
18 Correct 4 ms 14172 KB Output is correct
19 Correct 3 ms 13916 KB Output is correct
20 Correct 3 ms 14172 KB Output is correct
21 Correct 3 ms 14172 KB Output is correct
22 Correct 3 ms 14092 KB Output is correct
23 Correct 3 ms 14172 KB Output is correct
24 Correct 3 ms 14164 KB Output is correct
25 Correct 3 ms 14172 KB Output is correct
26 Correct 3 ms 14172 KB Output is correct
27 Correct 3 ms 14172 KB Output is correct
28 Correct 3 ms 14172 KB Output is correct
29 Correct 6 ms 14172 KB Output is correct
30 Correct 6 ms 14172 KB Output is correct
31 Correct 5 ms 14172 KB Output is correct
32 Correct 4 ms 14168 KB Output is correct
33 Correct 4 ms 14168 KB Output is correct
34 Correct 5 ms 14172 KB Output is correct
35 Correct 5 ms 14172 KB Output is correct
36 Correct 6 ms 14232 KB Output is correct
37 Correct 4 ms 14172 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 2 ms 13916 KB Output is correct
2 Correct 2 ms 13916 KB Output is correct
3 Correct 4 ms 14172 KB Output is correct
4 Correct 4 ms 14148 KB Output is correct
5 Correct 3 ms 14168 KB Output is correct
6 Correct 3 ms 14172 KB Output is correct
7 Correct 3 ms 14172 KB Output is correct
8 Correct 3 ms 14172 KB Output is correct
9 Correct 4 ms 14172 KB Output is correct
10 Correct 3 ms 14172 KB Output is correct
11 Correct 3 ms 14172 KB Output is correct
12 Correct 3 ms 14172 KB Output is correct
13 Correct 3 ms 14172 KB Output is correct
14 Correct 3 ms 13912 KB Output is correct
15 Correct 3 ms 14144 KB Output is correct
16 Correct 3 ms 14172 KB Output is correct
17 Correct 4 ms 14172 KB Output is correct
18 Correct 4 ms 14172 KB Output is correct
19 Correct 3 ms 13916 KB Output is correct
20 Correct 3 ms 14172 KB Output is correct
21 Correct 3 ms 14172 KB Output is correct
22 Correct 3 ms 14092 KB Output is correct
23 Correct 3 ms 14172 KB Output is correct
24 Correct 3 ms 14164 KB Output is correct
25 Correct 3 ms 14172 KB Output is correct
26 Correct 3 ms 14172 KB Output is correct
27 Correct 3 ms 14172 KB Output is correct
28 Correct 3 ms 14172 KB Output is correct
29 Correct 6 ms 14172 KB Output is correct
30 Correct 6 ms 14172 KB Output is correct
31 Correct 5 ms 14172 KB Output is correct
32 Correct 4 ms 14168 KB Output is correct
33 Correct 4 ms 14168 KB Output is correct
34 Correct 5 ms 14172 KB Output is correct
35 Correct 5 ms 14172 KB Output is correct
36 Correct 6 ms 14232 KB Output is correct
37 Correct 4 ms 14172 KB Output is correct
38 Correct 41 ms 15452 KB Output is correct
39 Correct 42 ms 32644 KB Output is correct
40 Correct 47 ms 15316 KB Output is correct
41 Correct 42 ms 15184 KB Output is correct
42 Correct 41 ms 15440 KB Output is correct
43 Correct 26 ms 15240 KB Output is correct
44 Correct 19 ms 14428 KB Output is correct
45 Correct 49 ms 25020 KB Output is correct
46 Correct 45 ms 25172 KB Output is correct
47 Correct 41 ms 28752 KB Output is correct
48 Correct 41 ms 28756 KB Output is correct
49 Correct 41 ms 25420 KB Output is correct
50 Correct 56 ms 25172 KB Output is correct
51 Correct 38 ms 26448 KB Output is correct
52 Correct 40 ms 26376 KB Output is correct
53 Correct 25 ms 14632 KB Output is correct
54 Correct 55 ms 25020 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 2 ms 14172 KB Output is correct
2 Correct 2 ms 14148 KB Output is correct
3 Correct 3 ms 13916 KB Output is correct
4 Correct 2 ms 14160 KB Output is correct
5 Correct 11 ms 14172 KB Output is correct
6 Correct 3 ms 14168 KB Output is correct
7 Correct 3 ms 14172 KB Output is correct
8 Correct 2 ms 14172 KB Output is correct
9 Correct 2 ms 13916 KB Output is correct
10 Correct 2 ms 14172 KB Output is correct
11 Correct 2 ms 13916 KB Output is correct
12 Correct 8 ms 14176 KB Output is correct
13 Correct 47 ms 14680 KB Output is correct
14 Correct 51 ms 14888 KB Output is correct
15 Correct 48 ms 14424 KB Output is correct
16 Correct 70 ms 25424 KB Output is correct
17 Correct 235 ms 25956 KB Output is correct
18 Correct 425 ms 27220 KB Output is correct
19 Correct 92 ms 25848 KB Output is correct
20 Correct 91 ms 25892 KB Output is correct
21 Correct 103 ms 26008 KB Output is correct
22 Correct 215 ms 26452 KB Output is correct
23 Correct 174 ms 26452 KB Output is correct
24 Correct 126 ms 26500 KB Output is correct
25 Correct 135 ms 26528 KB Output is correct
26 Correct 133 ms 26448 KB Output is correct
27 Correct 455 ms 42952 KB Output is correct
28 Correct 473 ms 48068 KB Output is correct
29 Correct 519 ms 37068 KB Output is correct
30 Correct 272 ms 35068 KB Output is correct
31 Correct 258 ms 36768 KB Output is correct
32 Correct 274 ms 34324 KB Output is correct
33 Correct 250 ms 36684 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 2 ms 13916 KB Output is correct
2 Correct 2 ms 13916 KB Output is correct
3 Correct 3 ms 13916 KB Output is correct
4 Correct 15 ms 14424 KB Output is correct
5 Correct 24 ms 14684 KB Output is correct
6 Correct 3 ms 14172 KB Output is correct
7 Correct 3 ms 14172 KB Output is correct
8 Correct 6 ms 14196 KB Output is correct
9 Correct 41 ms 15740 KB Output is correct
10 Correct 40 ms 33108 KB Output is correct
11 Correct 10 ms 14172 KB Output is correct
12 Correct 64 ms 15116 KB Output is correct
13 Correct 124 ms 33620 KB Output is correct
14 Correct 123 ms 33620 KB Output is correct
15 Correct 226 ms 33740 KB Output is correct
16 Correct 462 ms 34504 KB Output is correct
17 Correct 142 ms 33848 KB Output is correct
18 Correct 245 ms 47652 KB Output is correct
19 Correct 137 ms 33616 KB Output is correct
20 Correct 156 ms 33872 KB Output is correct
21 Correct 161 ms 33796 KB Output is correct
22 Correct 109 ms 33760 KB Output is correct
23 Correct 2 ms 13916 KB Output is correct
24 Correct 2 ms 13916 KB Output is correct
25 Correct 4 ms 14172 KB Output is correct
26 Correct 4 ms 14148 KB Output is correct
27 Correct 3 ms 14168 KB Output is correct
28 Correct 3 ms 14172 KB Output is correct
29 Correct 3 ms 14172 KB Output is correct
30 Correct 3 ms 14172 KB Output is correct
31 Correct 4 ms 14172 KB Output is correct
32 Correct 3 ms 14172 KB Output is correct
33 Correct 3 ms 14172 KB Output is correct
34 Correct 3 ms 14172 KB Output is correct
35 Correct 3 ms 14172 KB Output is correct
36 Correct 3 ms 13912 KB Output is correct
37 Correct 3 ms 14144 KB Output is correct
38 Correct 3 ms 14172 KB Output is correct
39 Correct 4 ms 14172 KB Output is correct
40 Correct 4 ms 14172 KB Output is correct
41 Correct 3 ms 13916 KB Output is correct
42 Correct 3 ms 14172 KB Output is correct
43 Correct 3 ms 14172 KB Output is correct
44 Correct 3 ms 14092 KB Output is correct
45 Correct 3 ms 14172 KB Output is correct
46 Correct 3 ms 14164 KB Output is correct
47 Correct 3 ms 14172 KB Output is correct
48 Correct 3 ms 14172 KB Output is correct
49 Correct 3 ms 14172 KB Output is correct
50 Correct 3 ms 14172 KB Output is correct
51 Correct 6 ms 14172 KB Output is correct
52 Correct 6 ms 14172 KB Output is correct
53 Correct 5 ms 14172 KB Output is correct
54 Correct 4 ms 14168 KB Output is correct
55 Correct 4 ms 14168 KB Output is correct
56 Correct 5 ms 14172 KB Output is correct
57 Correct 5 ms 14172 KB Output is correct
58 Correct 6 ms 14232 KB Output is correct
59 Correct 4 ms 14172 KB Output is correct
60 Correct 41 ms 15452 KB Output is correct
61 Correct 42 ms 32644 KB Output is correct
62 Correct 47 ms 15316 KB Output is correct
63 Correct 42 ms 15184 KB Output is correct
64 Correct 41 ms 15440 KB Output is correct
65 Correct 26 ms 15240 KB Output is correct
66 Correct 19 ms 14428 KB Output is correct
67 Correct 49 ms 25020 KB Output is correct
68 Correct 45 ms 25172 KB Output is correct
69 Correct 41 ms 28752 KB Output is correct
70 Correct 41 ms 28756 KB Output is correct
71 Correct 41 ms 25420 KB Output is correct
72 Correct 56 ms 25172 KB Output is correct
73 Correct 38 ms 26448 KB Output is correct
74 Correct 40 ms 26376 KB Output is correct
75 Correct 25 ms 14632 KB Output is correct
76 Correct 55 ms 25020 KB Output is correct
77 Correct 2 ms 14172 KB Output is correct
78 Correct 2 ms 14148 KB Output is correct
79 Correct 3 ms 13916 KB Output is correct
80 Correct 2 ms 14160 KB Output is correct
81 Correct 11 ms 14172 KB Output is correct
82 Correct 3 ms 14168 KB Output is correct
83 Correct 3 ms 14172 KB Output is correct
84 Correct 2 ms 14172 KB Output is correct
85 Correct 2 ms 13916 KB Output is correct
86 Correct 2 ms 14172 KB Output is correct
87 Correct 2 ms 13916 KB Output is correct
88 Correct 8 ms 14176 KB Output is correct
89 Correct 47 ms 14680 KB Output is correct
90 Correct 51 ms 14888 KB Output is correct
91 Correct 48 ms 14424 KB Output is correct
92 Correct 70 ms 25424 KB Output is correct
93 Correct 235 ms 25956 KB Output is correct
94 Correct 425 ms 27220 KB Output is correct
95 Correct 92 ms 25848 KB Output is correct
96 Correct 91 ms 25892 KB Output is correct
97 Correct 103 ms 26008 KB Output is correct
98 Correct 215 ms 26452 KB Output is correct
99 Correct 174 ms 26452 KB Output is correct
100 Correct 126 ms 26500 KB Output is correct
101 Correct 135 ms 26528 KB Output is correct
102 Correct 133 ms 26448 KB Output is correct
103 Correct 455 ms 42952 KB Output is correct
104 Correct 473 ms 48068 KB Output is correct
105 Correct 519 ms 37068 KB Output is correct
106 Correct 272 ms 35068 KB Output is correct
107 Correct 258 ms 36768 KB Output is correct
108 Correct 274 ms 34324 KB Output is correct
109 Correct 250 ms 36684 KB Output is correct
110 Correct 69 ms 14928 KB Output is correct
111 Correct 36 ms 14812 KB Output is correct
112 Correct 238 ms 30108 KB Output is correct
113 Correct 105 ms 29620 KB Output is correct
114 Correct 231 ms 29900 KB Output is correct
115 Correct 44 ms 25588 KB Output is correct
116 Correct 195 ms 26456 KB Output is correct
117 Correct 486 ms 27196 KB Output is correct
118 Correct 49 ms 25892 KB Output is correct
119 Correct 56 ms 25428 KB Output is correct
120 Correct 16 ms 16476 KB Output is correct
121 Correct 217 ms 26524 KB Output is correct
122 Correct 206 ms 26524 KB Output is correct
123 Correct 109 ms 30924 KB Output is correct
124 Correct 118 ms 30800 KB Output is correct
125 Correct 121 ms 30800 KB Output is correct
126 Correct 517 ms 32000 KB Output is correct
127 Correct 197 ms 28752 KB Output is correct
128 Correct 153 ms 28840 KB Output is correct
129 Correct 127 ms 28752 KB Output is correct
130 Correct 136 ms 28752 KB Output is correct