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 ll long long
#define f first
#define s second
#define pb push_back
using namespace std;
ll dp[300005][2],n,c[300005];
vector<ll>v[300005];
void go(ll x,ll par){
ll mx0 = 0,mx1 = 0,sum0 = 0,sum1 = 0;
for(int i=0; i<v[x].size(); i++){
int to = v[x][i];
if(to == par)continue;
go(to , x);
mx0 = max(mx0 , dp[to][0]);
sum0 += dp[to][0];
sum1 += dp[to][1];
mx1 = max(mx1 , dp[to][1]);
}
mx0 = max(mx0 , c[x] + mx1);
dp[x][0] = mx0;
dp[x][1] = max(c[x] , sum1 - c[x]);
}
int main(){
ios::sync_with_stdio(false);
cin >> n;
for(int i=1; i<n; i++){
ll x,y;
cin >> x >> y;
v[x].pb(y);
v[y].pb(x);
}
for(int i=1; i<=n; i++){
char x;
cin >> x;
c[i] = (x == '1');
}
go(1 , 0);
cout << max(dp[1][0] , dp[1][1]) << '\n';
return 0;
}
Compilation message (stderr)
power.cpp: In function 'void go(long long int, long long int)':
power.cpp:11:19: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<long long int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
11 | for(int i=0; i<v[x].size(); i++){
| ~^~~~~~~~~~~~
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |