Submission #1031804

# Submission time Handle Problem Language Result Execution time Memory
1031804 2024-07-23T07:31:46 Z 정민찬(#10962) Power Plant (JOI20_power) C++17
100 / 100
394 ms 28640 KB
#include <bits/stdc++.h>

using namespace std;

vector<int> adj[200010];

int sz[200010];
int chk[200010];

int getSize(int x, int p) {
	sz[x] = 1;
	for (auto &y : adj[x]) {
		if (y == p || chk[y]) continue;
		sz[x] += getSize(y, x);
	}
	return sz[x];
}

int getCent(int x, int p, int cap) {
	for (auto &y : adj[x]) {
		if (y == p || chk[y]) continue;
		if (sz[y] * 2 > cap) return getCent(y, x, cap);
	}
	return x;
}

int C[200010];
int dp[200010];

void dfs(int x, int p) {
	int sum = 0;
	for (auto &y : adj[x]) {
		if (y == p || chk[y]) continue;
		dfs(y, x);
		sum += dp[y];
	}
	dp[x] = max(C[x], sum - C[x]);
}

int ans = 0;

void dnc(int x) {
	x = getCent(x, -1, getSize(x, -1));
	dfs(x, -1);
	if (C[x]) {
		for (auto &y : adj[x]) {
			if (chk[y]) continue;
			ans = max(ans, dp[y] + 1);
		}
	}
	ans = max(ans, dp[x]);
	chk[x] = 1;
	for (auto &y : adj[x]) {
		if (!chk[y]) dnc(y);
	}
}

int main() {
	ios_base :: sync_with_stdio(false); cin.tie(NULL);
	int n;
	cin >> n;
	for (int i=0; i<n-1; i++) {
		int u, v;
		cin >> u >> v;
		adj[u].push_back(v);
		adj[v].push_back(u);
	}
	string s;
	cin >> s;
	for (int i=1; i<=n; i++) {
		C[i] = s[i-1] - '0';
	}
	dnc(1);
	cout << ans << '\n';
}
# Verdict Execution time Memory Grader output
1 Correct 3 ms 4956 KB Output is correct
2 Correct 2 ms 4956 KB Output is correct
3 Correct 2 ms 4956 KB Output is correct
4 Correct 2 ms 4956 KB Output is correct
5 Correct 2 ms 4956 KB Output is correct
6 Correct 2 ms 4956 KB Output is correct
7 Correct 2 ms 4956 KB Output is correct
8 Correct 2 ms 4956 KB Output is correct
9 Correct 2 ms 4956 KB Output is correct
10 Correct 2 ms 4956 KB Output is correct
11 Correct 2 ms 4956 KB Output is correct
12 Correct 2 ms 4956 KB Output is correct
13 Correct 2 ms 4956 KB Output is correct
14 Correct 2 ms 4956 KB Output is correct
15 Correct 2 ms 4956 KB Output is correct
16 Correct 2 ms 4956 KB Output is correct
17 Correct 2 ms 4956 KB Output is correct
18 Correct 2 ms 4980 KB Output is correct
19 Correct 2 ms 4964 KB Output is correct
20 Correct 2 ms 4964 KB Output is correct
21 Correct 2 ms 4960 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 3 ms 4956 KB Output is correct
2 Correct 2 ms 4956 KB Output is correct
3 Correct 2 ms 4956 KB Output is correct
4 Correct 2 ms 4956 KB Output is correct
5 Correct 2 ms 4956 KB Output is correct
6 Correct 2 ms 4956 KB Output is correct
7 Correct 2 ms 4956 KB Output is correct
8 Correct 2 ms 4956 KB Output is correct
9 Correct 2 ms 4956 KB Output is correct
10 Correct 2 ms 4956 KB Output is correct
11 Correct 2 ms 4956 KB Output is correct
12 Correct 2 ms 4956 KB Output is correct
13 Correct 2 ms 4956 KB Output is correct
14 Correct 2 ms 4956 KB Output is correct
15 Correct 2 ms 4956 KB Output is correct
16 Correct 2 ms 4956 KB Output is correct
17 Correct 2 ms 4956 KB Output is correct
18 Correct 2 ms 4980 KB Output is correct
19 Correct 2 ms 4964 KB Output is correct
20 Correct 2 ms 4964 KB Output is correct
21 Correct 2 ms 4960 KB Output is correct
22 Correct 3 ms 5212 KB Output is correct
23 Correct 3 ms 5212 KB Output is correct
24 Correct 3 ms 5212 KB Output is correct
25 Correct 5 ms 5224 KB Output is correct
26 Correct 3 ms 5212 KB Output is correct
27 Correct 3 ms 5236 KB Output is correct
28 Correct 4 ms 5208 KB Output is correct
29 Correct 3 ms 5252 KB Output is correct
30 Correct 3 ms 5212 KB Output is correct
31 Correct 3 ms 5212 KB Output is correct
32 Correct 3 ms 5208 KB Output is correct
33 Correct 3 ms 5212 KB Output is correct
34 Correct 3 ms 5216 KB Output is correct
35 Correct 3 ms 5172 KB Output is correct
36 Correct 3 ms 5176 KB Output is correct
37 Correct 3 ms 5212 KB Output is correct
38 Correct 3 ms 5300 KB Output is correct
39 Correct 3 ms 5216 KB Output is correct
40 Correct 3 ms 5216 KB Output is correct
41 Correct 3 ms 5212 KB Output is correct
42 Correct 3 ms 5212 KB Output is correct
43 Correct 3 ms 5304 KB Output is correct
44 Correct 3 ms 5216 KB Output is correct
45 Correct 3 ms 5104 KB Output is correct
46 Correct 3 ms 5296 KB Output is correct
47 Correct 3 ms 5212 KB Output is correct
48 Correct 3 ms 5212 KB Output is correct
49 Correct 3 ms 5168 KB Output is correct
50 Correct 3 ms 5216 KB Output is correct
51 Correct 3 ms 5292 KB Output is correct
52 Correct 3 ms 5468 KB Output is correct
53 Correct 4 ms 5212 KB Output is correct
54 Correct 4 ms 5212 KB Output is correct
55 Correct 3 ms 5212 KB Output is correct
56 Correct 3 ms 5212 KB Output is correct
57 Correct 3 ms 5168 KB Output is correct
58 Correct 2 ms 5212 KB Output is correct
59 Correct 3 ms 5212 KB Output is correct
60 Correct 3 ms 5212 KB Output is correct
61 Correct 2 ms 5212 KB Output is correct
62 Correct 3 ms 5212 KB Output is correct
63 Correct 3 ms 5212 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 3 ms 4956 KB Output is correct
2 Correct 2 ms 4956 KB Output is correct
3 Correct 2 ms 4956 KB Output is correct
4 Correct 2 ms 4956 KB Output is correct
5 Correct 2 ms 4956 KB Output is correct
6 Correct 2 ms 4956 KB Output is correct
7 Correct 2 ms 4956 KB Output is correct
8 Correct 2 ms 4956 KB Output is correct
9 Correct 2 ms 4956 KB Output is correct
10 Correct 2 ms 4956 KB Output is correct
11 Correct 2 ms 4956 KB Output is correct
12 Correct 2 ms 4956 KB Output is correct
13 Correct 2 ms 4956 KB Output is correct
14 Correct 2 ms 4956 KB Output is correct
15 Correct 2 ms 4956 KB Output is correct
16 Correct 2 ms 4956 KB Output is correct
17 Correct 2 ms 4956 KB Output is correct
18 Correct 2 ms 4980 KB Output is correct
19 Correct 2 ms 4964 KB Output is correct
20 Correct 2 ms 4964 KB Output is correct
21 Correct 2 ms 4960 KB Output is correct
22 Correct 3 ms 5212 KB Output is correct
23 Correct 3 ms 5212 KB Output is correct
24 Correct 3 ms 5212 KB Output is correct
25 Correct 5 ms 5224 KB Output is correct
26 Correct 3 ms 5212 KB Output is correct
27 Correct 3 ms 5236 KB Output is correct
28 Correct 4 ms 5208 KB Output is correct
29 Correct 3 ms 5252 KB Output is correct
30 Correct 3 ms 5212 KB Output is correct
31 Correct 3 ms 5212 KB Output is correct
32 Correct 3 ms 5208 KB Output is correct
33 Correct 3 ms 5212 KB Output is correct
34 Correct 3 ms 5216 KB Output is correct
35 Correct 3 ms 5172 KB Output is correct
36 Correct 3 ms 5176 KB Output is correct
37 Correct 3 ms 5212 KB Output is correct
38 Correct 3 ms 5300 KB Output is correct
39 Correct 3 ms 5216 KB Output is correct
40 Correct 3 ms 5216 KB Output is correct
41 Correct 3 ms 5212 KB Output is correct
42 Correct 3 ms 5212 KB Output is correct
43 Correct 3 ms 5304 KB Output is correct
44 Correct 3 ms 5216 KB Output is correct
45 Correct 3 ms 5104 KB Output is correct
46 Correct 3 ms 5296 KB Output is correct
47 Correct 3 ms 5212 KB Output is correct
48 Correct 3 ms 5212 KB Output is correct
49 Correct 3 ms 5168 KB Output is correct
50 Correct 3 ms 5216 KB Output is correct
51 Correct 3 ms 5292 KB Output is correct
52 Correct 3 ms 5468 KB Output is correct
53 Correct 4 ms 5212 KB Output is correct
54 Correct 4 ms 5212 KB Output is correct
55 Correct 3 ms 5212 KB Output is correct
56 Correct 3 ms 5212 KB Output is correct
57 Correct 3 ms 5168 KB Output is correct
58 Correct 2 ms 5212 KB Output is correct
59 Correct 3 ms 5212 KB Output is correct
60 Correct 3 ms 5212 KB Output is correct
61 Correct 2 ms 5212 KB Output is correct
62 Correct 3 ms 5212 KB Output is correct
63 Correct 3 ms 5212 KB Output is correct
64 Correct 219 ms 16452 KB Output is correct
65 Correct 217 ms 17556 KB Output is correct
66 Correct 197 ms 17536 KB Output is correct
67 Correct 205 ms 17576 KB Output is correct
68 Correct 203 ms 17448 KB Output is correct
69 Correct 197 ms 17660 KB Output is correct
70 Correct 200 ms 17568 KB Output is correct
71 Correct 213 ms 17452 KB Output is correct
72 Correct 203 ms 17548 KB Output is correct
73 Correct 348 ms 27804 KB Output is correct
74 Correct 343 ms 27536 KB Output is correct
75 Correct 183 ms 17356 KB Output is correct
76 Correct 205 ms 17560 KB Output is correct
77 Correct 211 ms 17548 KB Output is correct
78 Correct 214 ms 17556 KB Output is correct
79 Correct 251 ms 17400 KB Output is correct
80 Correct 220 ms 17548 KB Output is correct
81 Correct 69 ms 17872 KB Output is correct
82 Correct 89 ms 17708 KB Output is correct
83 Correct 303 ms 21588 KB Output is correct
84 Correct 295 ms 23460 KB Output is correct
85 Correct 328 ms 23008 KB Output is correct
86 Correct 227 ms 18576 KB Output is correct
87 Correct 248 ms 18248 KB Output is correct
88 Correct 181 ms 18640 KB Output is correct
89 Correct 193 ms 18592 KB Output is correct
90 Correct 186 ms 18732 KB Output is correct
91 Correct 175 ms 18800 KB Output is correct
92 Correct 166 ms 18820 KB Output is correct
93 Correct 222 ms 23492 KB Output is correct
94 Correct 208 ms 24408 KB Output is correct
95 Correct 198 ms 21324 KB Output is correct
96 Correct 202 ms 20084 KB Output is correct
97 Correct 168 ms 18424 KB Output is correct
98 Correct 150 ms 18116 KB Output is correct
99 Correct 155 ms 17924 KB Output is correct
100 Correct 185 ms 20392 KB Output is correct
101 Correct 169 ms 18704 KB Output is correct
102 Correct 175 ms 18392 KB Output is correct
103 Correct 178 ms 18100 KB Output is correct
104 Correct 47 ms 18372 KB Output is correct
105 Correct 388 ms 25200 KB Output is correct
106 Correct 48 ms 18372 KB Output is correct
107 Correct 45 ms 18256 KB Output is correct
108 Correct 382 ms 28308 KB Output is correct
109 Correct 45 ms 18304 KB Output is correct
110 Correct 394 ms 28640 KB Output is correct
111 Correct 127 ms 17560 KB Output is correct