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, dp1[nx], dp2[nx];
vector<int> d[nx];
string s;
void dfs(int u, int p)
{
for (auto v:d[u])
{
if (v==p) continue;
dfs(v, u);
if (s[u-1]=='1') dp1[u]+=dp1[v], dp2[u]=max(dp2[u], dp1[v]+1);
else dp1[u]+=max(dp1[v], dp2[v]-2), dp2[u]=max(dp2[u], dp2[v]);
}
if (s[u-1]=='1') dp1[u]=max(1, dp1[u]-1), dp2[u]=max(dp2[u], 1);
}
int main()
{
cin.tie(NULL)->sync_with_stdio(false);
cin>>n;
for (int i=1; i<n; i++) cin>>u>>v, d[u].push_back(v), d[v].push_back(u);
cin>>s;
dfs(1, 1);
cout<<max({dp1[1], dp2[1], 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... |