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;
vector<int> haha[200001];
vector<int> dp(200001);
vector<int> col(200001);
int ans = 0;
void dfs(int a, int t) {
int sb = 0;
for(int v: haha[a]) {
if(v != t) {
dfs(v,a);
sb+=dp[v];
}
}
sb-=col[a];
dp[a] = max(col[a],sb);
}
void dude(int a, int t, int u) {
int big = u,sb = u-col[a];
for(int v: haha[a]) {
if(v != t) {
big = max(big,dp[v]);
sb+=dp[v];
}
}
if(col[a]) {
ans = max(ans,big+1);
}
for(int v: haha[a]) {
if(v != t) {
dude(v,a,max(col[a],sb-dp[v]));
}
}
}
int main()
{
ios_base::sync_with_stdio(NULL);
cin.tie(NULL);
cout.tie(NULL);
int n,a,b;
cin >> n;
for(int i = 0; i < n-1; i++) {
cin >> a >> b;
haha[a].push_back(b);
haha[b].push_back(a);
}
for(int i = 1; i <= n; i++) {
char a;
cin >> a;
if(a == '1') {
col[i] = 1;
}
}
dfs(1,-1);
dude(1,-1,0);
cout << ans;
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... |