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>
#define ll long long
using namespace std;
vector<vector<int>> v;
vector<ll> dp;
string s;
ll ans = 0;
void dfs(int x, int p){
ll su = 0, mx = 0;
for (auto el : v[x]){
if (el != p){
dfs(el, x);
su += dp[el];
mx = max(dp[el], mx);
}
}
if (s[x] == '0'){
dp[x] = su;
ans = max(su, ans);
}
else{
dp[x] = max(1LL, su - 1);
ans = max(ans, max(mx + 1, su - 1));
}
}
int main(){
int n, a, b;
cin>>n;
v.resize(n + 1);
dp.assign(n + 1, 0);
for (int i = 0; i < n - 1; i++){
cin>>a>>b;
v[a].push_back(b);
v[b].push_back(a);
}
cin>>s;
s = " " + s;
dfs(1, 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... |