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 dfs1(int u=0, int p=-1) {
res[u]=s[u]-'0';
for(int pp : adj[u]) {
if(pp==p) continue;
dfs1(pp, u);
if(s[pp]=='1') res[u]++;
else res[u]+=res[pp];
}
}
void dfs2(int u=0, int p=-1, int to_p=0) {
int sz=adj[u].size();
if(p!=-1 || s[u]=='0') ans=max(ans, res[u]+to_p-2*(s[u]=='1')*(sz>1));
else {
int cnt=0, mn=2;
for(int pp : adj[0]) {if(res[pp]>0) cnt++, mn=min(mn, res[pp]);}
if(cnt==1) ans=max(ans, res[0]);
else ans=max(ans, res[0]-mn);
}
if(u && s[u]=='1') {
int cnt=0, mn=2, jalur;
if(to_p>0) cnt++, mn=min(mn, to_p), jalur=to_p;
for(int pp : adj[u]) {if(res[pp]>0) cnt++, mn=min(mn, res[pp]), jalur=res[pp];}
if(cnt==1) ans=max(ans, jalur+1);
else ans=max(ans, res[u]+to_p-mn);
}
for(int pp : adj[u]) {
if(pp==p) continue;
if(s[u]=='1') dfs2(pp, u, 1);
else dfs2(pp, u, to_p+res[u]-(s[pp]=='1' ? 1 : res[pp]));
}
}
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;
dfs1();
dfs2();
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... |