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 MAXN = 2e5;
int n, ans, dp[MAXN];
vector<int> g[MAXN];
string s;
void dfs(int u, int p){
if(s[u] - '0') dp[u] = -1;
for(int &v : g[u]){
if(v == p) continue;
dfs(v, u);
if(s[u] - '0') ans = max(ans, 1 + dp[v]);
dp[u] += dp[v];
}
dp[u] = max(dp[u], s[u] - '0');
ans = max(ans, dp[u]);
}
signed main(){
cin.tie(0)->sync_with_stdio(0);
cin >> n;
for(int i=1; i<n; ++i){
int u, v; cin >> u >> v;
--u, --v;
g[u].push_back(v);
g[v].push_back(u);
}
cin >> s;
dfs(0, 0);
cout << ans;
}
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |