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 all(x) x.begin(), x.end()
#define ar array
#define pb push_back
#define ln '\n'
#define int long long
using i64 = long long;
template <class F, class _S>
bool chmin(F &u, const _S &v){
bool flag = false;
if ( u > v ){
u = v; flag |= true;
}
return flag;
}
template <class F, class _S>
bool chmax(F &u, const _S &v){
bool flag = false;
if ( u < v ){
u = v; flag |= true;
}
return flag;
}
const int inf = 1e9;
signed main(){
ios_base::sync_with_stdio(false);
cin.tie(nullptr);
int n; cin >> n;
vector <vector<int>> adj(n);
for ( int i = 1; i < n; i++ ){
int u, v; cin >> u >> v;
--u, --v;
adj[u].pb(v);
adj[v].pb(u);
}
string s; cin >> s;
int cnt = 0;
for ( auto &u: s ){
u -= '0';
cnt += 1;
}
vector <int> dp(n, -inf), d(n);
int ans = min(2LL, cnt);
auto dfs = [&](auto dfs, int u, int p) -> void{
vector <int> child;
for ( auto &v: adj[u] ){
if ( v != p ){
d[v] = d[u] + s[u];
dfs(dfs, v, u);
child.pb(dp[v]);
if ( s[u] > 0 ){
chmax(ans, s[u] * 2 + dp[v] + d[u]);
}
}
}
if ( s[u] > 0 ){
dp[u] = s[u] - d[u];
}
sort(all(child), greater <int> ());
int cnt = 0;
for ( int i = 0; i < child.size(); i++ ){
cnt += child[i];
chmax(dp[u], cnt + d[u] * (i + 1) + s[u] * i - d[u]);
}
chmax(ans, dp[u]);
};
dfs(dfs, 0, -1);
cout << ans << ln;
cout << '\n';
}
Compilation message (stderr)
power.cpp: In lambda function:
power.cpp:89:28: warning: comparison of integer expressions of different signedness: 'long long int' and 'std::vector<long long int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
89 | for ( int i = 0; i < child.size(); i++ ){
| ~~^~~~~~~~~~~~~~
power.cpp: In instantiation of 'main()::<lambda(auto:23, long long int, long long int)> [with auto:23 = main()::<lambda(auto:23, long long int, long long int)>]':
power.cpp:98:19: required from here
power.cpp:89:28: warning: comparison of integer expressions of different signedness: 'long long int' and 'std::vector<long long int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |