#include <cstdio>
#include <stdio.h>
#include <stdbool.h>
#include <iostream>
#include <map>
#include <vector>
#include <climits>
#include <stack>
#include <string>
#include <queue>
#include <algorithm>
#include <set>
#include <unordered_set>
#include <unordered_map>
#include <cmath>
#include <cctype>
#include <bitset>
#include <iomanip>
#include <cstring>
#include <numeric>
#include <cassert>
using namespace std;
#define int long long
#define pii pair<int, int>
#define mp make_pair
#define pb push_back
#define fi first
#define se second
int ans=0;
vector<int> dp;
vector<vector<int> > graph;
string s;
void dfs(int node, int par){
int mx=0;
for (auto num:graph[node])if (num!=par)dfs(num, node), mx=max(mx, dp[num]), dp[node]+=dp[num];
dp[node]=max(dp[node]-(s[node]-'0'), (int)(s[node]-'0'));
ans=max({ans, dp[node], mx+s[node]-'0'});
}
int32_t main(){
ios_base::sync_with_stdio(0);
cin.tie(0);
cout.tie(0);
int n, a, b;
cin>>n;
graph.resize(n+1);
dp.resize(n+1, 0);
for (int i=1; i<n; ++i){
cin>>a>>b;
graph[a].pb(b);
graph[b].pb(a);
}
cin>>s, s=' '+s;
dfs(1, -1);
cout<<ans;
}
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |