이 제출은 이전 버전의 oj.uz에서 채점하였습니다. 현재는 제출 당시와는 다른 서버에서 채점을 하기 때문에, 다시 제출하면 결과가 달라질 수도 있습니다.
#include <bits/stdc++.h>
#define mp make_pair
#define X first
#define Y second
using namespace std;
typedef long long ll;
typedef pair <int, int> ii;
const int N = 2e5 + 1;
int n;
vector <int> adj[N];
string s;
int a[N], dp[N][2];
void readInput(){
cin >> n;
for(int i = 1; i < n; i++){
int u, v;
cin >> u >> v;
adj[u].push_back(v);
adj[v].push_back(u);
}
cin >> s;
}
void dfs(int u, int p){
dp[u][0] = -a[u];
dp[u][1] = a[u];
int maxVal = 0;
for(int v : adj[u]){
if (v == p)
continue;
dfs(v, u);
dp[u][0] += max(a[v], dp[v][0]);
maxVal = max(maxVal, dp[v][0]);
maxVal = max(maxVal, a[v]);
}
dp[u][1] += maxVal;
dp[u][0] = max(dp[u][0], dp[u][1] - 2 * a[u]);
}
void solve(){
for(int i = 1; i <= n; i++)
a[i] = s[i - 1] - '0';
dfs(1, -1);
int res = 0;
for(int i = 1; i <= n; i++){
//cerr << i << ' ' << dp[i][0] << ' ' << dp[i][1] << endl;
res = max(res, dp[i][0]);
res = max(res, dp[i][1]);
}
cout << res;
}
int main(){
ios_base::sync_with_stdio(0);
cin.tie(0);
readInput();
solve();
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... |