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 <cstdio>
#include <cstring>
#include <cassert>
#include <string>
#include <deque>
#include <vector>
#include <map>
#include <queue>
#include <algorithm>
#include <iostream>
#include <utility>
using namespace std;
using ll = long long;
using ld = long double;
#define ShinLena cin.tie(nullptr)->sync_with_stdio(false)
#define N 200005
#define ALL(x) x.begin(), x.end()
int n, ans, f[N], h[N], pwr[N];
vector<int> g[N];
char s[N];
void dfs(int u, int p)
{
for (auto v:g[u]) if(v-p)
{
dfs(v, u);
if(s[u]=='1')f[u]+=f[v],h[u]=max(h[u],f[v]+1);
else f[u]+=max(f[v],h[v]-2),h[u]=max(h[u],f[v]);
}
if(s[u]=='1')f[u]=max(1,f[u]-1),h[u]=max(h[u],1);
ans=max({ans,f[u],h[u]});
}
int main()
{
ShinLena;
cin >> n;
for (int i = 1, u, v; i < n; ++i) cin >> u >> v, g[u].push_back(v), g[v].push_back(u);
cin >> (s+1);
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... |