#include <bits/stdc++.h>
#define pb push_back
#define f first
#define sc second
using namespace std;
typedef long long int ll;
typedef string str;
int n;
str s;
vector <vector<int>> v;
vector <ll> sth, root;
ll ans = 0;
ll dfs1(int nd, int ss){
ll r = '0'-s[nd];
if(v[nd].size() == 1 && s[nd] == '1') r = 1;
for(int x: v[nd]) if(x != ss) r+=dfs1(x, nd);
sth[nd] = max(r, (ll)s[nd] - '0');
return sth[nd];
}
void dfs2(int nd, int ss){
ll sss = sth[nd];
if(ss != -1){
root[ss]+=sth[nd];
sss+=max(sth[ss] - sth[nd], (ll)s[ss] - '0');
}
for(int x: v[nd]) if(ss != x){
sss-=sth[x];
root[x]+=max(sss, (ll)s[nd] - '0');
}
for(int x: v[nd]) if(x != ss) dfs2(x, nd);
}
int main(){
ios_base::sync_with_stdio(0);
cin.tie(0);
cin >> n;
v = vector<vector<int>> (n+1);
sth = vector <ll> (n+1, -1);
root = vector<ll> (n+1, 0);
for(int i = 1; i < n; i++){
int a, b; cin >> a >> b;
v[a].pb(b);
v[b].pb(a);
}
cin >> s;
s = "x"+s;
dfs1(1, -1);
dfs2(1, -1);
for(int i = 1; i <+ n; i++){
root[i] = max(root[i]+'0'-s[i], (ll)s[i] - '0');
ans = max(ans, root[i]);
}
cout << ans << "\n";
}
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Incorrect |
1 ms |
384 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Incorrect |
1 ms |
384 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Incorrect |
1 ms |
384 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |