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>
#define se second
#define fs first
#define mp make_pair
#define pb push_back
#define ll long long
#define ii pair<ll,ll>
#define ld long double
#define SZ(v) (int)v.size()
#define ALL(v) v.begin(), v.end()
#define bit(msk, i) ((msk >> i) & 1)
#define iter(id, v) for(auto id : v)
#define rep(i,m,n) for(int i=(m); i<=(n); i++)
#define reb(i,m,n) for(int i=(m); i>=(n); i--)
using namespace std;
mt19937_64 rd(chrono :: steady_clock :: now().time_since_epoch().count());
ll Rand(ll l, ll r) { return uniform_int_distribution<ll> (l, r)(rd); }
const int N = 2e5 + 7;
const int Mod = 1e9 + 7;
const int szBL = 240;
const ll INF = 1e18;
const int BASE = 137;
int n;
vector<int> ke[N];
int dp[N][3];
int a[N];
void dfs (int u, int p ) {
iter (&v, ke[u]) {
if (v == p) continue;
dfs(v, u);
}
dp[u][0] = 0;
{
int sum = 0;
iter (&v, ke[u]) {
if (v == p) continue;
sum += dp[v][1];
}
dp[u][1] = max(a[u], sum - a[u]);
}
int sum = 0;
iter (&v, ke[u]) {
if (v == p) continue;
sum += dp[v][1];
dp[u][2] = max(dp[u][2], max(dp[v][1] + a[u], dp[v][2] - a[u]));
}
dp[u][2] = max(dp[u][2], sum - a[u]);
}
void solution (){
cin >> n;
rep (i, 1, n - 1) {
int u, v;
cin >> u >> v;
ke[u].push_back(v);
ke[v].push_back(u);\
}
string s;
cin >> s;
rep (i, 1, n) {
a[i] = s[i - 1] - '0';
}
dfs(1, 0);
int res = 0;
rep (i, 1, n)
rep (j, 0, 2) res = max(res, dp[i][j]);
cout << res<<"\n";
}
#define file(name) freopen(name".inp", "r", stdin); \
freopen(name".out", "w", stdout);
/*
*/
int main () {
// file("c");
ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0);
ll num_Test = 1;
// cin >> num_Test;
while(num_Test--)
solution();
}
/*
no bug challenge +2
*/
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |