#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;
vector <bool> bl;
vector <ll> curr;
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');
root[nd] = r;
return sth[nd];
}
void dfs2(int nd, int ss, ll cur){
for(int x: v[nd]) if(x != ss) cur+=sth[x];
curr[nd] = cur;
for(int x: v[nd]) if(x != ss){
ll cc = cur;
cc-=sth[x];
cc = max(cc, (ll)s[nd] - '0');
root[x] += cc;
dfs2(x, nd, cc + '0' - s[x]);
}
}
int main(){
ios_base::sync_with_stdio(0);
cin.tie(0);
cin >> n;
v = vector<vector<int>> (n+1);
bl = vector<bool> (n+1, 0);
sth = vector<ll> (n+1, -1);
root = vector<ll> (n+1, 0);
curr = root;
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, s[1] - '0');
for(int i = 1; i <= n; i++){
if(bl[i]){
root[i]--;
root[i] = max(root[i], 1LL);
}
}
for(int i = 1; i <= n; i++) 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 |
- |