#include <bits/stdc++.h>
using namespace std;
#define int long long
mt19937_64 RNG(chrono::steady_clock::now().time_since_epoch().count());
string power_plants;
const int N = 2e5 + 5;
vector<int> g[N];
int dp[N];
int ans = 1;
void dfs(int node, int par) {
int sum = 0, maxi = 0;
for(int i = 0; i < g[node].size(); i++) {
if(g[node][i] == par) continue;
dfs(g[node][i], node);
sum += dp[g[node][i]];
maxi = max(maxi, dp[g[node][i]]);
}
if(power_plants[node - 1] == '0') {
dp[node] = sum;
ans = max(ans, sum);
}
else {
dp[node] = max(1LL, sum - 1);
ans = max(ans, sum - 1);
ans = max(ans, maxi + 1);
dp[node] = max(dp[node], maxi - 1);
}
//cout << "node = " << node << ", dp[node][0] = " << dp[node][0] << ", dp[node][1] = " << dp[node][1] << "\n";
}
void Solve() {
int n;
cin >> n;
for(int i = 0; i < n - 1; i++) {
int u, v;
cin >> u >> v;
g[u].push_back(v);
g[v].push_back(u);
}
cin >> power_plants;
dfs(1, -1);
cout << ans << "\n";
}
int32_t main() {
auto begin = std::chrono::high_resolution_clock::now();
ios_base::sync_with_stdio(0);
cin.tie(0);
int t = 1;
//cin >> t;
while(t--) {
Solve();
}
auto end = std::chrono::high_resolution_clock::now();
auto elapsed = std::chrono::duration_cast<std::chrono::nanoseconds>(end - begin);
cerr << "Time measured: " << elapsed.count() * 1e-9 << " seconds.\n";
return 0;
}
Compilation message
power.cpp: In function 'void dfs(long long int, long long int)':
power.cpp:15:19: warning: comparison of integer expressions of different signedness: 'long long int' and 'std::vector<long long int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
15 | for(int i = 0; i < g[node].size(); i++) {
| ~~^~~~~~~~~~~~~~~~
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
2 ms |
4940 KB |
Output is correct |
2 |
Correct |
3 ms |
4940 KB |
Output is correct |
3 |
Correct |
3 ms |
5020 KB |
Output is correct |
4 |
Correct |
3 ms |
5016 KB |
Output is correct |
5 |
Correct |
3 ms |
4952 KB |
Output is correct |
6 |
Correct |
3 ms |
4904 KB |
Output is correct |
7 |
Correct |
3 ms |
4940 KB |
Output is correct |
8 |
Correct |
3 ms |
5012 KB |
Output is correct |
9 |
Correct |
3 ms |
4940 KB |
Output is correct |
10 |
Correct |
3 ms |
5060 KB |
Output is correct |
11 |
Correct |
3 ms |
4940 KB |
Output is correct |
12 |
Correct |
3 ms |
4940 KB |
Output is correct |
13 |
Correct |
3 ms |
4940 KB |
Output is correct |
14 |
Correct |
3 ms |
4940 KB |
Output is correct |
15 |
Correct |
3 ms |
4940 KB |
Output is correct |
16 |
Correct |
3 ms |
5016 KB |
Output is correct |
17 |
Correct |
3 ms |
4940 KB |
Output is correct |
18 |
Correct |
3 ms |
5016 KB |
Output is correct |
19 |
Correct |
3 ms |
5020 KB |
Output is correct |
20 |
Correct |
3 ms |
4940 KB |
Output is correct |
21 |
Correct |
3 ms |
4940 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
2 ms |
4940 KB |
Output is correct |
2 |
Correct |
3 ms |
4940 KB |
Output is correct |
3 |
Correct |
3 ms |
5020 KB |
Output is correct |
4 |
Correct |
3 ms |
5016 KB |
Output is correct |
5 |
Correct |
3 ms |
4952 KB |
Output is correct |
6 |
Correct |
3 ms |
4904 KB |
Output is correct |
7 |
Correct |
3 ms |
4940 KB |
Output is correct |
8 |
Correct |
3 ms |
5012 KB |
Output is correct |
9 |
Correct |
3 ms |
4940 KB |
Output is correct |
10 |
Correct |
3 ms |
5060 KB |
Output is correct |
11 |
Correct |
3 ms |
4940 KB |
Output is correct |
12 |
Correct |
3 ms |
4940 KB |
Output is correct |
13 |
Correct |
3 ms |
4940 KB |
Output is correct |
14 |
Correct |
3 ms |
4940 KB |
Output is correct |
15 |
Correct |
3 ms |
4940 KB |
Output is correct |
16 |
Correct |
3 ms |
5016 KB |
Output is correct |
17 |
Correct |
3 ms |
4940 KB |
Output is correct |
18 |
Correct |
3 ms |
5016 KB |
Output is correct |
19 |
Correct |
3 ms |
5020 KB |
Output is correct |
20 |
Correct |
3 ms |
4940 KB |
Output is correct |
21 |
Correct |
3 ms |
4940 KB |
Output is correct |
22 |
Correct |
4 ms |
5068 KB |
Output is correct |
23 |
Correct |
3 ms |
5032 KB |
Output is correct |
24 |
Correct |
3 ms |
5068 KB |
Output is correct |
25 |
Correct |
3 ms |
5068 KB |
Output is correct |
26 |
Correct |
3 ms |
5028 KB |
Output is correct |
27 |
Correct |
3 ms |
5024 KB |
Output is correct |
28 |
Correct |
4 ms |
5028 KB |
Output is correct |
29 |
Correct |
4 ms |
5196 KB |
Output is correct |
30 |
Correct |
3 ms |
5204 KB |
Output is correct |
31 |
Correct |
3 ms |
5068 KB |
Output is correct |
32 |
Correct |
3 ms |
5068 KB |
Output is correct |
33 |
Correct |
3 ms |
5068 KB |
Output is correct |
34 |
Correct |
3 ms |
5068 KB |
Output is correct |
35 |
Correct |
3 ms |
5068 KB |
Output is correct |
36 |
Correct |
3 ms |
5028 KB |
Output is correct |
37 |
Correct |
3 ms |
5024 KB |
Output is correct |
38 |
Correct |
4 ms |
5024 KB |
Output is correct |
39 |
Correct |
4 ms |
5156 KB |
Output is correct |
40 |
Correct |
3 ms |
5068 KB |
Output is correct |
41 |
Correct |
3 ms |
5088 KB |
Output is correct |
42 |
Correct |
3 ms |
5068 KB |
Output is correct |
43 |
Correct |
3 ms |
5068 KB |
Output is correct |
44 |
Correct |
4 ms |
5152 KB |
Output is correct |
45 |
Correct |
4 ms |
5068 KB |
Output is correct |
46 |
Correct |
3 ms |
5032 KB |
Output is correct |
47 |
Correct |
4 ms |
5068 KB |
Output is correct |
48 |
Correct |
4 ms |
5156 KB |
Output is correct |
49 |
Correct |
3 ms |
5196 KB |
Output is correct |
50 |
Correct |
3 ms |
5068 KB |
Output is correct |
51 |
Correct |
3 ms |
5160 KB |
Output is correct |
52 |
Correct |
3 ms |
5068 KB |
Output is correct |
53 |
Correct |
4 ms |
5068 KB |
Output is correct |
54 |
Correct |
3 ms |
5068 KB |
Output is correct |
55 |
Correct |
3 ms |
5080 KB |
Output is correct |
56 |
Correct |
3 ms |
5028 KB |
Output is correct |
57 |
Correct |
3 ms |
5156 KB |
Output is correct |
58 |
Correct |
3 ms |
5068 KB |
Output is correct |
59 |
Correct |
3 ms |
5068 KB |
Output is correct |
60 |
Correct |
3 ms |
5152 KB |
Output is correct |
61 |
Correct |
3 ms |
5028 KB |
Output is correct |
62 |
Correct |
3 ms |
5196 KB |
Output is correct |
63 |
Correct |
3 ms |
5068 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
2 ms |
4940 KB |
Output is correct |
2 |
Correct |
3 ms |
4940 KB |
Output is correct |
3 |
Correct |
3 ms |
5020 KB |
Output is correct |
4 |
Correct |
3 ms |
5016 KB |
Output is correct |
5 |
Correct |
3 ms |
4952 KB |
Output is correct |
6 |
Correct |
3 ms |
4904 KB |
Output is correct |
7 |
Correct |
3 ms |
4940 KB |
Output is correct |
8 |
Correct |
3 ms |
5012 KB |
Output is correct |
9 |
Correct |
3 ms |
4940 KB |
Output is correct |
10 |
Correct |
3 ms |
5060 KB |
Output is correct |
11 |
Correct |
3 ms |
4940 KB |
Output is correct |
12 |
Correct |
3 ms |
4940 KB |
Output is correct |
13 |
Correct |
3 ms |
4940 KB |
Output is correct |
14 |
Correct |
3 ms |
4940 KB |
Output is correct |
15 |
Correct |
3 ms |
4940 KB |
Output is correct |
16 |
Correct |
3 ms |
5016 KB |
Output is correct |
17 |
Correct |
3 ms |
4940 KB |
Output is correct |
18 |
Correct |
3 ms |
5016 KB |
Output is correct |
19 |
Correct |
3 ms |
5020 KB |
Output is correct |
20 |
Correct |
3 ms |
4940 KB |
Output is correct |
21 |
Correct |
3 ms |
4940 KB |
Output is correct |
22 |
Correct |
4 ms |
5068 KB |
Output is correct |
23 |
Correct |
3 ms |
5032 KB |
Output is correct |
24 |
Correct |
3 ms |
5068 KB |
Output is correct |
25 |
Correct |
3 ms |
5068 KB |
Output is correct |
26 |
Correct |
3 ms |
5028 KB |
Output is correct |
27 |
Correct |
3 ms |
5024 KB |
Output is correct |
28 |
Correct |
4 ms |
5028 KB |
Output is correct |
29 |
Correct |
4 ms |
5196 KB |
Output is correct |
30 |
Correct |
3 ms |
5204 KB |
Output is correct |
31 |
Correct |
3 ms |
5068 KB |
Output is correct |
32 |
Correct |
3 ms |
5068 KB |
Output is correct |
33 |
Correct |
3 ms |
5068 KB |
Output is correct |
34 |
Correct |
3 ms |
5068 KB |
Output is correct |
35 |
Correct |
3 ms |
5068 KB |
Output is correct |
36 |
Correct |
3 ms |
5028 KB |
Output is correct |
37 |
Correct |
3 ms |
5024 KB |
Output is correct |
38 |
Correct |
4 ms |
5024 KB |
Output is correct |
39 |
Correct |
4 ms |
5156 KB |
Output is correct |
40 |
Correct |
3 ms |
5068 KB |
Output is correct |
41 |
Correct |
3 ms |
5088 KB |
Output is correct |
42 |
Correct |
3 ms |
5068 KB |
Output is correct |
43 |
Correct |
3 ms |
5068 KB |
Output is correct |
44 |
Correct |
4 ms |
5152 KB |
Output is correct |
45 |
Correct |
4 ms |
5068 KB |
Output is correct |
46 |
Correct |
3 ms |
5032 KB |
Output is correct |
47 |
Correct |
4 ms |
5068 KB |
Output is correct |
48 |
Correct |
4 ms |
5156 KB |
Output is correct |
49 |
Correct |
3 ms |
5196 KB |
Output is correct |
50 |
Correct |
3 ms |
5068 KB |
Output is correct |
51 |
Correct |
3 ms |
5160 KB |
Output is correct |
52 |
Correct |
3 ms |
5068 KB |
Output is correct |
53 |
Correct |
4 ms |
5068 KB |
Output is correct |
54 |
Correct |
3 ms |
5068 KB |
Output is correct |
55 |
Correct |
3 ms |
5080 KB |
Output is correct |
56 |
Correct |
3 ms |
5028 KB |
Output is correct |
57 |
Correct |
3 ms |
5156 KB |
Output is correct |
58 |
Correct |
3 ms |
5068 KB |
Output is correct |
59 |
Correct |
3 ms |
5068 KB |
Output is correct |
60 |
Correct |
3 ms |
5152 KB |
Output is correct |
61 |
Correct |
3 ms |
5028 KB |
Output is correct |
62 |
Correct |
3 ms |
5196 KB |
Output is correct |
63 |
Correct |
3 ms |
5068 KB |
Output is correct |
64 |
Correct |
123 ms |
16932 KB |
Output is correct |
65 |
Correct |
136 ms |
16988 KB |
Output is correct |
66 |
Correct |
125 ms |
16888 KB |
Output is correct |
67 |
Correct |
122 ms |
16992 KB |
Output is correct |
68 |
Correct |
121 ms |
17052 KB |
Output is correct |
69 |
Correct |
115 ms |
16912 KB |
Output is correct |
70 |
Correct |
118 ms |
16904 KB |
Output is correct |
71 |
Correct |
119 ms |
16888 KB |
Output is correct |
72 |
Correct |
119 ms |
16876 KB |
Output is correct |
73 |
Correct |
154 ms |
28584 KB |
Output is correct |
74 |
Correct |
141 ms |
28476 KB |
Output is correct |
75 |
Correct |
124 ms |
18132 KB |
Output is correct |
76 |
Correct |
115 ms |
18100 KB |
Output is correct |
77 |
Correct |
124 ms |
16840 KB |
Output is correct |
78 |
Correct |
136 ms |
16872 KB |
Output is correct |
79 |
Correct |
116 ms |
16900 KB |
Output is correct |
80 |
Correct |
114 ms |
16832 KB |
Output is correct |
81 |
Correct |
100 ms |
16924 KB |
Output is correct |
82 |
Correct |
110 ms |
17084 KB |
Output is correct |
83 |
Correct |
127 ms |
21968 KB |
Output is correct |
84 |
Correct |
124 ms |
24120 KB |
Output is correct |
85 |
Correct |
125 ms |
23776 KB |
Output is correct |
86 |
Correct |
113 ms |
18272 KB |
Output is correct |
87 |
Correct |
119 ms |
17664 KB |
Output is correct |
88 |
Correct |
112 ms |
17732 KB |
Output is correct |
89 |
Correct |
115 ms |
17740 KB |
Output is correct |
90 |
Correct |
127 ms |
17580 KB |
Output is correct |
91 |
Correct |
116 ms |
17872 KB |
Output is correct |
92 |
Correct |
114 ms |
18132 KB |
Output is correct |
93 |
Correct |
110 ms |
20324 KB |
Output is correct |
94 |
Correct |
123 ms |
22140 KB |
Output is correct |
95 |
Correct |
108 ms |
20788 KB |
Output is correct |
96 |
Correct |
107 ms |
18892 KB |
Output is correct |
97 |
Correct |
117 ms |
17628 KB |
Output is correct |
98 |
Correct |
108 ms |
17076 KB |
Output is correct |
99 |
Correct |
105 ms |
17056 KB |
Output is correct |
100 |
Correct |
109 ms |
19952 KB |
Output is correct |
101 |
Correct |
119 ms |
18028 KB |
Output is correct |
102 |
Correct |
105 ms |
17236 KB |
Output is correct |
103 |
Correct |
104 ms |
17180 KB |
Output is correct |
104 |
Correct |
72 ms |
17536 KB |
Output is correct |
105 |
Correct |
135 ms |
25396 KB |
Output is correct |
106 |
Correct |
75 ms |
17576 KB |
Output is correct |
107 |
Correct |
70 ms |
17428 KB |
Output is correct |
108 |
Correct |
134 ms |
29300 KB |
Output is correct |
109 |
Correct |
77 ms |
17628 KB |
Output is correct |
110 |
Correct |
128 ms |
29676 KB |
Output is correct |
111 |
Correct |
103 ms |
16820 KB |
Output is correct |