#include <bits/stdc++.h>
#define int long long
#define F first
#define S second
#define pb push_back
using namespace std;
const int N = 3e5 + 10;
const int inf = 2e15;
const int mod = 1e9 + 7;
int dp[N],a[N],ans = 0;
string s;
vector<int>g[N];
void Dfs(int x,int par) {
int mx = 0;
for(int j : g[x]) {
if(j != par) {
Dfs(j,x);
dp[x] += dp[j];
mx = max(mx,dp[j]);
}
}
dp[x] = max(dp[x] - a[x],a[x]);
ans = max(ans,max(dp[x],mx + a[x]));
}
signed main() {
ios_base::sync_with_stdio(false);
cin.tie(NULL);
int n;
cin >> n;
for(int i = 1; i < n; i++) {
int u,v;
cin >> u >> v;
g[u].pb(v); g[v].pb(u);
}
cin >> s;
s = '.' + s;
for(int i = 1; i <= n; i++) a[i] = s[i] - '0';
Dfs(1,0);
cout << ans;
return 0;
}
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |