답안 #440107

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
440107 2021-07-01T15:31:57 Z Sohsoh84 Power Plant (JOI20_power) C++14
100 / 100
247 ms 50752 KB
// ¯\_(ツ)_/¯
#include <bits/stdc++.h>

using namespace std;

typedef long long ll;
typedef pair<ll, ll> 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 = 1e6 + 10;
const ll INF = 1e9;
const ll MOD = 1e9 + 7; // 998244353; // 1e9 + 9;

int n, dp[MAXN], ans = 0;
bool B[MAXN];
vector<int> adj[MAXN];

void dfs(int v, int p) {
	if (!B[v]) {
		dp[v] = 0;
		for (int u : adj[v]) {
			if (u == p) continue;
			dfs(u, v);
			dp[v] += dp[u];
		}

		ans = max(ans, dp[v]);
		return;
	}

	int mx = 0;
	dp[v] = -1;
	for (int u : adj[v]) {
		if (u == p) continue;
		dfs(u, v);
		dp[v] += dp[u];
		mx = max(mx, dp[u]);
	}

	dp[v] = max(dp[v], 1);
	ans = max({ans, mx + 1, dp[v]});
}

int main() {
	ios_base::sync_with_stdio(false); cin.tie(0); cout.tie(0);
	cin >> n;
	fill(dp, dp + MAXN, -INF);

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

	for (int i = 1; i <= n; i++) {
		char c;
		cin >> c;
		B[i] = (c == '1');
	}
	
	dfs(1, 0);
	
	cout << ans << endl;
	return 0;
}
# 결과 실행 시간 메모리 Grader output
1 Correct 21 ms 27596 KB Output is correct
2 Correct 16 ms 27596 KB Output is correct
3 Correct 18 ms 27644 KB Output is correct
4 Correct 18 ms 27648 KB Output is correct
5 Correct 19 ms 27596 KB Output is correct
6 Correct 17 ms 27596 KB Output is correct
7 Correct 19 ms 27596 KB Output is correct
8 Correct 19 ms 27720 KB Output is correct
9 Correct 18 ms 27704 KB Output is correct
10 Correct 18 ms 27596 KB Output is correct
11 Correct 20 ms 27616 KB Output is correct
12 Correct 19 ms 27596 KB Output is correct
13 Correct 18 ms 27620 KB Output is correct
14 Correct 18 ms 27588 KB Output is correct
15 Correct 19 ms 27600 KB Output is correct
16 Correct 19 ms 27720 KB Output is correct
17 Correct 18 ms 27720 KB Output is correct
18 Correct 19 ms 27596 KB Output is correct
19 Correct 19 ms 27596 KB Output is correct
20 Correct 18 ms 27708 KB Output is correct
21 Correct 18 ms 27596 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 21 ms 27596 KB Output is correct
2 Correct 16 ms 27596 KB Output is correct
3 Correct 18 ms 27644 KB Output is correct
4 Correct 18 ms 27648 KB Output is correct
5 Correct 19 ms 27596 KB Output is correct
6 Correct 17 ms 27596 KB Output is correct
7 Correct 19 ms 27596 KB Output is correct
8 Correct 19 ms 27720 KB Output is correct
9 Correct 18 ms 27704 KB Output is correct
10 Correct 18 ms 27596 KB Output is correct
11 Correct 20 ms 27616 KB Output is correct
12 Correct 19 ms 27596 KB Output is correct
13 Correct 18 ms 27620 KB Output is correct
14 Correct 18 ms 27588 KB Output is correct
15 Correct 19 ms 27600 KB Output is correct
16 Correct 19 ms 27720 KB Output is correct
17 Correct 18 ms 27720 KB Output is correct
18 Correct 19 ms 27596 KB Output is correct
19 Correct 19 ms 27596 KB Output is correct
20 Correct 18 ms 27708 KB Output is correct
21 Correct 18 ms 27596 KB Output is correct
22 Correct 19 ms 27768 KB Output is correct
23 Correct 18 ms 27716 KB Output is correct
24 Correct 18 ms 27740 KB Output is correct
25 Correct 20 ms 27784 KB Output is correct
26 Correct 18 ms 27724 KB Output is correct
27 Correct 22 ms 27728 KB Output is correct
28 Correct 18 ms 27808 KB Output is correct
29 Correct 20 ms 27852 KB Output is correct
30 Correct 18 ms 27788 KB Output is correct
31 Correct 21 ms 27852 KB Output is correct
32 Correct 21 ms 27784 KB Output is correct
33 Correct 18 ms 27788 KB Output is correct
34 Correct 19 ms 27784 KB Output is correct
35 Correct 21 ms 27772 KB Output is correct
36 Correct 20 ms 27724 KB Output is correct
37 Correct 20 ms 27692 KB Output is correct
38 Correct 19 ms 27720 KB Output is correct
39 Correct 18 ms 27852 KB Output is correct
40 Correct 19 ms 27724 KB Output is correct
41 Correct 19 ms 27852 KB Output is correct
42 Correct 18 ms 27724 KB Output is correct
43 Correct 20 ms 27724 KB Output is correct
44 Correct 21 ms 27892 KB Output is correct
45 Correct 20 ms 27724 KB Output is correct
46 Correct 18 ms 27820 KB Output is correct
47 Correct 20 ms 27760 KB Output is correct
48 Correct 19 ms 27792 KB Output is correct
49 Correct 20 ms 27884 KB Output is correct
50 Correct 22 ms 27752 KB Output is correct
51 Correct 21 ms 27728 KB Output is correct
52 Correct 19 ms 27788 KB Output is correct
53 Correct 20 ms 27784 KB Output is correct
54 Correct 19 ms 27724 KB Output is correct
55 Correct 19 ms 27724 KB Output is correct
56 Correct 20 ms 27820 KB Output is correct
57 Correct 20 ms 27852 KB Output is correct
58 Correct 21 ms 27724 KB Output is correct
59 Correct 17 ms 27784 KB Output is correct
60 Correct 22 ms 27852 KB Output is correct
61 Correct 19 ms 27768 KB Output is correct
62 Correct 18 ms 27908 KB Output is correct
63 Correct 21 ms 27724 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 21 ms 27596 KB Output is correct
2 Correct 16 ms 27596 KB Output is correct
3 Correct 18 ms 27644 KB Output is correct
4 Correct 18 ms 27648 KB Output is correct
5 Correct 19 ms 27596 KB Output is correct
6 Correct 17 ms 27596 KB Output is correct
7 Correct 19 ms 27596 KB Output is correct
8 Correct 19 ms 27720 KB Output is correct
9 Correct 18 ms 27704 KB Output is correct
10 Correct 18 ms 27596 KB Output is correct
11 Correct 20 ms 27616 KB Output is correct
12 Correct 19 ms 27596 KB Output is correct
13 Correct 18 ms 27620 KB Output is correct
14 Correct 18 ms 27588 KB Output is correct
15 Correct 19 ms 27600 KB Output is correct
16 Correct 19 ms 27720 KB Output is correct
17 Correct 18 ms 27720 KB Output is correct
18 Correct 19 ms 27596 KB Output is correct
19 Correct 19 ms 27596 KB Output is correct
20 Correct 18 ms 27708 KB Output is correct
21 Correct 18 ms 27596 KB Output is correct
22 Correct 19 ms 27768 KB Output is correct
23 Correct 18 ms 27716 KB Output is correct
24 Correct 18 ms 27740 KB Output is correct
25 Correct 20 ms 27784 KB Output is correct
26 Correct 18 ms 27724 KB Output is correct
27 Correct 22 ms 27728 KB Output is correct
28 Correct 18 ms 27808 KB Output is correct
29 Correct 20 ms 27852 KB Output is correct
30 Correct 18 ms 27788 KB Output is correct
31 Correct 21 ms 27852 KB Output is correct
32 Correct 21 ms 27784 KB Output is correct
33 Correct 18 ms 27788 KB Output is correct
34 Correct 19 ms 27784 KB Output is correct
35 Correct 21 ms 27772 KB Output is correct
36 Correct 20 ms 27724 KB Output is correct
37 Correct 20 ms 27692 KB Output is correct
38 Correct 19 ms 27720 KB Output is correct
39 Correct 18 ms 27852 KB Output is correct
40 Correct 19 ms 27724 KB Output is correct
41 Correct 19 ms 27852 KB Output is correct
42 Correct 18 ms 27724 KB Output is correct
43 Correct 20 ms 27724 KB Output is correct
44 Correct 21 ms 27892 KB Output is correct
45 Correct 20 ms 27724 KB Output is correct
46 Correct 18 ms 27820 KB Output is correct
47 Correct 20 ms 27760 KB Output is correct
48 Correct 19 ms 27792 KB Output is correct
49 Correct 20 ms 27884 KB Output is correct
50 Correct 22 ms 27752 KB Output is correct
51 Correct 21 ms 27728 KB Output is correct
52 Correct 19 ms 27788 KB Output is correct
53 Correct 20 ms 27784 KB Output is correct
54 Correct 19 ms 27724 KB Output is correct
55 Correct 19 ms 27724 KB Output is correct
56 Correct 20 ms 27820 KB Output is correct
57 Correct 20 ms 27852 KB Output is correct
58 Correct 21 ms 27724 KB Output is correct
59 Correct 17 ms 27784 KB Output is correct
60 Correct 22 ms 27852 KB Output is correct
61 Correct 19 ms 27768 KB Output is correct
62 Correct 18 ms 27908 KB Output is correct
63 Correct 21 ms 27724 KB Output is correct
64 Correct 178 ms 36992 KB Output is correct
65 Correct 178 ms 36984 KB Output is correct
66 Correct 247 ms 36964 KB Output is correct
67 Correct 178 ms 37068 KB Output is correct
68 Correct 183 ms 36972 KB Output is correct
69 Correct 221 ms 36872 KB Output is correct
70 Correct 197 ms 36944 KB Output is correct
71 Correct 172 ms 37036 KB Output is correct
72 Correct 200 ms 36932 KB Output is correct
73 Correct 213 ms 49632 KB Output is correct
74 Correct 215 ms 49476 KB Output is correct
75 Correct 192 ms 36920 KB Output is correct
76 Correct 168 ms 36800 KB Output is correct
77 Correct 203 ms 36856 KB Output is correct
78 Correct 243 ms 36860 KB Output is correct
79 Correct 224 ms 36760 KB Output is correct
80 Correct 209 ms 36924 KB Output is correct
81 Correct 160 ms 37236 KB Output is correct
82 Correct 218 ms 37140 KB Output is correct
83 Correct 215 ms 42184 KB Output is correct
84 Correct 185 ms 44116 KB Output is correct
85 Correct 233 ms 43896 KB Output is correct
86 Correct 220 ms 38456 KB Output is correct
87 Correct 172 ms 37648 KB Output is correct
88 Correct 223 ms 38236 KB Output is correct
89 Correct 176 ms 38224 KB Output is correct
90 Correct 217 ms 37908 KB Output is correct
91 Correct 210 ms 38176 KB Output is correct
92 Correct 175 ms 38564 KB Output is correct
93 Correct 221 ms 40940 KB Output is correct
94 Correct 203 ms 42640 KB Output is correct
95 Correct 177 ms 41340 KB Output is correct
96 Correct 180 ms 39576 KB Output is correct
97 Correct 172 ms 38012 KB Output is correct
98 Correct 167 ms 37484 KB Output is correct
99 Correct 167 ms 37332 KB Output is correct
100 Correct 208 ms 40468 KB Output is correct
101 Correct 185 ms 38456 KB Output is correct
102 Correct 183 ms 37828 KB Output is correct
103 Correct 222 ms 37444 KB Output is correct
104 Correct 135 ms 37664 KB Output is correct
105 Correct 246 ms 46464 KB Output is correct
106 Correct 125 ms 37536 KB Output is correct
107 Correct 124 ms 37556 KB Output is correct
108 Correct 229 ms 50384 KB Output is correct
109 Correct 129 ms 37776 KB Output is correct
110 Correct 215 ms 50752 KB Output is correct
111 Correct 230 ms 36748 KB Output is correct