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 fi first
#define se second
using namespace std;
const int N=2e5;
vector<int> e[N+10];
int dp[N+10][2];
int t[N+10];
void dfs(int x,int p)
{
for(auto v:e[x])
{
if(v!=p)
dfs(v,x);
}
int s1=0;
for(auto v:e[x])
{
if(v!=p)
s1+=dp[v][1];
}
// dp[0]
dp[x][0]=t[x];
for(auto v:e[x])
{
if(v!=p)
dp[x][0]=max({dp[x][0],dp[v][0],dp[v][1]+t[x]});
}
dp[x][0]=max(dp[x][0],s1-t[x]);
// dp[1]
dp[x][1]=t[x];
for(auto v:e[x])
{
if(v!=p)
dp[x][1]=max(dp[x][1],dp[v][1]-t[x]);
}
dp[x][1]=max(dp[x][1],s1-t[x]);
return;
}
int main()
{
ios_base::sync_with_stdio(false);
cin.tie(NULL);
cout.tie(NULL);
int n;
cin>>n;
for(int i=1;i<n;i++)
{
int a,b;
cin>>a>>b;
e[a].push_back(b);
e[b].push_back(a);
}
for(int i=1;i<=n;i++)
{
char c;
cin>>c;
t[i]=c-'0';
}
dfs(1,0);
cout<<dp[1][0]<<"\n";
return 0;
}
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |