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;
template<class Fun>
class y_combinator_result {
Fun fun_;
public:
template<class T>
explicit y_combinator_result(T &&fun): fun_(std::forward<T>(fun)) {}
template<class ...Args>
decltype(auto) operator()(Args &&...args) {
return fun_(std::ref(*this), std::forward<Args>(args)...);
}
};
template<class Fun>
decltype(auto) y_combinator(Fun &&fun) {
return y_combinator_result<std::decay_t<Fun>>(std::forward<Fun>(fun));
}
int main()
{
ios_base::sync_with_stdio(false);
cin.tie(NULL);
int n;
cin >> n;
vector<vector<int>> g(n);
for (int i = 0; i < n-1; i++) {
int u,v;
cin >> u >> v;
u--,v--;
g[u].push_back(v);
g[v].push_back(u);
}
string gen;
cin >> gen;
vector<int> dp(n);
int ans = 0;
y_combinator([&](auto self, int s, int p)->void {
for (auto& u : g[s]){
if (u==p) continue;
self(u,s);
dp[s] += dp[u];
if (gen[s] == '1')
ans = max(ans,dp[u] + 1);
}
if (gen[s] == '1')
dp[s] = max(dp[s]-1,1);
ans = max(dp[s],ans);
})(0,-1);
cout << ans << '\n';
}
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |