#include "bits/stdc++.h"
using namespace std;
const int maxn = 200005;
vector<int> edges[maxn];
int a[maxn], dp[maxn], ans = 0;
void dfs(int x, int p){
int cur = a[x], res = 0;
dp[x] = cur;
for(auto i : edges[x]){
if(i != p){
dfs(i, x);
res += dp[i];
}
}
res -= cur;
dp[x] = max(dp[x], res);
}
void dfs1(int x, int p, int carry){
int res = carry - a[x];
for(auto i : edges[x]){
if(i != p){
res += dp[i];
}
}
ans = max(ans, res);
for(auto i : edges[x]){
if(i != p){
dfs1(i, x, max(a[x], res - dp[i]));
}
}
}
int main(int argc, char const *argv[])
{
ios::sync_with_stdio(false);
cin.tie(0);
int n;
cin >> n;
for(int i = 0; i < n - 1; ++i){
int x, y;
cin >> x >> y;
edges[x].push_back(y);
edges[y].push_back(x);
}
string s;
cin >> s;
for(int i = 1; i <= n; ++i){
a[i] = (s[i - 1] - '0');
}
dfs(1, 1);
dfs1(1, 1, 0);
cout << ans << endl;
return 0;
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
4 ms |
5228 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
4 ms |
5228 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
4 ms |
5228 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |