답안 #600514

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
600514 2022-07-21T00:24:01 Z Bobaa Jail (JOI22_jail) C++17
100 / 100
605 ms 64648 KB
#include <bits/stdc++.h>
using namespace std; 

const int maxn = 120005; 

vector<int> g[maxn]; 
int n, par[maxn], hd[maxn], sz[maxn], dep[maxn], dfn[maxn], seq[maxn], best[maxn], dft, memtp, tg[10 * maxn], tp, indeg[10 * maxn]; 
pair<int, int> walk[maxn], mem[100 * maxn]; 

struct node {
	int up, dwn; 
} seg[maxn << 2]; 

void adde(int u, int v) {
	mem[++memtp] = pair<int, int>(tg[u], v); 
	tg[u] = memtp; 
	++indeg[v]; 
}

void build(int l, int r, int rt) {
	if (l == r) {
		seg[rt].up = l; 
		seg[rt].dwn = l + n; 
		return; 
	}
	seg[rt].up = ++tp; 
	indeg[tp] = 0; 
	tg[tp] = 0; 
	seg[rt].dwn = ++tp; 
	indeg[tp] = 0; 
	tg[tp] = 0; 
	int mid = (l + r) >> 1; 
	build(l, mid, rt << 1); 
	build(mid + 1, r, rt << 1 | 1); 
	adde(seg[rt << 1].up, seg[rt].up); 
	adde(seg[rt << 1 | 1].up, seg[rt].up); 
	adde(seg[rt].dwn, seg[rt << 1].dwn); 
	adde(seg[rt].dwn, seg[rt << 1 | 1].dwn); 
}

void modify(int L, int R, int l, int r, int rt, int up, int idx) {
	if (L <= l && R >= r) {
		if (up) adde(seg[rt].up, idx); 
		else adde(idx, seg[rt].dwn); 
		return; 
	}
	int mid = (l + r) >> 1; 
	if (L <= mid) modify(L, R, l, mid, rt << 1, up, idx); 
	if (R > mid) modify(L, R, mid + 1, r, rt << 1 | 1, up, idx); 
}

void dfs1(int u, int f) {
	par[u] = f; 
	sz[u] = 1; 
	best[u] = 0; 
	for (auto v : g[u]) if (v != f) {
		dep[v] = dep[u] + 1; 
		dfs1(v, u); 
		sz[u] += sz[v]; 
		if (sz[best[u]] < sz[v]) best[u] = v; 
	}
}

void dfs2(int u, int h) {
	hd[u] = h; 
	dfn[u] = ++dft; 
	seq[dft] = u; 
	if (best[u]) dfs2(best[u], h); 
	for (auto v : g[u]) if (v != par[u] && v != best[u]) dfs2(v, v); 
}

void pathadd(int u, int v, int up, int idx) {
	int ban = v; 
	while (hd[u] != hd[v]) {
		if (dep[hd[u]] < dep[hd[v]]) swap(u, v); 
		if (u != ban) modify(dfn[hd[u]], dfn[u], 1, n, 1, up, idx); 
		else if (dfn[hd[u]] < dfn[u]) modify(dfn[hd[u]], dfn[u] - 1, 1, n, 1, up, idx); 
		u = par[hd[u]]; 
	}
	if (dfn[u] > dfn[v]) swap(u, v); 
	if (u != ban && v != ban) modify(dfn[u], dfn[v], 1, n, 1, up, idx); 
	else if (u == ban && dfn[u] < dfn[v]) modify(dfn[u] + 1, dfn[v], 1, n, 1, up, idx); 
	else if (v == ban && dfn[u] < dfn[v]) modify(dfn[u], dfn[v] - 1, 1, n, 1, up, idx); 
}

bool solve() {
	memtp = 0; 
	int m; 
	cin >> n; 
	tp = n * 2; 
	for (int i = 1; i <= n; i++) {
		g[i].clear(); 
		indeg[i] = tg[i] = indeg[i + n] = tg[i + n] = 0;
	}
	build(1, n, 1); 
	for (int i = 1, a, b; i < n; i++) {
		cin >> a >> b; 
		g[a].push_back(b); 
		g[b].push_back(a); 
	}
	dft = 0; 
	dfs1(1, 1); 
	dfs2(1, 1); 
	cin >> m; 
	for (int i = 1; i <= m; i++) {
		indeg[tp + i] = tg[tp + i] = 0; 
		cin >> walk[i].first >> walk[i].second;
	}
	for (int i = 1; i <= m; i++) {
		adde(tp + i, dfn[walk[i].first]); 
		adde(dfn[walk[i].second] + n, tp + i); 
	}
	for (int i = 1; i <= m; i++) {
		int u = walk[i].first, v = walk[i].second; 
		pathadd(u, v, 0, tp + i); 
		pathadd(v, u, 1, tp + i); 
	}
	tp += m; 
	queue<int> q; 
	for (int i = 1; i <= tp; i++) if (!indeg[i]) q.push(i); 
	while (!q.empty()) {
		int u = q.front(); 
		q.pop(); 
		while (tg[u] != 0) {
			if (!--indeg[mem[tg[u]].second]) q.push(mem[tg[u]].second); 
			tg[u] = mem[tg[u]].first; 
		}
	}
	for (int i = 1; i <= tp; i++) if (indeg[i]) return 0; 
	return 1; 
}

int main() {
	ios_base::sync_with_stdio(false); 
	cin.tie(nullptr); 
	int t; 
	cin >> t; 
	while (t--) {
		if (solve()) cout << "Yes" << '\n'; 
		else cout << "No" << '\n'; 
	}
}
# 결과 실행 시간 메모리 Grader output
1 Correct 2 ms 3156 KB Output is correct
2 Correct 2 ms 3144 KB Output is correct
3 Correct 2 ms 3156 KB Output is correct
4 Correct 12 ms 3540 KB Output is correct
5 Correct 26 ms 3984 KB Output is correct
6 Correct 4 ms 3284 KB Output is correct
7 Correct 3 ms 3288 KB Output is correct
8 Correct 4 ms 3284 KB Output is correct
9 Correct 36 ms 5836 KB Output is correct
10 Correct 50 ms 31264 KB Output is correct
11 Correct 8 ms 3360 KB Output is correct
12 Correct 43 ms 4180 KB Output is correct
13 Correct 96 ms 38148 KB Output is correct
14 Correct 72 ms 38248 KB Output is correct
15 Correct 107 ms 44620 KB Output is correct
16 Correct 208 ms 63752 KB Output is correct
17 Correct 121 ms 48336 KB Output is correct
18 Correct 98 ms 37816 KB Output is correct
19 Correct 127 ms 46752 KB Output is correct
20 Correct 115 ms 46740 KB Output is correct
21 Correct 97 ms 48372 KB Output is correct
22 Correct 64 ms 34340 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 2 ms 3156 KB Output is correct
2 Correct 2 ms 3152 KB Output is correct
3 Correct 3 ms 3284 KB Output is correct
4 Correct 3 ms 3288 KB Output is correct
5 Correct 3 ms 3284 KB Output is correct
6 Correct 3 ms 3288 KB Output is correct
7 Correct 3 ms 3284 KB Output is correct
8 Correct 3 ms 3284 KB Output is correct
9 Correct 3 ms 3412 KB Output is correct
10 Correct 3 ms 3284 KB Output is correct
11 Correct 4 ms 3284 KB Output is correct
12 Correct 2 ms 3184 KB Output is correct
13 Correct 2 ms 3156 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 2 ms 3156 KB Output is correct
2 Correct 2 ms 3152 KB Output is correct
3 Correct 3 ms 3284 KB Output is correct
4 Correct 3 ms 3288 KB Output is correct
5 Correct 3 ms 3284 KB Output is correct
6 Correct 3 ms 3288 KB Output is correct
7 Correct 3 ms 3284 KB Output is correct
8 Correct 3 ms 3284 KB Output is correct
9 Correct 3 ms 3412 KB Output is correct
10 Correct 3 ms 3284 KB Output is correct
11 Correct 4 ms 3284 KB Output is correct
12 Correct 2 ms 3184 KB Output is correct
13 Correct 2 ms 3156 KB Output is correct
14 Correct 2 ms 3156 KB Output is correct
15 Correct 2 ms 3156 KB Output is correct
16 Correct 3 ms 3284 KB Output is correct
17 Correct 3 ms 3284 KB Output is correct
18 Correct 3 ms 3284 KB Output is correct
19 Correct 2 ms 3148 KB Output is correct
20 Correct 3 ms 3284 KB Output is correct
21 Correct 3 ms 3288 KB Output is correct
22 Correct 3 ms 3284 KB Output is correct
23 Correct 2 ms 3156 KB Output is correct
24 Correct 2 ms 3144 KB Output is correct
25 Correct 3 ms 3284 KB Output is correct
26 Correct 3 ms 3156 KB Output is correct
27 Correct 3 ms 3284 KB Output is correct
28 Correct 2 ms 3156 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 2 ms 3156 KB Output is correct
2 Correct 2 ms 3152 KB Output is correct
3 Correct 3 ms 3284 KB Output is correct
4 Correct 3 ms 3288 KB Output is correct
5 Correct 3 ms 3284 KB Output is correct
6 Correct 3 ms 3288 KB Output is correct
7 Correct 3 ms 3284 KB Output is correct
8 Correct 3 ms 3284 KB Output is correct
9 Correct 3 ms 3412 KB Output is correct
10 Correct 3 ms 3284 KB Output is correct
11 Correct 4 ms 3284 KB Output is correct
12 Correct 2 ms 3184 KB Output is correct
13 Correct 2 ms 3156 KB Output is correct
14 Correct 2 ms 3156 KB Output is correct
15 Correct 2 ms 3156 KB Output is correct
16 Correct 3 ms 3284 KB Output is correct
17 Correct 3 ms 3284 KB Output is correct
18 Correct 3 ms 3284 KB Output is correct
19 Correct 2 ms 3148 KB Output is correct
20 Correct 3 ms 3284 KB Output is correct
21 Correct 3 ms 3288 KB Output is correct
22 Correct 3 ms 3284 KB Output is correct
23 Correct 2 ms 3156 KB Output is correct
24 Correct 2 ms 3144 KB Output is correct
25 Correct 3 ms 3284 KB Output is correct
26 Correct 3 ms 3156 KB Output is correct
27 Correct 3 ms 3284 KB Output is correct
28 Correct 2 ms 3156 KB Output is correct
29 Correct 3 ms 3328 KB Output is correct
30 Correct 4 ms 3284 KB Output is correct
31 Correct 4 ms 3284 KB Output is correct
32 Correct 4 ms 3156 KB Output is correct
33 Correct 3 ms 3288 KB Output is correct
34 Correct 4 ms 3284 KB Output is correct
35 Correct 4 ms 3284 KB Output is correct
36 Correct 3 ms 3284 KB Output is correct
37 Correct 3 ms 3160 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 2 ms 3156 KB Output is correct
2 Correct 2 ms 3152 KB Output is correct
3 Correct 3 ms 3284 KB Output is correct
4 Correct 3 ms 3288 KB Output is correct
5 Correct 3 ms 3284 KB Output is correct
6 Correct 3 ms 3288 KB Output is correct
7 Correct 3 ms 3284 KB Output is correct
8 Correct 3 ms 3284 KB Output is correct
9 Correct 3 ms 3412 KB Output is correct
10 Correct 3 ms 3284 KB Output is correct
11 Correct 4 ms 3284 KB Output is correct
12 Correct 2 ms 3184 KB Output is correct
13 Correct 2 ms 3156 KB Output is correct
14 Correct 2 ms 3156 KB Output is correct
15 Correct 2 ms 3156 KB Output is correct
16 Correct 3 ms 3284 KB Output is correct
17 Correct 3 ms 3284 KB Output is correct
18 Correct 3 ms 3284 KB Output is correct
19 Correct 2 ms 3148 KB Output is correct
20 Correct 3 ms 3284 KB Output is correct
21 Correct 3 ms 3288 KB Output is correct
22 Correct 3 ms 3284 KB Output is correct
23 Correct 2 ms 3156 KB Output is correct
24 Correct 2 ms 3144 KB Output is correct
25 Correct 3 ms 3284 KB Output is correct
26 Correct 3 ms 3156 KB Output is correct
27 Correct 3 ms 3284 KB Output is correct
28 Correct 2 ms 3156 KB Output is correct
29 Correct 3 ms 3328 KB Output is correct
30 Correct 4 ms 3284 KB Output is correct
31 Correct 4 ms 3284 KB Output is correct
32 Correct 4 ms 3156 KB Output is correct
33 Correct 3 ms 3288 KB Output is correct
34 Correct 4 ms 3284 KB Output is correct
35 Correct 4 ms 3284 KB Output is correct
36 Correct 3 ms 3284 KB Output is correct
37 Correct 3 ms 3160 KB Output is correct
38 Correct 37 ms 5684 KB Output is correct
39 Correct 52 ms 31240 KB Output is correct
40 Correct 48 ms 5628 KB Output is correct
41 Correct 55 ms 5132 KB Output is correct
42 Correct 38 ms 5664 KB Output is correct
43 Correct 33 ms 5328 KB Output is correct
44 Correct 18 ms 3668 KB Output is correct
45 Correct 68 ms 21860 KB Output is correct
46 Correct 68 ms 21836 KB Output is correct
47 Correct 51 ms 26536 KB Output is correct
48 Correct 51 ms 26492 KB Output is correct
49 Correct 57 ms 22160 KB Output is correct
50 Correct 61 ms 22224 KB Output is correct
51 Correct 49 ms 23052 KB Output is correct
52 Correct 48 ms 23244 KB Output is correct
53 Correct 15 ms 4592 KB Output is correct
54 Correct 74 ms 21852 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 2 ms 3152 KB Output is correct
2 Correct 2 ms 3156 KB Output is correct
3 Correct 2 ms 3156 KB Output is correct
4 Correct 2 ms 3156 KB Output is correct
5 Correct 8 ms 3284 KB Output is correct
6 Correct 3 ms 3156 KB Output is correct
7 Correct 3 ms 3156 KB Output is correct
8 Correct 2 ms 3156 KB Output is correct
9 Correct 2 ms 3156 KB Output is correct
10 Correct 2 ms 3156 KB Output is correct
11 Correct 2 ms 3144 KB Output is correct
12 Correct 3 ms 3284 KB Output is correct
13 Correct 29 ms 3668 KB Output is correct
14 Correct 51 ms 3968 KB Output is correct
15 Correct 41 ms 3916 KB Output is correct
16 Correct 122 ms 24088 KB Output is correct
17 Correct 251 ms 35188 KB Output is correct
18 Correct 389 ms 49176 KB Output is correct
19 Correct 147 ms 26428 KB Output is correct
20 Correct 153 ms 26224 KB Output is correct
21 Correct 146 ms 26568 KB Output is correct
22 Correct 266 ms 35044 KB Output is correct
23 Correct 185 ms 35300 KB Output is correct
24 Correct 219 ms 34576 KB Output is correct
25 Correct 269 ms 35688 KB Output is correct
26 Correct 222 ms 35924 KB Output is correct
27 Correct 211 ms 29620 KB Output is correct
28 Correct 152 ms 30840 KB Output is correct
29 Correct 153 ms 30752 KB Output is correct
30 Correct 163 ms 26908 KB Output is correct
31 Correct 124 ms 26784 KB Output is correct
32 Correct 148 ms 26956 KB Output is correct
33 Correct 125 ms 26800 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 2 ms 3156 KB Output is correct
2 Correct 2 ms 3144 KB Output is correct
3 Correct 2 ms 3156 KB Output is correct
4 Correct 12 ms 3540 KB Output is correct
5 Correct 26 ms 3984 KB Output is correct
6 Correct 4 ms 3284 KB Output is correct
7 Correct 3 ms 3288 KB Output is correct
8 Correct 4 ms 3284 KB Output is correct
9 Correct 36 ms 5836 KB Output is correct
10 Correct 50 ms 31264 KB Output is correct
11 Correct 8 ms 3360 KB Output is correct
12 Correct 43 ms 4180 KB Output is correct
13 Correct 96 ms 38148 KB Output is correct
14 Correct 72 ms 38248 KB Output is correct
15 Correct 107 ms 44620 KB Output is correct
16 Correct 208 ms 63752 KB Output is correct
17 Correct 121 ms 48336 KB Output is correct
18 Correct 98 ms 37816 KB Output is correct
19 Correct 127 ms 46752 KB Output is correct
20 Correct 115 ms 46740 KB Output is correct
21 Correct 97 ms 48372 KB Output is correct
22 Correct 64 ms 34340 KB Output is correct
23 Correct 2 ms 3156 KB Output is correct
24 Correct 2 ms 3152 KB Output is correct
25 Correct 3 ms 3284 KB Output is correct
26 Correct 3 ms 3288 KB Output is correct
27 Correct 3 ms 3284 KB Output is correct
28 Correct 3 ms 3288 KB Output is correct
29 Correct 3 ms 3284 KB Output is correct
30 Correct 3 ms 3284 KB Output is correct
31 Correct 3 ms 3412 KB Output is correct
32 Correct 3 ms 3284 KB Output is correct
33 Correct 4 ms 3284 KB Output is correct
34 Correct 2 ms 3184 KB Output is correct
35 Correct 2 ms 3156 KB Output is correct
36 Correct 2 ms 3156 KB Output is correct
37 Correct 2 ms 3156 KB Output is correct
38 Correct 3 ms 3284 KB Output is correct
39 Correct 3 ms 3284 KB Output is correct
40 Correct 3 ms 3284 KB Output is correct
41 Correct 2 ms 3148 KB Output is correct
42 Correct 3 ms 3284 KB Output is correct
43 Correct 3 ms 3288 KB Output is correct
44 Correct 3 ms 3284 KB Output is correct
45 Correct 2 ms 3156 KB Output is correct
46 Correct 2 ms 3144 KB Output is correct
47 Correct 3 ms 3284 KB Output is correct
48 Correct 3 ms 3156 KB Output is correct
49 Correct 3 ms 3284 KB Output is correct
50 Correct 2 ms 3156 KB Output is correct
51 Correct 3 ms 3328 KB Output is correct
52 Correct 4 ms 3284 KB Output is correct
53 Correct 4 ms 3284 KB Output is correct
54 Correct 4 ms 3156 KB Output is correct
55 Correct 3 ms 3288 KB Output is correct
56 Correct 4 ms 3284 KB Output is correct
57 Correct 4 ms 3284 KB Output is correct
58 Correct 3 ms 3284 KB Output is correct
59 Correct 3 ms 3160 KB Output is correct
60 Correct 37 ms 5684 KB Output is correct
61 Correct 52 ms 31240 KB Output is correct
62 Correct 48 ms 5628 KB Output is correct
63 Correct 55 ms 5132 KB Output is correct
64 Correct 38 ms 5664 KB Output is correct
65 Correct 33 ms 5328 KB Output is correct
66 Correct 18 ms 3668 KB Output is correct
67 Correct 68 ms 21860 KB Output is correct
68 Correct 68 ms 21836 KB Output is correct
69 Correct 51 ms 26536 KB Output is correct
70 Correct 51 ms 26492 KB Output is correct
71 Correct 57 ms 22160 KB Output is correct
72 Correct 61 ms 22224 KB Output is correct
73 Correct 49 ms 23052 KB Output is correct
74 Correct 48 ms 23244 KB Output is correct
75 Correct 15 ms 4592 KB Output is correct
76 Correct 74 ms 21852 KB Output is correct
77 Correct 2 ms 3152 KB Output is correct
78 Correct 2 ms 3156 KB Output is correct
79 Correct 2 ms 3156 KB Output is correct
80 Correct 2 ms 3156 KB Output is correct
81 Correct 8 ms 3284 KB Output is correct
82 Correct 3 ms 3156 KB Output is correct
83 Correct 3 ms 3156 KB Output is correct
84 Correct 2 ms 3156 KB Output is correct
85 Correct 2 ms 3156 KB Output is correct
86 Correct 2 ms 3156 KB Output is correct
87 Correct 2 ms 3144 KB Output is correct
88 Correct 3 ms 3284 KB Output is correct
89 Correct 29 ms 3668 KB Output is correct
90 Correct 51 ms 3968 KB Output is correct
91 Correct 41 ms 3916 KB Output is correct
92 Correct 122 ms 24088 KB Output is correct
93 Correct 251 ms 35188 KB Output is correct
94 Correct 389 ms 49176 KB Output is correct
95 Correct 147 ms 26428 KB Output is correct
96 Correct 153 ms 26224 KB Output is correct
97 Correct 146 ms 26568 KB Output is correct
98 Correct 266 ms 35044 KB Output is correct
99 Correct 185 ms 35300 KB Output is correct
100 Correct 219 ms 34576 KB Output is correct
101 Correct 269 ms 35688 KB Output is correct
102 Correct 222 ms 35924 KB Output is correct
103 Correct 211 ms 29620 KB Output is correct
104 Correct 152 ms 30840 KB Output is correct
105 Correct 153 ms 30752 KB Output is correct
106 Correct 163 ms 26908 KB Output is correct
107 Correct 124 ms 26784 KB Output is correct
108 Correct 148 ms 26956 KB Output is correct
109 Correct 125 ms 26800 KB Output is correct
110 Correct 45 ms 4172 KB Output is correct
111 Correct 26 ms 3788 KB Output is correct
112 Correct 158 ms 42692 KB Output is correct
113 Correct 110 ms 32332 KB Output is correct
114 Correct 108 ms 36356 KB Output is correct
115 Correct 44 ms 21988 KB Output is correct
116 Correct 135 ms 26132 KB Output is correct
117 Correct 605 ms 64648 KB Output is correct
118 Correct 73 ms 21960 KB Output is correct
119 Correct 77 ms 22012 KB Output is correct
120 Correct 12 ms 5372 KB Output is correct
121 Correct 200 ms 28696 KB Output is correct
122 Correct 201 ms 28664 KB Output is correct
123 Correct 118 ms 34724 KB Output is correct
124 Correct 101 ms 34748 KB Output is correct
125 Correct 134 ms 35548 KB Output is correct
126 Correct 202 ms 60232 KB Output is correct
127 Correct 162 ms 52032 KB Output is correct
128 Correct 143 ms 53168 KB Output is correct
129 Correct 214 ms 52096 KB Output is correct
130 Correct 154 ms 53068 KB Output is correct