This submission is migrated from previous version of oj.uz, which used different machine for grading. This submission may have different result if resubmitted.
#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);
dp[v] = max(s[v] - '0', sum - (s[v] - '0'));
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 (stderr)
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:41:9: note: in expansion of macro 'rep'
41 | rep(i, 0, n - 1) {
| ^~~
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |