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>
using namespace std;
const int maxn = 2e5 + 5;
int t, dp[maxn], n, ans;
vector<int> V[maxn];
string s;
void dfs(int u,int p) {
int m = 0, all = 0;
for(int i = 0; i < V[u].size(); i++) {
if(V[u][i] == p) continue;
dfs(V[u][i], u);
all += dp[V[u][i]];
m = max(m, dp[V[u][i]]);
}
dp[u] = max(m - (s[u] == '1'), 0 + (s[u] == '1'));
dp[u] = max(dp[u], all - (s[u] == '1'));
ans = max(ans, m + (s[u] == '1')); ans = max(ans, dp[u]);
}
main(){
cin >> n;
for(int i = 2; i <= n; i++) {
int u,v;
cin >> u >> v;
V[u].push_back(v); V[v].push_back(u);
}
cin >> s; s = '#' + s;
dfs(1, 0);
cout << max(ans, dp[1]);
}
Compilation message (stderr)
power.cpp: In function 'void dfs(int, int)':
power.cpp:9:19: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
9 | for(int i = 0; i < V[u].size(); i++) {
| ~~^~~~~~~~~~~~~
power.cpp: At global scope:
power.cpp:19:1: warning: ISO C++ forbids declaration of 'main' with no type [-Wreturn-type]
19 | main(){
| ^~~~
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |