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;
const int nx=2e5+5;
int n, u, v, dp[2][nx];
vector<int> d[nx];
string s;
void dfs(int u, int p)
{
int cnt=0, cs=1;
for (auto v:d[u])
{
if (v==p) continue;
dfs(v, u);
dp[0][u]+=dp[0][v];
if (dp[0][v]!=0) cnt++;
if (dp[1][v]>dp[1][u]) dp[1][u]=dp[1][v], cs=0;
if (s[u-1]&&dp[0][v]+1>=dp[1][u]) dp[1][u]=dp[0][v]+1, cs=1;
}
if (s[u-1]=='1')
{
if (cnt>=2) dp[0][u]--;
else dp[0][u]=1;
if (!cs) dp[1][u]--;
}
//cout<<u<<' '<<dp[0][u]<<' '<<dp[1][u]<<'\n';
}
int main()
{
cin.tie(NULL)->sync_with_stdio(false);
cin>>n;
for (int i=0; i<n-1; i++) cin>>u>>v, d[u].push_back(v), d[v].push_back(u);
cin>>s;
dfs(1, 1);
cout<<max(dp[0][1], dp[1][1]);
}
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |