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;
int dp[200005], ok[200005], n, ans;
vector<int> adj[200005];
void dfs(int u, int p)
{
int sum=0, mx=0;
for (int v:adj[u])
{
if (v==p)
continue;
dfs(v, u);
sum+=dp[v];
mx=max(mx, dp[v]);
}
ans=max(ans, dp[u]=max(ok[u], sum-ok[u]));
if (ok[u])
ans=max(ans, mx+1);
}
int main()
{
ios::sync_with_stdio(0);
cin.tie(0);
cout.tie(0);
cin >> n;
for (int i=1; i<n; i++)
{
int u, v;
cin >> u >> v;
adj[u].push_back(v);
adj[v].push_back(u);
}
for (int i=1; i<=n; i++)
{
char x;
cin >> x;
ok[i]=x-'0';
}
dfs(1, 1);
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... |