답안 #660210

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
660210 2022-11-21T06:55:40 Z Sohsoh84 Jail (JOI22_jail) C++17
100 / 100
2205 ms 400452 KB
#include <bits/stdc++.h>

using namespace std;

typedef long long ll;
typedef pair<int, int> pll;

#define all(x)			(x).begin(),(x).end()
#define X			first
#define Y			second
#define sep			' '
#define endl			'\n'
#define debug(x)		cerr << #x << ": " <<  x << endl;

const ll MAXN = 12e4 + 10;
const ll LOG = 20;

int n, m, t, tin[MAXN], tout[MAXN], S[MAXN], T[MAXN], Par[MAXN][LOG], col[MAXN * LOG * 2],
    tn, gin[MAXN][LOG], gout[MAXN][LOG], H[MAXN], ind_S[MAXN], ind_T[MAXN];
vector<int> adj[MAXN], G[MAXN * LOG * 2];
bool flag = false;

inline void clear() {
	for (int i = 0; i < n + 5; i++) {
		tin[i] = tout[i] = S[i] = T[i] = H[i] = ind_S[i] = ind_T[i] = 0;
		memset(Par[i], 0, sizeof Par[i]);
		memset(gin[i], 0, sizeof gin[i]);
		memset(gout[i], 0, sizeof gout[i]);
		adj[i].clear();
	}
	
	for (int i = 0; i < tn + 10; i++) {
		col[i] = 0;
		G[i].clear();
	}

	n = m = t = tn = 0;
	flag = false;
}

void dfs(int v, int p) {
	H[v] = H[p] + 1;
	tin[v] = ++t;
	Par[v][0] = p;

	for (int u : adj[v])
		if (u != p)
			dfs(u, v);

	tout[v] = t;
}

inline bool par(int u, int v) {
	return tin[u] <= tin[v] && tout[u] >= tout[v];
}

inline int LCA(int u, int v) {
	if (par(u, v)) return u;
	if (par(v, u)) return v;

	for (int i = LOG - 1; i >= 0; i--)
		if (Par[u][i] != 0 && !par(Par[u][i], v))
			u = Par[u][i];

	return Par[u][0];
}

inline int k_par(int v, int k) {
	for (int i = LOG - 1; i >= 0; i--)
		if (k & (1 << i))
			v = Par[v][i];
	return v;
}

inline vector<pll> decomp(int v, int h) {
	vector<pll> ans;
	for (int i = LOG - 1; i >= 0; i--) {
		if (h & (1 << i)) {
			ans.push_back({v, i});
			v = Par[v][i];
		}
	}

	return ans;
}

void cyc(int v) {
	col[v] = 1;
	for (int u : G[v]) {
		if (!col[u]) cyc(u);
		else if (col[u] == 1) flag = true;
	}

	col[v] = 2;
}

inline vector<pll> path_decomp(int u, int v) { // -u
	if (u == v) return {};
	if (par(u, v)) u = k_par(v, H[v] - H[u] - 1);
	else u = Par[u][0];

	int lca = LCA(u, v);
	vector<pll> ans = decomp(u, H[u] - H[lca] + 1), tans = decomp(v, H[v] - H[lca] + 1);
	for (auto e : tans) ans.push_back(e);

	return ans;
}

inline int solve() {	
	clear();

	cin >> n;
	for (int i = 1; i < n; i++) {
		int u, v;
		cin >> u >> v;
		adj[u].push_back(v);
		adj[v].push_back(u);
	}

	cin >> m;
	for (int i = 1; i <= m; i++) {
		cin >> S[i] >> T[i];
		ind_S[S[i]] = i;
		ind_T[T[i]] = i;
	}

	dfs(1, 0);
	for (int v = 1; v <= n; v++) {
		gin[v][0] = ind_S[v];	
		gout[v][0] = ind_T[v];	
	}

	tn = n;

	for (int i = 1; i < LOG; i++) {
		for (int v = 1; v <= n; v++) {
			int p = Par[v][i - 1];
			Par[v][i] = Par[p][i - 1];

			if (gin[v][i - 1] || gin[p][i - 1]) {
				tn++;
				if (gin[v][i - 1]) G[gin[v][i - 1]].push_back(tn);
				if (gin[p][i - 1]) G[gin[p][i - 1]].push_back(tn);

				gin[v][i] = tn;
			} 

			if (gout[v][i - 1] || gout[p][i - 1]) {
				tn++;
				if (gout[v][i - 1]) G[tn].push_back(gout[v][i - 1]);
				if (gout[p][i - 1]) G[tn].push_back(gout[p][i - 1]);
				
				gout[v][i] = tn;
			}
		}
	}

	// check 0
	for (int i = 1; i <= m; i++) {
		vector<pll> svec = path_decomp(S[i], T[i]), tvec = path_decomp(T[i], S[i]);
		for (auto [v, j] : svec) {
			if (gin[v][j]) 
				G[gin[v][j]].push_back(i);
		}

		for (auto [v, j] : tvec) {
			if (gout[v][j])
				G[i].push_back(gout[v][j]);
		}
	}

	for (int i = 1; i <= tn; i++) {
		if (!col[i])
			cyc(i);
	}

	cout << (flag ? "No" : "Yes") << endl;
	return 0;
}

int main() {
	ios_base::sync_with_stdio(false); cin.tie(0); cout.tie(0);
	int q;
	cin >> q;
	while (q--) solve();
	return 0;
}
# 결과 실행 시간 메모리 Grader output
1 Correct 54 ms 115920 KB Output is correct
2 Correct 54 ms 115908 KB Output is correct
3 Correct 54 ms 115876 KB Output is correct
4 Correct 88 ms 116308 KB Output is correct
5 Correct 129 ms 116436 KB Output is correct
6 Correct 56 ms 116252 KB Output is correct
7 Correct 58 ms 116220 KB Output is correct
8 Correct 61 ms 116404 KB Output is correct
9 Correct 318 ms 125472 KB Output is correct
10 Correct 226 ms 169980 KB Output is correct
11 Correct 74 ms 116004 KB Output is correct
12 Correct 177 ms 116576 KB Output is correct
13 Correct 692 ms 315824 KB Output is correct
14 Correct 722 ms 317284 KB Output is correct
15 Correct 1394 ms 344080 KB Output is correct
16 Correct 2053 ms 400452 KB Output is correct
17 Correct 476 ms 248144 KB Output is correct
18 Correct 657 ms 326380 KB Output is correct
19 Correct 509 ms 260432 KB Output is correct
20 Correct 548 ms 260440 KB Output is correct
21 Correct 1142 ms 348776 KB Output is correct
22 Correct 722 ms 317528 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 58 ms 115848 KB Output is correct
2 Correct 62 ms 115920 KB Output is correct
3 Correct 64 ms 116148 KB Output is correct
4 Correct 65 ms 116312 KB Output is correct
5 Correct 76 ms 116292 KB Output is correct
6 Correct 73 ms 116268 KB Output is correct
7 Correct 75 ms 116252 KB Output is correct
8 Correct 67 ms 116232 KB Output is correct
9 Correct 64 ms 116356 KB Output is correct
10 Correct 60 ms 116248 KB Output is correct
11 Correct 65 ms 116320 KB Output is correct
12 Correct 61 ms 116336 KB Output is correct
13 Correct 59 ms 116320 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 58 ms 115848 KB Output is correct
2 Correct 62 ms 115920 KB Output is correct
3 Correct 64 ms 116148 KB Output is correct
4 Correct 65 ms 116312 KB Output is correct
5 Correct 76 ms 116292 KB Output is correct
6 Correct 73 ms 116268 KB Output is correct
7 Correct 75 ms 116252 KB Output is correct
8 Correct 67 ms 116232 KB Output is correct
9 Correct 64 ms 116356 KB Output is correct
10 Correct 60 ms 116248 KB Output is correct
11 Correct 65 ms 116320 KB Output is correct
12 Correct 61 ms 116336 KB Output is correct
13 Correct 59 ms 116320 KB Output is correct
14 Correct 56 ms 115916 KB Output is correct
15 Correct 58 ms 115956 KB Output is correct
16 Correct 70 ms 116172 KB Output is correct
17 Correct 69 ms 116276 KB Output is correct
18 Correct 64 ms 116212 KB Output is correct
19 Correct 63 ms 115920 KB Output is correct
20 Correct 69 ms 116256 KB Output is correct
21 Correct 78 ms 116276 KB Output is correct
22 Correct 62 ms 116164 KB Output is correct
23 Correct 59 ms 115888 KB Output is correct
24 Correct 57 ms 115916 KB Output is correct
25 Correct 64 ms 116240 KB Output is correct
26 Correct 63 ms 115984 KB Output is correct
27 Correct 66 ms 116368 KB Output is correct
28 Correct 57 ms 115896 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 58 ms 115848 KB Output is correct
2 Correct 62 ms 115920 KB Output is correct
3 Correct 64 ms 116148 KB Output is correct
4 Correct 65 ms 116312 KB Output is correct
5 Correct 76 ms 116292 KB Output is correct
6 Correct 73 ms 116268 KB Output is correct
7 Correct 75 ms 116252 KB Output is correct
8 Correct 67 ms 116232 KB Output is correct
9 Correct 64 ms 116356 KB Output is correct
10 Correct 60 ms 116248 KB Output is correct
11 Correct 65 ms 116320 KB Output is correct
12 Correct 61 ms 116336 KB Output is correct
13 Correct 59 ms 116320 KB Output is correct
14 Correct 56 ms 115916 KB Output is correct
15 Correct 58 ms 115956 KB Output is correct
16 Correct 70 ms 116172 KB Output is correct
17 Correct 69 ms 116276 KB Output is correct
18 Correct 64 ms 116212 KB Output is correct
19 Correct 63 ms 115920 KB Output is correct
20 Correct 69 ms 116256 KB Output is correct
21 Correct 78 ms 116276 KB Output is correct
22 Correct 62 ms 116164 KB Output is correct
23 Correct 59 ms 115888 KB Output is correct
24 Correct 57 ms 115916 KB Output is correct
25 Correct 64 ms 116240 KB Output is correct
26 Correct 63 ms 115984 KB Output is correct
27 Correct 66 ms 116368 KB Output is correct
28 Correct 57 ms 115896 KB Output is correct
29 Correct 66 ms 116312 KB Output is correct
30 Correct 64 ms 116304 KB Output is correct
31 Correct 78 ms 116420 KB Output is correct
32 Correct 59 ms 116268 KB Output is correct
33 Correct 69 ms 116216 KB Output is correct
34 Correct 64 ms 116108 KB Output is correct
35 Correct 64 ms 116172 KB Output is correct
36 Correct 64 ms 116208 KB Output is correct
37 Correct 66 ms 116236 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 58 ms 115848 KB Output is correct
2 Correct 62 ms 115920 KB Output is correct
3 Correct 64 ms 116148 KB Output is correct
4 Correct 65 ms 116312 KB Output is correct
5 Correct 76 ms 116292 KB Output is correct
6 Correct 73 ms 116268 KB Output is correct
7 Correct 75 ms 116252 KB Output is correct
8 Correct 67 ms 116232 KB Output is correct
9 Correct 64 ms 116356 KB Output is correct
10 Correct 60 ms 116248 KB Output is correct
11 Correct 65 ms 116320 KB Output is correct
12 Correct 61 ms 116336 KB Output is correct
13 Correct 59 ms 116320 KB Output is correct
14 Correct 56 ms 115916 KB Output is correct
15 Correct 58 ms 115956 KB Output is correct
16 Correct 70 ms 116172 KB Output is correct
17 Correct 69 ms 116276 KB Output is correct
18 Correct 64 ms 116212 KB Output is correct
19 Correct 63 ms 115920 KB Output is correct
20 Correct 69 ms 116256 KB Output is correct
21 Correct 78 ms 116276 KB Output is correct
22 Correct 62 ms 116164 KB Output is correct
23 Correct 59 ms 115888 KB Output is correct
24 Correct 57 ms 115916 KB Output is correct
25 Correct 64 ms 116240 KB Output is correct
26 Correct 63 ms 115984 KB Output is correct
27 Correct 66 ms 116368 KB Output is correct
28 Correct 57 ms 115896 KB Output is correct
29 Correct 66 ms 116312 KB Output is correct
30 Correct 64 ms 116304 KB Output is correct
31 Correct 78 ms 116420 KB Output is correct
32 Correct 59 ms 116268 KB Output is correct
33 Correct 69 ms 116216 KB Output is correct
34 Correct 64 ms 116108 KB Output is correct
35 Correct 64 ms 116172 KB Output is correct
36 Correct 64 ms 116208 KB Output is correct
37 Correct 66 ms 116236 KB Output is correct
38 Correct 382 ms 126364 KB Output is correct
39 Correct 253 ms 170940 KB Output is correct
40 Correct 336 ms 126956 KB Output is correct
41 Correct 170 ms 123060 KB Output is correct
42 Correct 189 ms 126124 KB Output is correct
43 Correct 250 ms 124984 KB Output is correct
44 Correct 83 ms 117488 KB Output is correct
45 Correct 277 ms 182732 KB Output is correct
46 Correct 291 ms 179648 KB Output is correct
47 Correct 138 ms 155508 KB Output is correct
48 Correct 146 ms 155468 KB Output is correct
49 Correct 351 ms 198104 KB Output is correct
50 Correct 312 ms 191528 KB Output is correct
51 Correct 440 ms 238480 KB Output is correct
52 Correct 570 ms 248060 KB Output is correct
53 Correct 108 ms 123812 KB Output is correct
54 Correct 196 ms 157124 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 68 ms 115916 KB Output is correct
2 Correct 73 ms 115860 KB Output is correct
3 Correct 60 ms 115952 KB Output is correct
4 Correct 65 ms 115844 KB Output is correct
5 Correct 85 ms 116044 KB Output is correct
6 Correct 67 ms 116324 KB Output is correct
7 Correct 59 ms 116300 KB Output is correct
8 Correct 59 ms 115868 KB Output is correct
9 Correct 57 ms 115852 KB Output is correct
10 Correct 56 ms 115916 KB Output is correct
11 Correct 56 ms 115916 KB Output is correct
12 Correct 65 ms 116316 KB Output is correct
13 Correct 120 ms 116624 KB Output is correct
14 Correct 141 ms 117132 KB Output is correct
15 Correct 149 ms 116836 KB Output is correct
16 Correct 499 ms 220336 KB Output is correct
17 Correct 1286 ms 314340 KB Output is correct
18 Correct 1478 ms 341372 KB Output is correct
19 Correct 338 ms 179244 KB Output is correct
20 Correct 402 ms 191904 KB Output is correct
21 Correct 322 ms 184148 KB Output is correct
22 Correct 924 ms 269092 KB Output is correct
23 Correct 788 ms 267604 KB Output is correct
24 Correct 806 ms 267668 KB Output is correct
25 Correct 802 ms 267784 KB Output is correct
26 Correct 768 ms 267672 KB Output is correct
27 Correct 805 ms 318912 KB Output is correct
28 Correct 982 ms 327364 KB Output is correct
29 Correct 1020 ms 321348 KB Output is correct
30 Correct 797 ms 278412 KB Output is correct
31 Correct 702 ms 279560 KB Output is correct
32 Correct 944 ms 275360 KB Output is correct
33 Correct 899 ms 278120 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 54 ms 115920 KB Output is correct
2 Correct 54 ms 115908 KB Output is correct
3 Correct 54 ms 115876 KB Output is correct
4 Correct 88 ms 116308 KB Output is correct
5 Correct 129 ms 116436 KB Output is correct
6 Correct 56 ms 116252 KB Output is correct
7 Correct 58 ms 116220 KB Output is correct
8 Correct 61 ms 116404 KB Output is correct
9 Correct 318 ms 125472 KB Output is correct
10 Correct 226 ms 169980 KB Output is correct
11 Correct 74 ms 116004 KB Output is correct
12 Correct 177 ms 116576 KB Output is correct
13 Correct 692 ms 315824 KB Output is correct
14 Correct 722 ms 317284 KB Output is correct
15 Correct 1394 ms 344080 KB Output is correct
16 Correct 2053 ms 400452 KB Output is correct
17 Correct 476 ms 248144 KB Output is correct
18 Correct 657 ms 326380 KB Output is correct
19 Correct 509 ms 260432 KB Output is correct
20 Correct 548 ms 260440 KB Output is correct
21 Correct 1142 ms 348776 KB Output is correct
22 Correct 722 ms 317528 KB Output is correct
23 Correct 58 ms 115848 KB Output is correct
24 Correct 62 ms 115920 KB Output is correct
25 Correct 64 ms 116148 KB Output is correct
26 Correct 65 ms 116312 KB Output is correct
27 Correct 76 ms 116292 KB Output is correct
28 Correct 73 ms 116268 KB Output is correct
29 Correct 75 ms 116252 KB Output is correct
30 Correct 67 ms 116232 KB Output is correct
31 Correct 64 ms 116356 KB Output is correct
32 Correct 60 ms 116248 KB Output is correct
33 Correct 65 ms 116320 KB Output is correct
34 Correct 61 ms 116336 KB Output is correct
35 Correct 59 ms 116320 KB Output is correct
36 Correct 56 ms 115916 KB Output is correct
37 Correct 58 ms 115956 KB Output is correct
38 Correct 70 ms 116172 KB Output is correct
39 Correct 69 ms 116276 KB Output is correct
40 Correct 64 ms 116212 KB Output is correct
41 Correct 63 ms 115920 KB Output is correct
42 Correct 69 ms 116256 KB Output is correct
43 Correct 78 ms 116276 KB Output is correct
44 Correct 62 ms 116164 KB Output is correct
45 Correct 59 ms 115888 KB Output is correct
46 Correct 57 ms 115916 KB Output is correct
47 Correct 64 ms 116240 KB Output is correct
48 Correct 63 ms 115984 KB Output is correct
49 Correct 66 ms 116368 KB Output is correct
50 Correct 57 ms 115896 KB Output is correct
51 Correct 66 ms 116312 KB Output is correct
52 Correct 64 ms 116304 KB Output is correct
53 Correct 78 ms 116420 KB Output is correct
54 Correct 59 ms 116268 KB Output is correct
55 Correct 69 ms 116216 KB Output is correct
56 Correct 64 ms 116108 KB Output is correct
57 Correct 64 ms 116172 KB Output is correct
58 Correct 64 ms 116208 KB Output is correct
59 Correct 66 ms 116236 KB Output is correct
60 Correct 382 ms 126364 KB Output is correct
61 Correct 253 ms 170940 KB Output is correct
62 Correct 336 ms 126956 KB Output is correct
63 Correct 170 ms 123060 KB Output is correct
64 Correct 189 ms 126124 KB Output is correct
65 Correct 250 ms 124984 KB Output is correct
66 Correct 83 ms 117488 KB Output is correct
67 Correct 277 ms 182732 KB Output is correct
68 Correct 291 ms 179648 KB Output is correct
69 Correct 138 ms 155508 KB Output is correct
70 Correct 146 ms 155468 KB Output is correct
71 Correct 351 ms 198104 KB Output is correct
72 Correct 312 ms 191528 KB Output is correct
73 Correct 440 ms 238480 KB Output is correct
74 Correct 570 ms 248060 KB Output is correct
75 Correct 108 ms 123812 KB Output is correct
76 Correct 196 ms 157124 KB Output is correct
77 Correct 68 ms 115916 KB Output is correct
78 Correct 73 ms 115860 KB Output is correct
79 Correct 60 ms 115952 KB Output is correct
80 Correct 65 ms 115844 KB Output is correct
81 Correct 85 ms 116044 KB Output is correct
82 Correct 67 ms 116324 KB Output is correct
83 Correct 59 ms 116300 KB Output is correct
84 Correct 59 ms 115868 KB Output is correct
85 Correct 57 ms 115852 KB Output is correct
86 Correct 56 ms 115916 KB Output is correct
87 Correct 56 ms 115916 KB Output is correct
88 Correct 65 ms 116316 KB Output is correct
89 Correct 120 ms 116624 KB Output is correct
90 Correct 141 ms 117132 KB Output is correct
91 Correct 149 ms 116836 KB Output is correct
92 Correct 499 ms 220336 KB Output is correct
93 Correct 1286 ms 314340 KB Output is correct
94 Correct 1478 ms 341372 KB Output is correct
95 Correct 338 ms 179244 KB Output is correct
96 Correct 402 ms 191904 KB Output is correct
97 Correct 322 ms 184148 KB Output is correct
98 Correct 924 ms 269092 KB Output is correct
99 Correct 788 ms 267604 KB Output is correct
100 Correct 806 ms 267668 KB Output is correct
101 Correct 802 ms 267784 KB Output is correct
102 Correct 768 ms 267672 KB Output is correct
103 Correct 805 ms 318912 KB Output is correct
104 Correct 982 ms 327364 KB Output is correct
105 Correct 1020 ms 321348 KB Output is correct
106 Correct 797 ms 278412 KB Output is correct
107 Correct 702 ms 279560 KB Output is correct
108 Correct 944 ms 275360 KB Output is correct
109 Correct 899 ms 278120 KB Output is correct
110 Correct 174 ms 117464 KB Output is correct
111 Correct 144 ms 116764 KB Output is correct
112 Correct 1553 ms 351892 KB Output is correct
113 Correct 349 ms 181908 KB Output is correct
114 Correct 1185 ms 323892 KB Output is correct
115 Correct 292 ms 235744 KB Output is correct
116 Correct 818 ms 274112 KB Output is correct
117 Correct 1779 ms 345784 KB Output is correct
118 Correct 250 ms 164408 KB Output is correct
119 Correct 250 ms 165692 KB Output is correct
120 Correct 119 ms 129104 KB Output is correct
121 Correct 1102 ms 302824 KB Output is correct
122 Correct 848 ms 274756 KB Output is correct
123 Correct 542 ms 216648 KB Output is correct
124 Correct 888 ms 263448 KB Output is correct
125 Correct 403 ms 192584 KB Output is correct
126 Correct 2205 ms 397288 KB Output is correct
127 Correct 1071 ms 283896 KB Output is correct
128 Correct 863 ms 284892 KB Output is correct
129 Correct 713 ms 261012 KB Output is correct
130 Correct 844 ms 279968 KB Output is correct