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 endl '\n'
#define st first
#define nd second
#define pb push_back
#define sz(x) (int)(x).size()
#define all(x) (x).begin(), (x).end()
#define ll long long
ll mod=1000000007;
int inf=1000000007;
ll infl=1000000000000000007;
const int N=200007;
vector<int>G[N];
string s;
int DP[N];
int ans;
void dfs(int v,int o)
{
if(s[v]=='1') DP[v]=-1;
for(auto u:G[v])
{
if(u==o) continue;
dfs(u,v);
DP[v]+=DP[u];
}
if(s[v]=='1') DP[v]=max(DP[v],1);
ans=max(ans,DP[v]);
if(s[o]=='1') ans=max(ans,DP[v]+1);
}
int main()
{
ios_base::sync_with_stdio(0);
cin.tie(0);
cout.tie(0);
int n;
cin>>n;
for(int i=1;i<n;i++)
{
int a,b;
cin>>a>>b;
G[a].pb(b);
G[b].pb(a);
}
cin>>s;
s='#'+s;
dfs(1,0);
cout<<ans;
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... |