#include <bits/stdc++.h>
#define rep(i, a, n) for(int (i) = (a); (i) < (n); ++(i))
using namespace std;
const int N = 2e5 + 50;
int n;
vector<int> graph[N];
string s;
int dp[N];
int sol = 0;
int rek(int v, int p) {
int sum = 0, mx = 0, cnt = 0;
for(auto n : graph[v]) {
if(n == p) continue;
int val = rek(n, v);
if(val) cnt++;
sum += val;
mx = max(mx, val);
}
if(s[v] == '0' || cnt > 1) dp[v] = max(s[v] - '0', sum - s[v] + '0');
else dp[v] = max(1, sum);
if(s[v] == '1') {
sol = max(sol, max(mx + 1, dp[v]));
} else {
sol = max(sol, dp[v]);
}
return dp[v];
}
int main() {
cin >> n;
rep(i, 0, n - 1) {
int a, b;
cin >> a >> b;
--a, --b;
graph[a].push_back(b);
graph[b].push_back(a);
}
cin >> s;
rek(0, -1);
printf("%d\n", sol);
return 0;
}
Compilation message
power.cpp: In function 'int main()':
power.cpp:3:30: warning: unnecessary parentheses in declaration of 'i' [-Wparentheses]
3 | #define rep(i, a, n) for(int (i) = (a); (i) < (n); ++(i))
| ^
power.cpp:40:9: note: in expansion of macro 'rep'
40 | rep(i, 0, n - 1) {
| ^~~
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
5 ms |
4992 KB |
Output is correct |
2 |
Correct |
4 ms |
4992 KB |
Output is correct |
3 |
Correct |
4 ms |
4992 KB |
Output is correct |
4 |
Correct |
5 ms |
4992 KB |
Output is correct |
5 |
Correct |
5 ms |
4992 KB |
Output is correct |
6 |
Correct |
4 ms |
5076 KB |
Output is correct |
7 |
Correct |
4 ms |
4992 KB |
Output is correct |
8 |
Correct |
4 ms |
4992 KB |
Output is correct |
9 |
Correct |
4 ms |
4992 KB |
Output is correct |
10 |
Correct |
5 ms |
4992 KB |
Output is correct |
11 |
Correct |
4 ms |
4992 KB |
Output is correct |
12 |
Incorrect |
5 ms |
4992 KB |
Output isn't correct |
13 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
5 ms |
4992 KB |
Output is correct |
2 |
Correct |
4 ms |
4992 KB |
Output is correct |
3 |
Correct |
4 ms |
4992 KB |
Output is correct |
4 |
Correct |
5 ms |
4992 KB |
Output is correct |
5 |
Correct |
5 ms |
4992 KB |
Output is correct |
6 |
Correct |
4 ms |
5076 KB |
Output is correct |
7 |
Correct |
4 ms |
4992 KB |
Output is correct |
8 |
Correct |
4 ms |
4992 KB |
Output is correct |
9 |
Correct |
4 ms |
4992 KB |
Output is correct |
10 |
Correct |
5 ms |
4992 KB |
Output is correct |
11 |
Correct |
4 ms |
4992 KB |
Output is correct |
12 |
Incorrect |
5 ms |
4992 KB |
Output isn't correct |
13 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
5 ms |
4992 KB |
Output is correct |
2 |
Correct |
4 ms |
4992 KB |
Output is correct |
3 |
Correct |
4 ms |
4992 KB |
Output is correct |
4 |
Correct |
5 ms |
4992 KB |
Output is correct |
5 |
Correct |
5 ms |
4992 KB |
Output is correct |
6 |
Correct |
4 ms |
5076 KB |
Output is correct |
7 |
Correct |
4 ms |
4992 KB |
Output is correct |
8 |
Correct |
4 ms |
4992 KB |
Output is correct |
9 |
Correct |
4 ms |
4992 KB |
Output is correct |
10 |
Correct |
5 ms |
4992 KB |
Output is correct |
11 |
Correct |
4 ms |
4992 KB |
Output is correct |
12 |
Incorrect |
5 ms |
4992 KB |
Output isn't correct |
13 |
Halted |
0 ms |
0 KB |
- |