답안 #788250

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
788250 2023-07-20T03:43:28 Z iee Jail (JOI22_jail) C++17
100 / 100
807 ms 150812 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 24 ms 56660 KB Output is correct
2 Correct 25 ms 56660 KB Output is correct
3 Correct 24 ms 56728 KB Output is correct
4 Correct 47 ms 56672 KB Output is correct
5 Correct 73 ms 57428 KB Output is correct
6 Correct 27 ms 56884 KB Output is correct
7 Correct 27 ms 56788 KB Output is correct
8 Correct 28 ms 56916 KB Output is correct
9 Correct 92 ms 61132 KB Output is correct
10 Correct 133 ms 118364 KB Output is correct
11 Correct 37 ms 56844 KB Output is correct
12 Correct 92 ms 57772 KB Output is correct
13 Correct 204 ms 126020 KB Output is correct
14 Correct 197 ms 126204 KB Output is correct
15 Correct 243 ms 131692 KB Output is correct
16 Correct 403 ms 150812 KB Output is correct
17 Correct 219 ms 134500 KB Output is correct
18 Correct 220 ms 131552 KB Output is correct
19 Correct 213 ms 133428 KB Output is correct
20 Correct 237 ms 133368 KB Output is correct
21 Correct 220 ms 134648 KB Output is correct
22 Correct 183 ms 124828 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 28 ms 56716 KB Output is correct
2 Correct 25 ms 56752 KB Output is correct
3 Correct 28 ms 56792 KB Output is correct
4 Correct 27 ms 56756 KB Output is correct
5 Correct 28 ms 56788 KB Output is correct
6 Correct 28 ms 56788 KB Output is correct
7 Correct 28 ms 56788 KB Output is correct
8 Correct 28 ms 56892 KB Output is correct
9 Correct 27 ms 56788 KB Output is correct
10 Correct 27 ms 56848 KB Output is correct
11 Correct 28 ms 56788 KB Output is correct
12 Correct 27 ms 56840 KB Output is correct
13 Correct 29 ms 56804 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 28 ms 56716 KB Output is correct
2 Correct 25 ms 56752 KB Output is correct
3 Correct 28 ms 56792 KB Output is correct
4 Correct 27 ms 56756 KB Output is correct
5 Correct 28 ms 56788 KB Output is correct
6 Correct 28 ms 56788 KB Output is correct
7 Correct 28 ms 56788 KB Output is correct
8 Correct 28 ms 56892 KB Output is correct
9 Correct 27 ms 56788 KB Output is correct
10 Correct 27 ms 56848 KB Output is correct
11 Correct 28 ms 56788 KB Output is correct
12 Correct 27 ms 56840 KB Output is correct
13 Correct 29 ms 56804 KB Output is correct
14 Correct 25 ms 56696 KB Output is correct
15 Correct 24 ms 56664 KB Output is correct
16 Correct 26 ms 56812 KB Output is correct
17 Correct 28 ms 56824 KB Output is correct
18 Correct 27 ms 56788 KB Output is correct
19 Correct 26 ms 56684 KB Output is correct
20 Correct 30 ms 56788 KB Output is correct
21 Correct 28 ms 56904 KB Output is correct
22 Correct 28 ms 56836 KB Output is correct
23 Correct 26 ms 56660 KB Output is correct
24 Correct 26 ms 56748 KB Output is correct
25 Correct 29 ms 56788 KB Output is correct
26 Correct 27 ms 56836 KB Output is correct
27 Correct 27 ms 56788 KB Output is correct
28 Correct 26 ms 56748 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 28 ms 56716 KB Output is correct
2 Correct 25 ms 56752 KB Output is correct
3 Correct 28 ms 56792 KB Output is correct
4 Correct 27 ms 56756 KB Output is correct
5 Correct 28 ms 56788 KB Output is correct
6 Correct 28 ms 56788 KB Output is correct
7 Correct 28 ms 56788 KB Output is correct
8 Correct 28 ms 56892 KB Output is correct
9 Correct 27 ms 56788 KB Output is correct
10 Correct 27 ms 56848 KB Output is correct
11 Correct 28 ms 56788 KB Output is correct
12 Correct 27 ms 56840 KB Output is correct
13 Correct 29 ms 56804 KB Output is correct
14 Correct 25 ms 56696 KB Output is correct
15 Correct 24 ms 56664 KB Output is correct
16 Correct 26 ms 56812 KB Output is correct
17 Correct 28 ms 56824 KB Output is correct
18 Correct 27 ms 56788 KB Output is correct
19 Correct 26 ms 56684 KB Output is correct
20 Correct 30 ms 56788 KB Output is correct
21 Correct 28 ms 56904 KB Output is correct
22 Correct 28 ms 56836 KB Output is correct
23 Correct 26 ms 56660 KB Output is correct
24 Correct 26 ms 56748 KB Output is correct
25 Correct 29 ms 56788 KB Output is correct
26 Correct 27 ms 56836 KB Output is correct
27 Correct 27 ms 56788 KB Output is correct
28 Correct 26 ms 56748 KB Output is correct
29 Correct 27 ms 56916 KB Output is correct
30 Correct 27 ms 56904 KB Output is correct
31 Correct 28 ms 56916 KB Output is correct
32 Correct 28 ms 56864 KB Output is correct
33 Correct 28 ms 56788 KB Output is correct
34 Correct 28 ms 56888 KB Output is correct
35 Correct 28 ms 56880 KB Output is correct
36 Correct 28 ms 56820 KB Output is correct
37 Correct 31 ms 56848 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 28 ms 56716 KB Output is correct
2 Correct 25 ms 56752 KB Output is correct
3 Correct 28 ms 56792 KB Output is correct
4 Correct 27 ms 56756 KB Output is correct
5 Correct 28 ms 56788 KB Output is correct
6 Correct 28 ms 56788 KB Output is correct
7 Correct 28 ms 56788 KB Output is correct
8 Correct 28 ms 56892 KB Output is correct
9 Correct 27 ms 56788 KB Output is correct
10 Correct 27 ms 56848 KB Output is correct
11 Correct 28 ms 56788 KB Output is correct
12 Correct 27 ms 56840 KB Output is correct
13 Correct 29 ms 56804 KB Output is correct
14 Correct 25 ms 56696 KB Output is correct
15 Correct 24 ms 56664 KB Output is correct
16 Correct 26 ms 56812 KB Output is correct
17 Correct 28 ms 56824 KB Output is correct
18 Correct 27 ms 56788 KB Output is correct
19 Correct 26 ms 56684 KB Output is correct
20 Correct 30 ms 56788 KB Output is correct
21 Correct 28 ms 56904 KB Output is correct
22 Correct 28 ms 56836 KB Output is correct
23 Correct 26 ms 56660 KB Output is correct
24 Correct 26 ms 56748 KB Output is correct
25 Correct 29 ms 56788 KB Output is correct
26 Correct 27 ms 56836 KB Output is correct
27 Correct 27 ms 56788 KB Output is correct
28 Correct 26 ms 56748 KB Output is correct
29 Correct 27 ms 56916 KB Output is correct
30 Correct 27 ms 56904 KB Output is correct
31 Correct 28 ms 56916 KB Output is correct
32 Correct 28 ms 56864 KB Output is correct
33 Correct 28 ms 56788 KB Output is correct
34 Correct 28 ms 56888 KB Output is correct
35 Correct 28 ms 56880 KB Output is correct
36 Correct 28 ms 56820 KB Output is correct
37 Correct 31 ms 56848 KB Output is correct
38 Correct 94 ms 61224 KB Output is correct
39 Correct 132 ms 118404 KB Output is correct
40 Correct 100 ms 61028 KB Output is correct
41 Correct 107 ms 60964 KB Output is correct
42 Correct 104 ms 61296 KB Output is correct
43 Correct 84 ms 60632 KB Output is correct
44 Correct 47 ms 57612 KB Output is correct
45 Correct 139 ms 109004 KB Output is correct
46 Correct 157 ms 109012 KB Output is correct
47 Correct 129 ms 113604 KB Output is correct
48 Correct 132 ms 113652 KB Output is correct
49 Correct 139 ms 109240 KB Output is correct
50 Correct 134 ms 109132 KB Output is correct
51 Correct 132 ms 110512 KB Output is correct
52 Correct 131 ms 110360 KB Output is correct
53 Correct 50 ms 60864 KB Output is correct
54 Correct 141 ms 108884 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 25 ms 56640 KB Output is correct
2 Correct 26 ms 56664 KB Output is correct
3 Correct 25 ms 56660 KB Output is correct
4 Correct 24 ms 56752 KB Output is correct
5 Correct 40 ms 56704 KB Output is correct
6 Correct 26 ms 56780 KB Output is correct
7 Correct 26 ms 56756 KB Output is correct
8 Correct 26 ms 56660 KB Output is correct
9 Correct 26 ms 56644 KB Output is correct
10 Correct 28 ms 56680 KB Output is correct
11 Correct 26 ms 56688 KB Output is correct
12 Correct 31 ms 56876 KB Output is correct
13 Correct 71 ms 57296 KB Output is correct
14 Correct 99 ms 57676 KB Output is correct
15 Correct 85 ms 57424 KB Output is correct
16 Correct 175 ms 111088 KB Output is correct
17 Correct 384 ms 121872 KB Output is correct
18 Correct 592 ms 135132 KB Output is correct
19 Correct 229 ms 113228 KB Output is correct
20 Correct 215 ms 113308 KB Output is correct
21 Correct 244 ms 113308 KB Output is correct
22 Correct 362 ms 121688 KB Output is correct
23 Correct 290 ms 121744 KB Output is correct
24 Correct 307 ms 121988 KB Output is correct
25 Correct 298 ms 121884 KB Output is correct
26 Correct 313 ms 122048 KB Output is correct
27 Correct 357 ms 131284 KB Output is correct
28 Correct 324 ms 142104 KB Output is correct
29 Correct 320 ms 130616 KB Output is correct
30 Correct 276 ms 121032 KB Output is correct
31 Correct 256 ms 127448 KB Output is correct
32 Correct 256 ms 122688 KB Output is correct
33 Correct 250 ms 127568 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 24 ms 56660 KB Output is correct
2 Correct 25 ms 56660 KB Output is correct
3 Correct 24 ms 56728 KB Output is correct
4 Correct 47 ms 56672 KB Output is correct
5 Correct 73 ms 57428 KB Output is correct
6 Correct 27 ms 56884 KB Output is correct
7 Correct 27 ms 56788 KB Output is correct
8 Correct 28 ms 56916 KB Output is correct
9 Correct 92 ms 61132 KB Output is correct
10 Correct 133 ms 118364 KB Output is correct
11 Correct 37 ms 56844 KB Output is correct
12 Correct 92 ms 57772 KB Output is correct
13 Correct 204 ms 126020 KB Output is correct
14 Correct 197 ms 126204 KB Output is correct
15 Correct 243 ms 131692 KB Output is correct
16 Correct 403 ms 150812 KB Output is correct
17 Correct 219 ms 134500 KB Output is correct
18 Correct 220 ms 131552 KB Output is correct
19 Correct 213 ms 133428 KB Output is correct
20 Correct 237 ms 133368 KB Output is correct
21 Correct 220 ms 134648 KB Output is correct
22 Correct 183 ms 124828 KB Output is correct
23 Correct 28 ms 56716 KB Output is correct
24 Correct 25 ms 56752 KB Output is correct
25 Correct 28 ms 56792 KB Output is correct
26 Correct 27 ms 56756 KB Output is correct
27 Correct 28 ms 56788 KB Output is correct
28 Correct 28 ms 56788 KB Output is correct
29 Correct 28 ms 56788 KB Output is correct
30 Correct 28 ms 56892 KB Output is correct
31 Correct 27 ms 56788 KB Output is correct
32 Correct 27 ms 56848 KB Output is correct
33 Correct 28 ms 56788 KB Output is correct
34 Correct 27 ms 56840 KB Output is correct
35 Correct 29 ms 56804 KB Output is correct
36 Correct 25 ms 56696 KB Output is correct
37 Correct 24 ms 56664 KB Output is correct
38 Correct 26 ms 56812 KB Output is correct
39 Correct 28 ms 56824 KB Output is correct
40 Correct 27 ms 56788 KB Output is correct
41 Correct 26 ms 56684 KB Output is correct
42 Correct 30 ms 56788 KB Output is correct
43 Correct 28 ms 56904 KB Output is correct
44 Correct 28 ms 56836 KB Output is correct
45 Correct 26 ms 56660 KB Output is correct
46 Correct 26 ms 56748 KB Output is correct
47 Correct 29 ms 56788 KB Output is correct
48 Correct 27 ms 56836 KB Output is correct
49 Correct 27 ms 56788 KB Output is correct
50 Correct 26 ms 56748 KB Output is correct
51 Correct 27 ms 56916 KB Output is correct
52 Correct 27 ms 56904 KB Output is correct
53 Correct 28 ms 56916 KB Output is correct
54 Correct 28 ms 56864 KB Output is correct
55 Correct 28 ms 56788 KB Output is correct
56 Correct 28 ms 56888 KB Output is correct
57 Correct 28 ms 56880 KB Output is correct
58 Correct 28 ms 56820 KB Output is correct
59 Correct 31 ms 56848 KB Output is correct
60 Correct 94 ms 61224 KB Output is correct
61 Correct 132 ms 118404 KB Output is correct
62 Correct 100 ms 61028 KB Output is correct
63 Correct 107 ms 60964 KB Output is correct
64 Correct 104 ms 61296 KB Output is correct
65 Correct 84 ms 60632 KB Output is correct
66 Correct 47 ms 57612 KB Output is correct
67 Correct 139 ms 109004 KB Output is correct
68 Correct 157 ms 109012 KB Output is correct
69 Correct 129 ms 113604 KB Output is correct
70 Correct 132 ms 113652 KB Output is correct
71 Correct 139 ms 109240 KB Output is correct
72 Correct 134 ms 109132 KB Output is correct
73 Correct 132 ms 110512 KB Output is correct
74 Correct 131 ms 110360 KB Output is correct
75 Correct 50 ms 60864 KB Output is correct
76 Correct 141 ms 108884 KB Output is correct
77 Correct 25 ms 56640 KB Output is correct
78 Correct 26 ms 56664 KB Output is correct
79 Correct 25 ms 56660 KB Output is correct
80 Correct 24 ms 56752 KB Output is correct
81 Correct 40 ms 56704 KB Output is correct
82 Correct 26 ms 56780 KB Output is correct
83 Correct 26 ms 56756 KB Output is correct
84 Correct 26 ms 56660 KB Output is correct
85 Correct 26 ms 56644 KB Output is correct
86 Correct 28 ms 56680 KB Output is correct
87 Correct 26 ms 56688 KB Output is correct
88 Correct 31 ms 56876 KB Output is correct
89 Correct 71 ms 57296 KB Output is correct
90 Correct 99 ms 57676 KB Output is correct
91 Correct 85 ms 57424 KB Output is correct
92 Correct 175 ms 111088 KB Output is correct
93 Correct 384 ms 121872 KB Output is correct
94 Correct 592 ms 135132 KB Output is correct
95 Correct 229 ms 113228 KB Output is correct
96 Correct 215 ms 113308 KB Output is correct
97 Correct 244 ms 113308 KB Output is correct
98 Correct 362 ms 121688 KB Output is correct
99 Correct 290 ms 121744 KB Output is correct
100 Correct 307 ms 121988 KB Output is correct
101 Correct 298 ms 121884 KB Output is correct
102 Correct 313 ms 122048 KB Output is correct
103 Correct 357 ms 131284 KB Output is correct
104 Correct 324 ms 142104 KB Output is correct
105 Correct 320 ms 130616 KB Output is correct
106 Correct 276 ms 121032 KB Output is correct
107 Correct 256 ms 127448 KB Output is correct
108 Correct 256 ms 122688 KB Output is correct
109 Correct 250 ms 127568 KB Output is correct
110 Correct 98 ms 57856 KB Output is correct
111 Correct 73 ms 57448 KB Output is correct
112 Correct 293 ms 129780 KB Output is correct
113 Correct 198 ms 118920 KB Output is correct
114 Correct 243 ms 124744 KB Output is correct
115 Correct 125 ms 109376 KB Output is correct
116 Correct 247 ms 116016 KB Output is correct
117 Correct 807 ms 143808 KB Output is correct
118 Correct 151 ms 109116 KB Output is correct
119 Correct 148 ms 109024 KB Output is correct
120 Correct 44 ms 61644 KB Output is correct
121 Correct 277 ms 117408 KB Output is correct
122 Correct 281 ms 117352 KB Output is correct
123 Correct 205 ms 121552 KB Output is correct
124 Correct 198 ms 121512 KB Output is correct
125 Correct 212 ms 122444 KB Output is correct
126 Correct 427 ms 147784 KB Output is correct
127 Correct 267 ms 133272 KB Output is correct
128 Correct 242 ms 133836 KB Output is correct
129 Correct 253 ms 133156 KB Output is correct
130 Correct 245 ms 133816 KB Output is correct