# | Time | Username | Problem | Language | Result | Execution time | Memory |
---|---|---|---|---|---|---|---|
298516 | BeanZ | Power Plant (JOI20_power) | C++14 | 4 ms | 4992 KiB |
This submission is migrated from previous version of oj.uz, which used different machine for grading. This submission may have different result if resubmitted.
#include <bits/stdc++.h>
using namespace std;
#define ll long long
#define endl '\n'
const int N = 2e5 + 5;
ll dp[N], cur[N];
vector<ll> node[N];
string s;
ll ans = 0;
void dfs(ll u, ll p){
vector<ll> val;
for (auto j : node[u]){
if (j == p) continue;
dfs(j, u);
val.push_back(dp[j]);
}
if (val.size()){
sort(val.begin(), val.end(), greater<ll>());
ans = max(ans, val[0] + (s[u] == '1'));
ll sum = 0;
for (auto j : val) sum += j;
ans = max(ans, sum - (s[u] == '1'));
dp[u] = max((ll)(s[u] == '1'), sum - (s[u] == '1'));
} else {
dp[u] = (s[u] == '1');
}
}
int main(){
ios_base::sync_with_stdio(false);
cin.tie(0);
if (fopen("A.INP", "r")){
freopen("A.INP", "r", stdin);
freopen("A.OUT", "w", stdout);
}
ll n;
cin >> n;
for (int i = 1; i < n; i++){
ll u, v;
cin >> u >> v;
node[u].push_back(v);
node[v].push_back(u);
}
cin >> s;
s = " " + s;
dfs(1, 1);
cout << ans;
}
/*
*/
Compilation message (stderr)
# | Verdict | Execution time | Memory | Grader output |
---|---|---|---|---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|---|---|---|---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|---|---|---|---|
Fetching results... |