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;
#define all(v) v.begin(), v.end()
typedef long long ll;
const int NMAX = 2e5 + 5;
int n, ans, a[NMAX], u, v;
vector<int> adj[NMAX];
string s;
int dfs(int x, int p){
int v1 = 0, v2 = 0;
for(int& nx : adj[x]){
if(nx == p) continue;
int t = dfs(nx, x);
v1 = max(v1, t);
v2 += max(t, 0);
}
ans = max(ans, v1 + a[x]);
ans = max(ans, v2 - a[x]);
return max(v2 - a[x], a[x]);
}
int main(void){
ios::sync_with_stdio(0); cin.tie(0); cout.tie(0);
cin >> n;
for(int i = 1; i < n; i++){
cin >> u >> v;
adj[u].emplace_back(v);
adj[v].emplace_back(u);
}
cin >>s;
for(int i = 1; i <= n; i++) a[i] = s[i - 1] - '0';
dfs(1, -1);
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... |