#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;
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');
if(r < sth[nd]) bl[nd] = 1;
return sth[nd];
}
void dfs2(int nd, int ss, ll cur){
for(int x: v[nd]) if(x != ss) cur+=sth[x];
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);
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);
for(int i = 1; i <= n; i++) root[i] = sth[i];
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";
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
384 KB |
Output is correct |
2 |
Correct |
0 ms |
384 KB |
Output is correct |
3 |
Correct |
0 ms |
384 KB |
Output is correct |
4 |
Correct |
0 ms |
384 KB |
Output is correct |
5 |
Correct |
0 ms |
384 KB |
Output is correct |
6 |
Correct |
1 ms |
384 KB |
Output is correct |
7 |
Correct |
0 ms |
384 KB |
Output is correct |
8 |
Correct |
0 ms |
384 KB |
Output is correct |
9 |
Correct |
0 ms |
384 KB |
Output is correct |
10 |
Correct |
0 ms |
384 KB |
Output is correct |
11 |
Correct |
1 ms |
384 KB |
Output is correct |
12 |
Incorrect |
0 ms |
384 KB |
Output isn't correct |
13 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
384 KB |
Output is correct |
2 |
Correct |
0 ms |
384 KB |
Output is correct |
3 |
Correct |
0 ms |
384 KB |
Output is correct |
4 |
Correct |
0 ms |
384 KB |
Output is correct |
5 |
Correct |
0 ms |
384 KB |
Output is correct |
6 |
Correct |
1 ms |
384 KB |
Output is correct |
7 |
Correct |
0 ms |
384 KB |
Output is correct |
8 |
Correct |
0 ms |
384 KB |
Output is correct |
9 |
Correct |
0 ms |
384 KB |
Output is correct |
10 |
Correct |
0 ms |
384 KB |
Output is correct |
11 |
Correct |
1 ms |
384 KB |
Output is correct |
12 |
Incorrect |
0 ms |
384 KB |
Output isn't correct |
13 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
384 KB |
Output is correct |
2 |
Correct |
0 ms |
384 KB |
Output is correct |
3 |
Correct |
0 ms |
384 KB |
Output is correct |
4 |
Correct |
0 ms |
384 KB |
Output is correct |
5 |
Correct |
0 ms |
384 KB |
Output is correct |
6 |
Correct |
1 ms |
384 KB |
Output is correct |
7 |
Correct |
0 ms |
384 KB |
Output is correct |
8 |
Correct |
0 ms |
384 KB |
Output is correct |
9 |
Correct |
0 ms |
384 KB |
Output is correct |
10 |
Correct |
0 ms |
384 KB |
Output is correct |
11 |
Correct |
1 ms |
384 KB |
Output is correct |
12 |
Incorrect |
0 ms |
384 KB |
Output isn't correct |
13 |
Halted |
0 ms |
0 KB |
- |