#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 tot=0, mx=0, path=0;
if(to_p>0) tot+=to_p, mx=max(mx, to_p), path++;
for(int pp : adj[u]) {
if(pp==p) continue;
if(res[pp]>0) tot+=res[pp], mx=max(mx, res[pp]), path++;
if(s[u]=='1') dfs2(pp, u, 1);
else dfs2(pp, u, to_p+res[u]-(s[pp]=='1' ? 1 : res[pp]));
}
if(path==1) ans=max(ans, tot+(s[u]=='1'));
else ans=max({ans, tot-(s[u]=='1'), mx+1});
}
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 |
1 |
Correct |
0 ms |
212 KB |
Output is correct |
2 |
Correct |
0 ms |
212 KB |
Output is correct |
3 |
Correct |
0 ms |
212 KB |
Output is correct |
4 |
Correct |
0 ms |
212 KB |
Output is correct |
5 |
Incorrect |
0 ms |
212 KB |
Output isn't correct |
6 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
212 KB |
Output is correct |
2 |
Correct |
0 ms |
212 KB |
Output is correct |
3 |
Correct |
0 ms |
212 KB |
Output is correct |
4 |
Correct |
0 ms |
212 KB |
Output is correct |
5 |
Incorrect |
0 ms |
212 KB |
Output isn't correct |
6 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
212 KB |
Output is correct |
2 |
Correct |
0 ms |
212 KB |
Output is correct |
3 |
Correct |
0 ms |
212 KB |
Output is correct |
4 |
Correct |
0 ms |
212 KB |
Output is correct |
5 |
Incorrect |
0 ms |
212 KB |
Output isn't correct |
6 |
Halted |
0 ms |
0 KB |
- |