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 ans=1;
vector<int> res;
vector<vector<int>> adj;
string s;
void dfs(int u=0, int p=-1) {
int sum=0, mx=0, path=0;
for(int pp : adj[u]) {
if(pp==p) continue;
dfs(pp, u);
if(res[pp]>0) sum+=res[pp], mx=max(mx, res[pp]), path++;}
res[u]=sum;
if(path==0) {res[u]=s[u]-'0'; return;}
if(s[u]=='1') res[u]--, ans=max({ans, sum-2*(path>1)+1, mx+1});
else ans=max(ans, sum);
res[u]=max(1, res[u]);
}
int main() {
ios::sync_with_stdio(0); cin.tie(0); cout.tie(0);
int n; cin >> n;
res.resize(n); adj.resize(n);
while(--n) {
int u, v; cin >> u >> v; --u; --v;
adj[u].push_back(v);
adj[v].push_back(u);}
cin >> s;
dfs();
cout << ans << '\n';
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... |