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 int long long
const int MAX_N = 2*1e5;
int n, dp[MAX_N][2];
string s;
vector<int> adj[MAX_N];
void find_dp(int id, int anc){
dp[id][0]= 0;
dp[id][1] = 0;
bool gen = s[id] == '1';
int s1=0;
int best1= 0;
int best0= 0;
for(auto e: adj[id]){
if(e!=anc){
find_dp(e, id);
s1 += dp[e][1];
best1 = max(best1, dp[e][1]);
best0 = max(best0, dp[e][0]);
}
}
if(!gen){
dp[id][0] = max(max(s1, best0), 0LL);
dp[id][1] = max(s1, 0LL);
}
else{
dp[id][0] = max(s1-1, max(best1+1, best0));
dp[id][1] = max(s1-1, 1LL);
}
//cout<<id<<" "<<dp[id][0]<<" "<<dp[id][1]<<endl;
}
signed main(){
cin>>n;
for(int i = 0; i<n-1; i++){
int a, b;
cin>>a>>b;
a--;b--;
adj[a].push_back(b);
adj[b].push_back(a);
}
cin>>s;
find_dp(0, -1);
cout<<dp[0][0]<<endl;
}
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |