답안 #1031803

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
1031803 2024-07-23T07:30:40 Z 정민찬(#10962) Power Plant (JOI20_power) C++17
47 / 100
1500 ms 16136 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);
	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';
	}
	for (int i=1; i<=n; i++) {
		dfs(i, -1);
		if (C[i]) {
			for (auto &j : adj[i]) {
				ans = max(ans, dp[j] + 1);
			}
		}
		ans = max(ans, dp[i]);
	}
	cout << ans << '\n';
}
# 결과 실행 시간 메모리 Grader output
1 Correct 2 ms 4952 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 5164 KB Output is correct
8 Correct 3 ms 4956 KB Output is correct
9 Correct 2 ms 5156 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 4 ms 4976 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 5212 KB Output is correct
17 Correct 2 ms 4956 KB Output is correct
18 Correct 2 ms 4956 KB Output is correct
19 Correct 2 ms 4992 KB Output is correct
20 Correct 2 ms 4960 KB Output is correct
21 Correct 2 ms 4960 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 2 ms 4952 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 5164 KB Output is correct
8 Correct 3 ms 4956 KB Output is correct
9 Correct 2 ms 5156 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 4 ms 4976 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 5212 KB Output is correct
17 Correct 2 ms 4956 KB Output is correct
18 Correct 2 ms 4956 KB Output is correct
19 Correct 2 ms 4992 KB Output is correct
20 Correct 2 ms 4960 KB Output is correct
21 Correct 2 ms 4960 KB Output is correct
22 Correct 33 ms 5468 KB Output is correct
23 Correct 33 ms 5264 KB Output is correct
24 Correct 32 ms 5244 KB Output is correct
25 Correct 32 ms 5212 KB Output is correct
26 Correct 32 ms 5268 KB Output is correct
27 Correct 32 ms 5212 KB Output is correct
28 Correct 32 ms 5212 KB Output is correct
29 Correct 51 ms 5212 KB Output is correct
30 Correct 51 ms 5208 KB Output is correct
31 Correct 28 ms 5212 KB Output is correct
32 Correct 29 ms 5212 KB Output is correct
33 Correct 36 ms 5212 KB Output is correct
34 Correct 38 ms 5216 KB Output is correct
35 Correct 36 ms 5208 KB Output is correct
36 Correct 36 ms 5212 KB Output is correct
37 Correct 25 ms 5212 KB Output is correct
38 Correct 29 ms 5216 KB Output is correct
39 Correct 38 ms 5216 KB Output is correct
40 Correct 36 ms 5216 KB Output is correct
41 Correct 37 ms 5212 KB Output is correct
42 Correct 41 ms 5212 KB Output is correct
43 Correct 33 ms 5304 KB Output is correct
44 Correct 35 ms 5212 KB Output is correct
45 Correct 37 ms 5212 KB Output is correct
46 Correct 34 ms 5208 KB Output is correct
47 Correct 34 ms 5212 KB Output is correct
48 Correct 34 ms 5212 KB Output is correct
49 Correct 34 ms 5208 KB Output is correct
50 Correct 43 ms 5212 KB Output is correct
51 Correct 40 ms 5212 KB Output is correct
52 Correct 34 ms 5212 KB Output is correct
53 Correct 30 ms 5212 KB Output is correct
54 Correct 34 ms 5212 KB Output is correct
55 Correct 39 ms 5212 KB Output is correct
56 Correct 18 ms 5212 KB Output is correct
57 Correct 50 ms 5212 KB Output is correct
58 Correct 17 ms 5212 KB Output is correct
59 Correct 19 ms 5212 KB Output is correct
60 Correct 51 ms 5212 KB Output is correct
61 Correct 20 ms 5212 KB Output is correct
62 Correct 50 ms 5212 KB Output is correct
63 Correct 25 ms 5216 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 2 ms 4952 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 5164 KB Output is correct
8 Correct 3 ms 4956 KB Output is correct
9 Correct 2 ms 5156 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 4 ms 4976 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 5212 KB Output is correct
17 Correct 2 ms 4956 KB Output is correct
18 Correct 2 ms 4956 KB Output is correct
19 Correct 2 ms 4992 KB Output is correct
20 Correct 2 ms 4960 KB Output is correct
21 Correct 2 ms 4960 KB Output is correct
22 Correct 33 ms 5468 KB Output is correct
23 Correct 33 ms 5264 KB Output is correct
24 Correct 32 ms 5244 KB Output is correct
25 Correct 32 ms 5212 KB Output is correct
26 Correct 32 ms 5268 KB Output is correct
27 Correct 32 ms 5212 KB Output is correct
28 Correct 32 ms 5212 KB Output is correct
29 Correct 51 ms 5212 KB Output is correct
30 Correct 51 ms 5208 KB Output is correct
31 Correct 28 ms 5212 KB Output is correct
32 Correct 29 ms 5212 KB Output is correct
33 Correct 36 ms 5212 KB Output is correct
34 Correct 38 ms 5216 KB Output is correct
35 Correct 36 ms 5208 KB Output is correct
36 Correct 36 ms 5212 KB Output is correct
37 Correct 25 ms 5212 KB Output is correct
38 Correct 29 ms 5216 KB Output is correct
39 Correct 38 ms 5216 KB Output is correct
40 Correct 36 ms 5216 KB Output is correct
41 Correct 37 ms 5212 KB Output is correct
42 Correct 41 ms 5212 KB Output is correct
43 Correct 33 ms 5304 KB Output is correct
44 Correct 35 ms 5212 KB Output is correct
45 Correct 37 ms 5212 KB Output is correct
46 Correct 34 ms 5208 KB Output is correct
47 Correct 34 ms 5212 KB Output is correct
48 Correct 34 ms 5212 KB Output is correct
49 Correct 34 ms 5208 KB Output is correct
50 Correct 43 ms 5212 KB Output is correct
51 Correct 40 ms 5212 KB Output is correct
52 Correct 34 ms 5212 KB Output is correct
53 Correct 30 ms 5212 KB Output is correct
54 Correct 34 ms 5212 KB Output is correct
55 Correct 39 ms 5212 KB Output is correct
56 Correct 18 ms 5212 KB Output is correct
57 Correct 50 ms 5212 KB Output is correct
58 Correct 17 ms 5212 KB Output is correct
59 Correct 19 ms 5212 KB Output is correct
60 Correct 51 ms 5212 KB Output is correct
61 Correct 20 ms 5212 KB Output is correct
62 Correct 50 ms 5212 KB Output is correct
63 Correct 25 ms 5216 KB Output is correct
64 Execution timed out 1545 ms 16136 KB Time limit exceeded
65 Halted 0 ms 0 KB -