답안 #788252

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
788252 2023-07-20T03:44:54 Z vjudge1 Jail (JOI22_jail) C++17
100 / 100
826 ms 148160 KB
#include <bits/stdc++.h>
using namespace std;
constexpr int N = 1200005;
int n, m;
int st[N], to[N];
vector<int> tr[N], e[N];
int bh[N];
int ma;
int dfn[N], faz[N], dep[N], siz[N], son[N], top[N];
int dfn_now;
void dfs1(int u, int fa) {
	faz[u] = fa;
	dep[u] = dep[fa] + 1;
	siz[u] = 1;
	for (int v : tr[u]) {
		if (v == fa) {
			continue;
		}
		dfs1(v, u);
		siz[u] += siz[v];
		if (siz[v] > siz[son[u]]) {
			son[u] = v;
		}
	}
}
void dfs2(int u, int tp) {
	top[u] = tp;
	dfn[u] = ++dfn_now;
	if (son[u]) {
		dfs2(son[u], tp);
	}
	for (int v : tr[u]) {
		if (v == faz[u] || v == son[u]) {
			continue;
		}
		dfs2(v, v);
	}
}
vector<int> bb;
void cqj(int u, int l, int r, int ql, int qr) {
	if (l >= ql && r <= qr) {
		bb.push_back(u);
		return;
	}
	const int mid = (l + r) >> 1;
	if (ql <= mid) cqj(u << 1, l, mid, ql, qr);
	if (qr > mid) cqj(u << 1 | 1, mid + 1, r, ql, qr);
}
void jqj(int di, int L, int R) {
	cqj(1, 1, n, L, R);
	e[di].insert(e[di].end(), bb.begin(), bb.end());
	bb.clear();
}
void jia(int di, int L, int R, int zho) {
	if (R < zho || L > zho) {
		jqj(di, L, R);
	} else {
		if (L < zho) jqj(di, L, zho - 1);
		if (zho < R) jqj(di, zho + 1, R);
	}
}
void jb(int di, int u, int v) {
	int zho = dfn[v];
	while (top[u] != top[v]) {
		if (dep[top[u]] < dep[top[v]]) {
			swap(u, v);
		}
		jia(di, dfn[top[u]], dfn[u], zho);
		u = faz[top[u]];
	}
	if (dep[u] < dep[v]) {
		swap(u, v);
	}
	jia(di, dfn[v], dfn[u], zho);
}
void jqj2(int l, int r, int di) {
	cqj(1, 1, n, l, r);
	for (auto i : bb) {
		e[i + ma].push_back(di);
	}
	bb.clear();
}
void jia2(int L, int R, int di, int zho) {
	if (R < zho || L > zho) {
		jqj2(L, R, di);
	} else {
		if (L < zho) jqj2(L, zho - 1, di);
		if (zho < R) jqj2(zho + 1, R, di);
	}
}
void jb2(int u, int v, int di) {
	int zho = dfn[u];
	while (top[u] != top[v]) {
		if (dep[top[u]] < dep[top[v]]) {
			swap(u, v);
		}
		jia2(dfn[top[u]], dfn[u], di, zho);
		u = faz[top[u]];
	}
	if (dep[u] < dep[v]) {
		swap(u, v);
	}
	jia2(dfn[v], dfn[u], di, zho);
}
void xj(int u, int l, int r) {
	if (u != 1) {
		e[u / 2].push_back(u);
	}
	if (l == r) {
		bh[l] = u;
		return;
	}
	const int mid = (l + r) >> 1;
	xj(u << 1, l, mid);
	xj(u << 1 | 1, mid + 1, r);
}
void xj2(int u, int l, int r) {
	if (u != 1) {
		e[u + ma].push_back(u / 2 + ma);
	}
	if (l == r) {
		bh[l] = u;
		return;
	}
	const int mid = (l + r) >> 1;
	xj2(u << 1, l, mid);
	xj2(u << 1 | 1, mid + 1, r);
}
bool vis[N], ins[N];
bool dag() {
	bool flag = 1;
	function<void(int)> dfs = [&](int u) {
		if (ins[u]) {
			flag = 0;
			return;
		}
		if (vis[u]) {
			return;
		}
		ins[u] = 1;
		vis[u] = 1;
		for (int v : e[u]) {
			dfs(v);
			if (!flag) {
				break;
			}
		}
		ins[u] = 0;
	};
	for (int i = 1; i <= ma * 2 + m; i++) {
		dfs(i);
	}
	return flag;
}
void solve() {
	cin >> n;
	for (int i = 1, u, v; i < n; i++) {
		cin >> u >> v;
		tr[u].push_back(v);
		tr[v].push_back(u);
	}
	cin >> m;
	for (int i = 1; i <= m; i++) {
		cin >> st[i] >> to[i];
	}
	dfs1(1, 0), dfs2(1, 1);
	ma = n * 4 + 10;
	xj(1, 1, n);
	for (int u = 1; u <= m; u++) {
		jb(u + ma * 2, st[u], to[u]);
	}
	for (int v = 1; v <= m; v++) {
		e[bh[dfn[to[v]]]].push_back(v + ma * 2);
	}
	for (int u = 1; u <= m; u++) {
		e[u + ma * 2].push_back(bh[dfn[st[u]]] + ma);
	}
	for (int v = 1; v <= m; v++) {
		jb2(st[v], to[v], v + ma * 2);
	}
	xj2(1, 1, n);
	if (dag()) cout << "Yes" << "\n";
	else cout << "No" << "\n";
}
void clear() {
	for (int i = 1; i <= ma * 2 + m; i++) {
		st[i] = to[i] = bh[i] = dfn[i] = faz[i] = dep[i] = siz[i] = son[i] = top[i] = vis[i] = ins[i] = 0;
		tr[i].clear(), e[i].clear();
	}
	n = m = ma = dfn_now = 0;
}
int main() {
	int T;
	cin >> T;
	while (T--) {
		solve();
		clear();
	}
	return 0;
}
# 결과 실행 시간 메모리 Grader output
1 Correct 29 ms 56660 KB Output is correct
2 Correct 30 ms 56680 KB Output is correct
3 Correct 28 ms 56664 KB Output is correct
4 Correct 50 ms 56816 KB Output is correct
5 Correct 75 ms 56964 KB Output is correct
6 Correct 29 ms 56908 KB Output is correct
7 Correct 29 ms 56792 KB Output is correct
8 Correct 30 ms 56944 KB Output is correct
9 Correct 94 ms 60188 KB Output is correct
10 Correct 135 ms 117240 KB Output is correct
11 Correct 39 ms 56860 KB Output is correct
12 Correct 94 ms 57056 KB Output is correct
13 Correct 202 ms 124192 KB Output is correct
14 Correct 205 ms 124372 KB Output is correct
15 Correct 261 ms 129872 KB Output is correct
16 Correct 398 ms 148160 KB Output is correct
17 Correct 220 ms 132560 KB Output is correct
18 Correct 242 ms 128972 KB Output is correct
19 Correct 224 ms 131548 KB Output is correct
20 Correct 217 ms 131440 KB Output is correct
21 Correct 222 ms 132708 KB Output is correct
22 Correct 191 ms 123100 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 27 ms 56700 KB Output is correct
2 Correct 27 ms 56636 KB Output is correct
3 Correct 30 ms 56820 KB Output is correct
4 Correct 30 ms 56884 KB Output is correct
5 Correct 31 ms 56824 KB Output is correct
6 Correct 34 ms 56788 KB Output is correct
7 Correct 31 ms 56788 KB Output is correct
8 Correct 29 ms 56788 KB Output is correct
9 Correct 29 ms 56792 KB Output is correct
10 Correct 29 ms 56856 KB Output is correct
11 Correct 30 ms 56860 KB Output is correct
12 Correct 29 ms 56836 KB Output is correct
13 Correct 32 ms 56788 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 27 ms 56700 KB Output is correct
2 Correct 27 ms 56636 KB Output is correct
3 Correct 30 ms 56820 KB Output is correct
4 Correct 30 ms 56884 KB Output is correct
5 Correct 31 ms 56824 KB Output is correct
6 Correct 34 ms 56788 KB Output is correct
7 Correct 31 ms 56788 KB Output is correct
8 Correct 29 ms 56788 KB Output is correct
9 Correct 29 ms 56792 KB Output is correct
10 Correct 29 ms 56856 KB Output is correct
11 Correct 30 ms 56860 KB Output is correct
12 Correct 29 ms 56836 KB Output is correct
13 Correct 32 ms 56788 KB Output is correct
14 Correct 27 ms 56748 KB Output is correct
15 Correct 27 ms 56764 KB Output is correct
16 Correct 29 ms 56908 KB Output is correct
17 Correct 31 ms 56832 KB Output is correct
18 Correct 31 ms 56824 KB Output is correct
19 Correct 28 ms 56720 KB Output is correct
20 Correct 29 ms 56872 KB Output is correct
21 Correct 30 ms 56856 KB Output is correct
22 Correct 29 ms 56800 KB Output is correct
23 Correct 28 ms 56684 KB Output is correct
24 Correct 27 ms 56772 KB Output is correct
25 Correct 30 ms 56868 KB Output is correct
26 Correct 27 ms 56816 KB Output is correct
27 Correct 30 ms 56788 KB Output is correct
28 Correct 27 ms 56712 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 27 ms 56700 KB Output is correct
2 Correct 27 ms 56636 KB Output is correct
3 Correct 30 ms 56820 KB Output is correct
4 Correct 30 ms 56884 KB Output is correct
5 Correct 31 ms 56824 KB Output is correct
6 Correct 34 ms 56788 KB Output is correct
7 Correct 31 ms 56788 KB Output is correct
8 Correct 29 ms 56788 KB Output is correct
9 Correct 29 ms 56792 KB Output is correct
10 Correct 29 ms 56856 KB Output is correct
11 Correct 30 ms 56860 KB Output is correct
12 Correct 29 ms 56836 KB Output is correct
13 Correct 32 ms 56788 KB Output is correct
14 Correct 27 ms 56748 KB Output is correct
15 Correct 27 ms 56764 KB Output is correct
16 Correct 29 ms 56908 KB Output is correct
17 Correct 31 ms 56832 KB Output is correct
18 Correct 31 ms 56824 KB Output is correct
19 Correct 28 ms 56720 KB Output is correct
20 Correct 29 ms 56872 KB Output is correct
21 Correct 30 ms 56856 KB Output is correct
22 Correct 29 ms 56800 KB Output is correct
23 Correct 28 ms 56684 KB Output is correct
24 Correct 27 ms 56772 KB Output is correct
25 Correct 30 ms 56868 KB Output is correct
26 Correct 27 ms 56816 KB Output is correct
27 Correct 30 ms 56788 KB Output is correct
28 Correct 27 ms 56712 KB Output is correct
29 Correct 29 ms 56856 KB Output is correct
30 Correct 29 ms 56924 KB Output is correct
31 Correct 29 ms 56884 KB Output is correct
32 Correct 29 ms 56800 KB Output is correct
33 Correct 29 ms 56848 KB Output is correct
34 Correct 30 ms 56776 KB Output is correct
35 Correct 30 ms 56828 KB Output is correct
36 Correct 29 ms 56808 KB Output is correct
37 Correct 29 ms 56872 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 27 ms 56700 KB Output is correct
2 Correct 27 ms 56636 KB Output is correct
3 Correct 30 ms 56820 KB Output is correct
4 Correct 30 ms 56884 KB Output is correct
5 Correct 31 ms 56824 KB Output is correct
6 Correct 34 ms 56788 KB Output is correct
7 Correct 31 ms 56788 KB Output is correct
8 Correct 29 ms 56788 KB Output is correct
9 Correct 29 ms 56792 KB Output is correct
10 Correct 29 ms 56856 KB Output is correct
11 Correct 30 ms 56860 KB Output is correct
12 Correct 29 ms 56836 KB Output is correct
13 Correct 32 ms 56788 KB Output is correct
14 Correct 27 ms 56748 KB Output is correct
15 Correct 27 ms 56764 KB Output is correct
16 Correct 29 ms 56908 KB Output is correct
17 Correct 31 ms 56832 KB Output is correct
18 Correct 31 ms 56824 KB Output is correct
19 Correct 28 ms 56720 KB Output is correct
20 Correct 29 ms 56872 KB Output is correct
21 Correct 30 ms 56856 KB Output is correct
22 Correct 29 ms 56800 KB Output is correct
23 Correct 28 ms 56684 KB Output is correct
24 Correct 27 ms 56772 KB Output is correct
25 Correct 30 ms 56868 KB Output is correct
26 Correct 27 ms 56816 KB Output is correct
27 Correct 30 ms 56788 KB Output is correct
28 Correct 27 ms 56712 KB Output is correct
29 Correct 29 ms 56856 KB Output is correct
30 Correct 29 ms 56924 KB Output is correct
31 Correct 29 ms 56884 KB Output is correct
32 Correct 29 ms 56800 KB Output is correct
33 Correct 29 ms 56848 KB Output is correct
34 Correct 30 ms 56776 KB Output is correct
35 Correct 30 ms 56828 KB Output is correct
36 Correct 29 ms 56808 KB Output is correct
37 Correct 29 ms 56872 KB Output is correct
38 Correct 96 ms 60256 KB Output is correct
39 Correct 131 ms 117192 KB Output is correct
40 Correct 125 ms 60120 KB Output is correct
41 Correct 110 ms 59992 KB Output is correct
42 Correct 95 ms 60236 KB Output is correct
43 Correct 88 ms 59696 KB Output is correct
44 Correct 47 ms 57644 KB Output is correct
45 Correct 141 ms 107820 KB Output is correct
46 Correct 145 ms 107852 KB Output is correct
47 Correct 133 ms 112448 KB Output is correct
48 Correct 134 ms 112460 KB Output is correct
49 Correct 141 ms 107960 KB Output is correct
50 Correct 147 ms 107936 KB Output is correct
51 Correct 133 ms 109228 KB Output is correct
52 Correct 136 ms 109376 KB Output is correct
53 Correct 51 ms 60880 KB Output is correct
54 Correct 147 ms 107740 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 29 ms 56624 KB Output is correct
2 Correct 29 ms 56652 KB Output is correct
3 Correct 28 ms 56708 KB Output is correct
4 Correct 27 ms 56660 KB Output is correct
5 Correct 40 ms 56804 KB Output is correct
6 Correct 29 ms 56788 KB Output is correct
7 Correct 30 ms 56816 KB Output is correct
8 Correct 30 ms 56700 KB Output is correct
9 Correct 28 ms 56728 KB Output is correct
10 Correct 29 ms 56792 KB Output is correct
11 Correct 28 ms 56652 KB Output is correct
12 Correct 28 ms 56812 KB Output is correct
13 Correct 71 ms 57084 KB Output is correct
14 Correct 102 ms 56964 KB Output is correct
15 Correct 89 ms 57036 KB Output is correct
16 Correct 175 ms 109796 KB Output is correct
17 Correct 364 ms 119940 KB Output is correct
18 Correct 619 ms 132628 KB Output is correct
19 Correct 237 ms 111888 KB Output is correct
20 Correct 226 ms 111856 KB Output is correct
21 Correct 269 ms 111876 KB Output is correct
22 Correct 356 ms 119736 KB Output is correct
23 Correct 296 ms 119944 KB Output is correct
24 Correct 317 ms 120116 KB Output is correct
25 Correct 307 ms 120048 KB Output is correct
26 Correct 331 ms 120160 KB Output is correct
27 Correct 379 ms 128740 KB Output is correct
28 Correct 336 ms 139428 KB Output is correct
29 Correct 332 ms 128024 KB Output is correct
30 Correct 297 ms 119104 KB Output is correct
31 Correct 255 ms 125600 KB Output is correct
32 Correct 259 ms 120672 KB Output is correct
33 Correct 320 ms 125628 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 29 ms 56660 KB Output is correct
2 Correct 30 ms 56680 KB Output is correct
3 Correct 28 ms 56664 KB Output is correct
4 Correct 50 ms 56816 KB Output is correct
5 Correct 75 ms 56964 KB Output is correct
6 Correct 29 ms 56908 KB Output is correct
7 Correct 29 ms 56792 KB Output is correct
8 Correct 30 ms 56944 KB Output is correct
9 Correct 94 ms 60188 KB Output is correct
10 Correct 135 ms 117240 KB Output is correct
11 Correct 39 ms 56860 KB Output is correct
12 Correct 94 ms 57056 KB Output is correct
13 Correct 202 ms 124192 KB Output is correct
14 Correct 205 ms 124372 KB Output is correct
15 Correct 261 ms 129872 KB Output is correct
16 Correct 398 ms 148160 KB Output is correct
17 Correct 220 ms 132560 KB Output is correct
18 Correct 242 ms 128972 KB Output is correct
19 Correct 224 ms 131548 KB Output is correct
20 Correct 217 ms 131440 KB Output is correct
21 Correct 222 ms 132708 KB Output is correct
22 Correct 191 ms 123100 KB Output is correct
23 Correct 27 ms 56700 KB Output is correct
24 Correct 27 ms 56636 KB Output is correct
25 Correct 30 ms 56820 KB Output is correct
26 Correct 30 ms 56884 KB Output is correct
27 Correct 31 ms 56824 KB Output is correct
28 Correct 34 ms 56788 KB Output is correct
29 Correct 31 ms 56788 KB Output is correct
30 Correct 29 ms 56788 KB Output is correct
31 Correct 29 ms 56792 KB Output is correct
32 Correct 29 ms 56856 KB Output is correct
33 Correct 30 ms 56860 KB Output is correct
34 Correct 29 ms 56836 KB Output is correct
35 Correct 32 ms 56788 KB Output is correct
36 Correct 27 ms 56748 KB Output is correct
37 Correct 27 ms 56764 KB Output is correct
38 Correct 29 ms 56908 KB Output is correct
39 Correct 31 ms 56832 KB Output is correct
40 Correct 31 ms 56824 KB Output is correct
41 Correct 28 ms 56720 KB Output is correct
42 Correct 29 ms 56872 KB Output is correct
43 Correct 30 ms 56856 KB Output is correct
44 Correct 29 ms 56800 KB Output is correct
45 Correct 28 ms 56684 KB Output is correct
46 Correct 27 ms 56772 KB Output is correct
47 Correct 30 ms 56868 KB Output is correct
48 Correct 27 ms 56816 KB Output is correct
49 Correct 30 ms 56788 KB Output is correct
50 Correct 27 ms 56712 KB Output is correct
51 Correct 29 ms 56856 KB Output is correct
52 Correct 29 ms 56924 KB Output is correct
53 Correct 29 ms 56884 KB Output is correct
54 Correct 29 ms 56800 KB Output is correct
55 Correct 29 ms 56848 KB Output is correct
56 Correct 30 ms 56776 KB Output is correct
57 Correct 30 ms 56828 KB Output is correct
58 Correct 29 ms 56808 KB Output is correct
59 Correct 29 ms 56872 KB Output is correct
60 Correct 96 ms 60256 KB Output is correct
61 Correct 131 ms 117192 KB Output is correct
62 Correct 125 ms 60120 KB Output is correct
63 Correct 110 ms 59992 KB Output is correct
64 Correct 95 ms 60236 KB Output is correct
65 Correct 88 ms 59696 KB Output is correct
66 Correct 47 ms 57644 KB Output is correct
67 Correct 141 ms 107820 KB Output is correct
68 Correct 145 ms 107852 KB Output is correct
69 Correct 133 ms 112448 KB Output is correct
70 Correct 134 ms 112460 KB Output is correct
71 Correct 141 ms 107960 KB Output is correct
72 Correct 147 ms 107936 KB Output is correct
73 Correct 133 ms 109228 KB Output is correct
74 Correct 136 ms 109376 KB Output is correct
75 Correct 51 ms 60880 KB Output is correct
76 Correct 147 ms 107740 KB Output is correct
77 Correct 29 ms 56624 KB Output is correct
78 Correct 29 ms 56652 KB Output is correct
79 Correct 28 ms 56708 KB Output is correct
80 Correct 27 ms 56660 KB Output is correct
81 Correct 40 ms 56804 KB Output is correct
82 Correct 29 ms 56788 KB Output is correct
83 Correct 30 ms 56816 KB Output is correct
84 Correct 30 ms 56700 KB Output is correct
85 Correct 28 ms 56728 KB Output is correct
86 Correct 29 ms 56792 KB Output is correct
87 Correct 28 ms 56652 KB Output is correct
88 Correct 28 ms 56812 KB Output is correct
89 Correct 71 ms 57084 KB Output is correct
90 Correct 102 ms 56964 KB Output is correct
91 Correct 89 ms 57036 KB Output is correct
92 Correct 175 ms 109796 KB Output is correct
93 Correct 364 ms 119940 KB Output is correct
94 Correct 619 ms 132628 KB Output is correct
95 Correct 237 ms 111888 KB Output is correct
96 Correct 226 ms 111856 KB Output is correct
97 Correct 269 ms 111876 KB Output is correct
98 Correct 356 ms 119736 KB Output is correct
99 Correct 296 ms 119944 KB Output is correct
100 Correct 317 ms 120116 KB Output is correct
101 Correct 307 ms 120048 KB Output is correct
102 Correct 331 ms 120160 KB Output is correct
103 Correct 379 ms 128740 KB Output is correct
104 Correct 336 ms 139428 KB Output is correct
105 Correct 332 ms 128024 KB Output is correct
106 Correct 297 ms 119104 KB Output is correct
107 Correct 255 ms 125600 KB Output is correct
108 Correct 259 ms 120672 KB Output is correct
109 Correct 320 ms 125628 KB Output is correct
110 Correct 104 ms 57132 KB Output is correct
111 Correct 74 ms 56972 KB Output is correct
112 Correct 292 ms 127860 KB Output is correct
113 Correct 213 ms 117524 KB Output is correct
114 Correct 241 ms 123100 KB Output is correct
115 Correct 132 ms 108656 KB Output is correct
116 Correct 246 ms 114124 KB Output is correct
117 Correct 826 ms 141360 KB Output is correct
118 Correct 152 ms 107868 KB Output is correct
119 Correct 153 ms 107824 KB Output is correct
120 Correct 48 ms 61708 KB Output is correct
121 Correct 297 ms 115516 KB Output is correct
122 Correct 283 ms 115556 KB Output is correct
123 Correct 216 ms 120068 KB Output is correct
124 Correct 205 ms 120080 KB Output is correct
125 Correct 218 ms 120836 KB Output is correct
126 Correct 435 ms 145088 KB Output is correct
127 Correct 317 ms 131184 KB Output is correct
128 Correct 253 ms 131780 KB Output is correct
129 Correct 263 ms 131220 KB Output is correct
130 Correct 251 ms 131848 KB Output is correct