Submission #698019

# Submission time Handle Problem Language Result Execution time Memory
698019 2023-02-11T22:04:41 Z GusterGoose27 Jail (JOI22_jail) C++17
100 / 100
893 ms 101460 KB
#include <bits/stdc++.h>

using namespace std;

typedef pair<int, int> pii;

const int MAXN = 12e4;

vector<int> edges[MAXN];
vector<int> edges_expanded[5*MAXN]; // nodes for queries, tree nodes (2light), tree nodes (4heavy)
int in_deg[5*MAXN];
int N=0;
int r=0;
int n, m;
int sz[MAXN];
int depth[MAXN];
int to_hld[MAXN];
int par[MAXN];
vector<int> in_hld[MAXN]; // bottom to top
int light_ids[MAXN][2];
pii startend[MAXN];

void clear() {
	for (int i = 0; i < n; i++) {
		edges[i].clear();
		to_hld[i] = -1;
		in_hld[i].clear();
		startend[i] = pii(-1, -1);
	}
	for (int i = 0; i < N; i++) {
		edges_expanded[i].clear();
		in_deg[i] = 0;
	}
	N=0;
	r=0;
}

void make_edge(int a, int b) {
	// assert(a != 17 || b != 18);
	in_deg[b]++;
	edges_expanded[a].push_back(b);
}

class stree {
public:
	int lp, rp;
	stree *l = nullptr;
	stree *r = nullptr;
	int id;
	stree(int lv, int rv, bool ud) { // 1 => down, 0 => up
		lp = lv;
		rp = rv;
		id = N++;
		if (lp < rp) {
			int mid = (lp+rp)/2;
			l = new stree(lp, mid, ud);
			r = new stree(mid+1, rp, ud);
			if (ud) {
				make_edge(id, l->id);
				make_edge(id, r->id);
			}
			else {
				make_edge(l->id, id);
				make_edge(r->id, id);
			}
		}
	}
	void connect(int lv, int rv, int i, bool ud) {
		if (lp > rv || rp < lv) return;
		if (lp >= lv && rp <= rv) {
			if (ud) make_edge(i, id);
			else make_edge(id, i);
			return;
		}
		l->connect(lv, rv, i, ud);
		r->connect(lv, rv, i, ud);
	}
};

stree *trees[MAXN][2];

void dfs(int cur = 0, int p = -1) {
	sz[cur] = 1;
	for (int nxt: edges[cur]) {
		if (nxt == p) continue;
		depth[nxt] = depth[cur]+1;
		par[nxt] = cur;
		dfs(nxt, cur);
		sz[cur] += sz[nxt];
	}
}

void make_hld(int cur = 0, int p = -1) {
	for (int nxt: edges[cur]) {
		if (nxt == p) continue;
		if (sz[nxt] > sz[cur]/2) {
			if (to_hld[cur] == -1) {
				to_hld[cur] = cur;
			}
			to_hld[nxt] = to_hld[cur];
			make_hld(nxt, cur);
		}
		else make_hld(nxt, cur);
	}
	if (to_hld[cur] != -1) in_hld[to_hld[cur]].push_back(cur);
	else {
		light_ids[cur][0] = N++;
		light_ids[cur][1] = N++;
	}
}

bool comp(int x, int y) {
	if (to_hld[x] == -1 && to_hld[y] == -1) return depth[x] < depth[y];
	if (to_hld[x] == -1) {
		return pii(depth[x], depth[x]) < pii(depth[to_hld[y]], depth[y]);
	}
	if (to_hld[y] == -1) {
		return pii(depth[y], depth[y]) > pii(depth[to_hld[x]], depth[x]);
	}
	return pii(depth[to_hld[x]], depth[x]) < pii(depth[to_hld[y]], depth[y]);
}

void make_path(int x, int y, bool excx, bool excy) {
	if (x == y) {
		if (excx || excy) return;
		if (to_hld[x] == -1) {
			make_edge(light_ids[x][0], N-1);
			make_edge(N-1, light_ids[x][1]);
			return;
		}
	}
	if (comp(x, y)) {
		swap(x, y);
		swap(excx, excy);
	}
	if (to_hld[x] != -1 && to_hld[x] == to_hld[y]) {
		int curhld = to_hld[x];
		int d1 = depth[in_hld[curhld][0]]-depth[x];
		int d2 = depth[in_hld[curhld][0]]-depth[y];
		if (excx) d1++;
		if (excy) d2--;
		trees[curhld][0]->connect(d1, d2, N-1, 0);
		trees[curhld][1]->connect(d1, d2, N-1, 1);
		return;
	}
	if (to_hld[x] == -1) {
		if (excx) {
			return make_path(par[x], y, 0, excy);
		}
		make_edge(light_ids[x][0], N-1);
		make_edge(N-1, light_ids[x][1]);
		return make_path(par[x], y, excx, excy);
	}
	int curhld = to_hld[x];
	int d1 = depth[in_hld[curhld][0]]-depth[x];
	int d2 = in_hld[curhld].size()-1;
	if (excx) {
		d1++;
		excx = 0;
	}
	trees[curhld][0]->connect(d1, d2, N-1, 0);
	trees[curhld][1]->connect(d1, d2, N-1, 1);
	return make_path(par[to_hld[x]], y, excx, excy);
}

void prune(int cur) {
	in_deg[cur] = -1;
	r++;
	// cout << cur << "\n";
	for (int nxt: edges_expanded[cur]) {
		in_deg[nxt]--;
		if (in_deg[nxt] == 0) prune(nxt);
	}
}

int main() {
	int t; cin >> t;
	fill(to_hld, to_hld+MAXN, -1);
	fill(startend, startend+MAXN, pii(-1, -1));
	while (t--) {
		cin >> n;
		for (int i = 0; i < n-1; i++) {
			int x, y; cin >> x >> y;
			x--; y--;
			edges[x].push_back(y);
			edges[y].push_back(x);
		}
		dfs();
		make_hld();
		for (int i = 0; i < n; i++) {
			if (to_hld[i] == i) {
				for (int j = 0; j < 2; j++) {
					trees[i][j] = new stree(0, in_hld[i].size()-1, j);
				}
			}
		}
		cin >> m;
		for (int i = 0; i < m; i++) {
			int x, y; cin >> x >> y;
			x--; y--;
			N++;
			startend[x].first = N-1;
			startend[y].second = N-1;
			if (to_hld[y] == -1) {
				make_edge(light_ids[y][1], N-1);
			}
			else {
				int curhld = to_hld[y];
				int d = -depth[y]+depth[in_hld[curhld][0]];
				trees[curhld][1]->connect(d, d, N-1, 0);
			}
			if (to_hld[x] == -1) {
				make_edge(N-1, light_ids[x][0]);
			}
			else {
				int curhld = to_hld[x];
				int d = -depth[x]+depth[in_hld[curhld][0]];
				trees[curhld][0]->connect(d, d, N-1, 1);
			}
			make_path(x, y, 1, 1);
		}
		for (int i = 0; i < n; i++) {
			if (startend[i].first != -1 && startend[i].second != -1) {
				make_edge(startend[i].first, startend[i].second);
			}
		}
		for (int i = 0; i < N; i++) {
			if (in_deg[i] == 0) prune(i);
		}
		if (r == N) {
			cout << "Yes\n";
		}
		else cout << "No\n";
		clear();
	}
}
# Verdict Execution time Memory Grader output
1 Correct 12 ms 21448 KB Output is correct
2 Correct 11 ms 21460 KB Output is correct
3 Correct 10 ms 21460 KB Output is correct
4 Correct 44 ms 32080 KB Output is correct
5 Correct 81 ms 44456 KB Output is correct
6 Correct 14 ms 22484 KB Output is correct
7 Correct 14 ms 22484 KB Output is correct
8 Correct 18 ms 22440 KB Output is correct
9 Correct 120 ms 46772 KB Output is correct
10 Correct 145 ms 73696 KB Output is correct
11 Correct 28 ms 25180 KB Output is correct
12 Correct 117 ms 44668 KB Output is correct
13 Correct 257 ms 79308 KB Output is correct
14 Correct 246 ms 79564 KB Output is correct
15 Correct 449 ms 84536 KB Output is correct
16 Correct 893 ms 101460 KB Output is correct
17 Correct 313 ms 87744 KB Output is correct
18 Correct 269 ms 82924 KB Output is correct
19 Correct 302 ms 85352 KB Output is correct
20 Correct 283 ms 85220 KB Output is correct
21 Correct 299 ms 86500 KB Output is correct
22 Correct 212 ms 78332 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 11 ms 21460 KB Output is correct
2 Correct 11 ms 21448 KB Output is correct
3 Correct 14 ms 22484 KB Output is correct
4 Correct 15 ms 21972 KB Output is correct
5 Correct 13 ms 21976 KB Output is correct
6 Correct 14 ms 21940 KB Output is correct
7 Correct 16 ms 21976 KB Output is correct
8 Correct 13 ms 21972 KB Output is correct
9 Correct 14 ms 21972 KB Output is correct
10 Correct 13 ms 21972 KB Output is correct
11 Correct 13 ms 21844 KB Output is correct
12 Correct 12 ms 21504 KB Output is correct
13 Correct 11 ms 21464 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 11 ms 21460 KB Output is correct
2 Correct 11 ms 21448 KB Output is correct
3 Correct 14 ms 22484 KB Output is correct
4 Correct 15 ms 21972 KB Output is correct
5 Correct 13 ms 21976 KB Output is correct
6 Correct 14 ms 21940 KB Output is correct
7 Correct 16 ms 21976 KB Output is correct
8 Correct 13 ms 21972 KB Output is correct
9 Correct 14 ms 21972 KB Output is correct
10 Correct 13 ms 21972 KB Output is correct
11 Correct 13 ms 21844 KB Output is correct
12 Correct 12 ms 21504 KB Output is correct
13 Correct 11 ms 21464 KB Output is correct
14 Correct 11 ms 21448 KB Output is correct
15 Correct 10 ms 21456 KB Output is correct
16 Correct 13 ms 22484 KB Output is correct
17 Correct 12 ms 21716 KB Output is correct
18 Correct 15 ms 21992 KB Output is correct
19 Correct 11 ms 21420 KB Output is correct
20 Correct 15 ms 22044 KB Output is correct
21 Correct 14 ms 22028 KB Output is correct
22 Correct 13 ms 21720 KB Output is correct
23 Correct 11 ms 21412 KB Output is correct
24 Correct 12 ms 21460 KB Output is correct
25 Correct 15 ms 22356 KB Output is correct
26 Correct 12 ms 21584 KB Output is correct
27 Correct 13 ms 21716 KB Output is correct
28 Correct 11 ms 21460 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 11 ms 21460 KB Output is correct
2 Correct 11 ms 21448 KB Output is correct
3 Correct 14 ms 22484 KB Output is correct
4 Correct 15 ms 21972 KB Output is correct
5 Correct 13 ms 21976 KB Output is correct
6 Correct 14 ms 21940 KB Output is correct
7 Correct 16 ms 21976 KB Output is correct
8 Correct 13 ms 21972 KB Output is correct
9 Correct 14 ms 21972 KB Output is correct
10 Correct 13 ms 21972 KB Output is correct
11 Correct 13 ms 21844 KB Output is correct
12 Correct 12 ms 21504 KB Output is correct
13 Correct 11 ms 21464 KB Output is correct
14 Correct 11 ms 21448 KB Output is correct
15 Correct 10 ms 21456 KB Output is correct
16 Correct 13 ms 22484 KB Output is correct
17 Correct 12 ms 21716 KB Output is correct
18 Correct 15 ms 21992 KB Output is correct
19 Correct 11 ms 21420 KB Output is correct
20 Correct 15 ms 22044 KB Output is correct
21 Correct 14 ms 22028 KB Output is correct
22 Correct 13 ms 21720 KB Output is correct
23 Correct 11 ms 21412 KB Output is correct
24 Correct 12 ms 21460 KB Output is correct
25 Correct 15 ms 22356 KB Output is correct
26 Correct 12 ms 21584 KB Output is correct
27 Correct 13 ms 21716 KB Output is correct
28 Correct 11 ms 21460 KB Output is correct
29 Correct 15 ms 22492 KB Output is correct
30 Correct 13 ms 21724 KB Output is correct
31 Correct 14 ms 22104 KB Output is correct
32 Correct 15 ms 22100 KB Output is correct
33 Correct 14 ms 21972 KB Output is correct
34 Correct 14 ms 21716 KB Output is correct
35 Correct 15 ms 21976 KB Output is correct
36 Correct 14 ms 21576 KB Output is correct
37 Correct 13 ms 21620 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 11 ms 21460 KB Output is correct
2 Correct 11 ms 21448 KB Output is correct
3 Correct 14 ms 22484 KB Output is correct
4 Correct 15 ms 21972 KB Output is correct
5 Correct 13 ms 21976 KB Output is correct
6 Correct 14 ms 21940 KB Output is correct
7 Correct 16 ms 21976 KB Output is correct
8 Correct 13 ms 21972 KB Output is correct
9 Correct 14 ms 21972 KB Output is correct
10 Correct 13 ms 21972 KB Output is correct
11 Correct 13 ms 21844 KB Output is correct
12 Correct 12 ms 21504 KB Output is correct
13 Correct 11 ms 21464 KB Output is correct
14 Correct 11 ms 21448 KB Output is correct
15 Correct 10 ms 21456 KB Output is correct
16 Correct 13 ms 22484 KB Output is correct
17 Correct 12 ms 21716 KB Output is correct
18 Correct 15 ms 21992 KB Output is correct
19 Correct 11 ms 21420 KB Output is correct
20 Correct 15 ms 22044 KB Output is correct
21 Correct 14 ms 22028 KB Output is correct
22 Correct 13 ms 21720 KB Output is correct
23 Correct 11 ms 21412 KB Output is correct
24 Correct 12 ms 21460 KB Output is correct
25 Correct 15 ms 22356 KB Output is correct
26 Correct 12 ms 21584 KB Output is correct
27 Correct 13 ms 21716 KB Output is correct
28 Correct 11 ms 21460 KB Output is correct
29 Correct 15 ms 22492 KB Output is correct
30 Correct 13 ms 21724 KB Output is correct
31 Correct 14 ms 22104 KB Output is correct
32 Correct 15 ms 22100 KB Output is correct
33 Correct 14 ms 21972 KB Output is correct
34 Correct 14 ms 21716 KB Output is correct
35 Correct 15 ms 21976 KB Output is correct
36 Correct 14 ms 21576 KB Output is correct
37 Correct 13 ms 21620 KB Output is correct
38 Correct 109 ms 46864 KB Output is correct
39 Correct 145 ms 73680 KB Output is correct
40 Correct 100 ms 33356 KB Output is correct
41 Correct 94 ms 29084 KB Output is correct
42 Correct 80 ms 28012 KB Output is correct
43 Correct 89 ms 36500 KB Output is correct
44 Correct 29 ms 22868 KB Output is correct
45 Correct 136 ms 47868 KB Output is correct
46 Correct 140 ms 47820 KB Output is correct
47 Correct 129 ms 57764 KB Output is correct
48 Correct 130 ms 57860 KB Output is correct
49 Correct 125 ms 48228 KB Output is correct
50 Correct 127 ms 48416 KB Output is correct
51 Correct 116 ms 50772 KB Output is correct
52 Correct 118 ms 50748 KB Output is correct
53 Correct 30 ms 22600 KB Output is correct
54 Correct 115 ms 30076 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 11 ms 21460 KB Output is correct
2 Correct 13 ms 21460 KB Output is correct
3 Correct 11 ms 21452 KB Output is correct
4 Correct 11 ms 21412 KB Output is correct
5 Correct 27 ms 25216 KB Output is correct
6 Correct 12 ms 21464 KB Output is correct
7 Correct 12 ms 21484 KB Output is correct
8 Correct 11 ms 21460 KB Output is correct
9 Correct 13 ms 21460 KB Output is correct
10 Correct 11 ms 21456 KB Output is correct
11 Correct 12 ms 21448 KB Output is correct
12 Correct 13 ms 21588 KB Output is correct
13 Correct 51 ms 24844 KB Output is correct
14 Correct 78 ms 27460 KB Output is correct
15 Correct 68 ms 26888 KB Output is correct
16 Correct 143 ms 37688 KB Output is correct
17 Correct 277 ms 46380 KB Output is correct
18 Correct 451 ms 59600 KB Output is correct
19 Correct 174 ms 39664 KB Output is correct
20 Correct 174 ms 40628 KB Output is correct
21 Correct 179 ms 40356 KB Output is correct
22 Correct 267 ms 47472 KB Output is correct
23 Correct 231 ms 47432 KB Output is correct
24 Correct 254 ms 47396 KB Output is correct
25 Correct 236 ms 46280 KB Output is correct
26 Correct 225 ms 46940 KB Output is correct
27 Correct 263 ms 43204 KB Output is correct
28 Correct 260 ms 40248 KB Output is correct
29 Correct 237 ms 40264 KB Output is correct
30 Correct 205 ms 38600 KB Output is correct
31 Correct 193 ms 36960 KB Output is correct
32 Correct 223 ms 39180 KB Output is correct
33 Correct 183 ms 36964 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 12 ms 21448 KB Output is correct
2 Correct 11 ms 21460 KB Output is correct
3 Correct 10 ms 21460 KB Output is correct
4 Correct 44 ms 32080 KB Output is correct
5 Correct 81 ms 44456 KB Output is correct
6 Correct 14 ms 22484 KB Output is correct
7 Correct 14 ms 22484 KB Output is correct
8 Correct 18 ms 22440 KB Output is correct
9 Correct 120 ms 46772 KB Output is correct
10 Correct 145 ms 73696 KB Output is correct
11 Correct 28 ms 25180 KB Output is correct
12 Correct 117 ms 44668 KB Output is correct
13 Correct 257 ms 79308 KB Output is correct
14 Correct 246 ms 79564 KB Output is correct
15 Correct 449 ms 84536 KB Output is correct
16 Correct 893 ms 101460 KB Output is correct
17 Correct 313 ms 87744 KB Output is correct
18 Correct 269 ms 82924 KB Output is correct
19 Correct 302 ms 85352 KB Output is correct
20 Correct 283 ms 85220 KB Output is correct
21 Correct 299 ms 86500 KB Output is correct
22 Correct 212 ms 78332 KB Output is correct
23 Correct 11 ms 21460 KB Output is correct
24 Correct 11 ms 21448 KB Output is correct
25 Correct 14 ms 22484 KB Output is correct
26 Correct 15 ms 21972 KB Output is correct
27 Correct 13 ms 21976 KB Output is correct
28 Correct 14 ms 21940 KB Output is correct
29 Correct 16 ms 21976 KB Output is correct
30 Correct 13 ms 21972 KB Output is correct
31 Correct 14 ms 21972 KB Output is correct
32 Correct 13 ms 21972 KB Output is correct
33 Correct 13 ms 21844 KB Output is correct
34 Correct 12 ms 21504 KB Output is correct
35 Correct 11 ms 21464 KB Output is correct
36 Correct 11 ms 21448 KB Output is correct
37 Correct 10 ms 21456 KB Output is correct
38 Correct 13 ms 22484 KB Output is correct
39 Correct 12 ms 21716 KB Output is correct
40 Correct 15 ms 21992 KB Output is correct
41 Correct 11 ms 21420 KB Output is correct
42 Correct 15 ms 22044 KB Output is correct
43 Correct 14 ms 22028 KB Output is correct
44 Correct 13 ms 21720 KB Output is correct
45 Correct 11 ms 21412 KB Output is correct
46 Correct 12 ms 21460 KB Output is correct
47 Correct 15 ms 22356 KB Output is correct
48 Correct 12 ms 21584 KB Output is correct
49 Correct 13 ms 21716 KB Output is correct
50 Correct 11 ms 21460 KB Output is correct
51 Correct 15 ms 22492 KB Output is correct
52 Correct 13 ms 21724 KB Output is correct
53 Correct 14 ms 22104 KB Output is correct
54 Correct 15 ms 22100 KB Output is correct
55 Correct 14 ms 21972 KB Output is correct
56 Correct 14 ms 21716 KB Output is correct
57 Correct 15 ms 21976 KB Output is correct
58 Correct 14 ms 21576 KB Output is correct
59 Correct 13 ms 21620 KB Output is correct
60 Correct 109 ms 46864 KB Output is correct
61 Correct 145 ms 73680 KB Output is correct
62 Correct 100 ms 33356 KB Output is correct
63 Correct 94 ms 29084 KB Output is correct
64 Correct 80 ms 28012 KB Output is correct
65 Correct 89 ms 36500 KB Output is correct
66 Correct 29 ms 22868 KB Output is correct
67 Correct 136 ms 47868 KB Output is correct
68 Correct 140 ms 47820 KB Output is correct
69 Correct 129 ms 57764 KB Output is correct
70 Correct 130 ms 57860 KB Output is correct
71 Correct 125 ms 48228 KB Output is correct
72 Correct 127 ms 48416 KB Output is correct
73 Correct 116 ms 50772 KB Output is correct
74 Correct 118 ms 50748 KB Output is correct
75 Correct 30 ms 22600 KB Output is correct
76 Correct 115 ms 30076 KB Output is correct
77 Correct 11 ms 21460 KB Output is correct
78 Correct 13 ms 21460 KB Output is correct
79 Correct 11 ms 21452 KB Output is correct
80 Correct 11 ms 21412 KB Output is correct
81 Correct 27 ms 25216 KB Output is correct
82 Correct 12 ms 21464 KB Output is correct
83 Correct 12 ms 21484 KB Output is correct
84 Correct 11 ms 21460 KB Output is correct
85 Correct 13 ms 21460 KB Output is correct
86 Correct 11 ms 21456 KB Output is correct
87 Correct 12 ms 21448 KB Output is correct
88 Correct 13 ms 21588 KB Output is correct
89 Correct 51 ms 24844 KB Output is correct
90 Correct 78 ms 27460 KB Output is correct
91 Correct 68 ms 26888 KB Output is correct
92 Correct 143 ms 37688 KB Output is correct
93 Correct 277 ms 46380 KB Output is correct
94 Correct 451 ms 59600 KB Output is correct
95 Correct 174 ms 39664 KB Output is correct
96 Correct 174 ms 40628 KB Output is correct
97 Correct 179 ms 40356 KB Output is correct
98 Correct 267 ms 47472 KB Output is correct
99 Correct 231 ms 47432 KB Output is correct
100 Correct 254 ms 47396 KB Output is correct
101 Correct 236 ms 46280 KB Output is correct
102 Correct 225 ms 46940 KB Output is correct
103 Correct 263 ms 43204 KB Output is correct
104 Correct 260 ms 40248 KB Output is correct
105 Correct 237 ms 40264 KB Output is correct
106 Correct 205 ms 38600 KB Output is correct
107 Correct 193 ms 36960 KB Output is correct
108 Correct 223 ms 39180 KB Output is correct
109 Correct 183 ms 36964 KB Output is correct
110 Correct 87 ms 29532 KB Output is correct
111 Correct 66 ms 29796 KB Output is correct
112 Correct 459 ms 71112 KB Output is correct
113 Correct 250 ms 62564 KB Output is correct
114 Correct 321 ms 60784 KB Output is correct
115 Correct 74 ms 30332 KB Output is correct
116 Correct 233 ms 41372 KB Output is correct
117 Correct 531 ms 67264 KB Output is correct
118 Correct 117 ms 30392 KB Output is correct
119 Correct 118 ms 30396 KB Output is correct
120 Correct 27 ms 23796 KB Output is correct
121 Correct 328 ms 50248 KB Output is correct
122 Correct 338 ms 50260 KB Output is correct
123 Correct 276 ms 68048 KB Output is correct
124 Correct 260 ms 68052 KB Output is correct
125 Correct 286 ms 68788 KB Output is correct
126 Correct 797 ms 90324 KB Output is correct
127 Correct 356 ms 73508 KB Output is correct
128 Correct 278 ms 73596 KB Output is correct
129 Correct 298 ms 73860 KB Output is correct
130 Correct 279 ms 73692 KB Output is correct