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 MEM 111111
#define sanic ios_base::sync_with_stdio(0)
#define x first
#define y second
#define pf push_front
#define pb push_back
#define all(v) v.begin(), v.end()
#define sz size()
using namespace std;
typedef long long ll;
typedef pair<ll, ll> pi;
const ll MOD = 1e9+7;
const ll INF = 2e14+7;
ll mul(ll a, ll b){
return ((a*b)%MOD+MOD)%MOD;
}
ll add(ll a, ll b){
return ((a+b)%MOD+MOD)%MOD;
}
ll t,n,m,q,k;
ll dp[MEM];
ll ans;
vector<ll> g[MEM];
string s;
ll DP(ll c, ll p){
ll& ret = dp[c];
if(ret!=-1) return ret;
ll dsum=0, dmx=0;
for(ll nt:g[c]){
if(nt==p) continue;
ll o=DP(nt, c);
dsum += o;
dmx = max(o, dmx);
}
ll ct=(ll)(s[c-1]-'0');
ret = max(dsum-ct, ct);
ans = max({ans, dmx+ct, dsum-ct});
//cout << c << ' ' << ret << ' ' << ans << ' ' << dsum << ' ' << dmx << '\n';
return ret;
}
signed main(){
sanic; cin.tie(0); cout.tie(0);
memset(dp, -1, sizeof(dp));
cin >> n;
for(int i=1; i<n; i++){
ll q1,q2;
cin >> q1 >> q2;
g[q1].pb(q2);
g[q2].pb(q1);
}
cin >> s;
DP(1, -1);
cout << ans;
}
/*
6
2 3
4 3
1 3
3 5
6 2
110011
*/
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |