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 int long long
using namespace std;
vector<vector<int> > adj;
int DP[200005];
bool type[200005];
int ans = 0;
void dfs(int c, int p) {
int sum = 0;
DP[c] = type[c] ? 1 : 0;
for(int n : adj[c]) {
if(n==p) continue;
dfs(n,c);
sum += DP[n];
ans = max(ans, DP[n] + DP[c]);
}
DP[c] = max(DP[c],sum-DP[c]);
ans = max(ans, DP[c]);
}
signed main() {
cin.sync_with_stdio(false);
cin.tie(0);
cout.tie(0);
int N;
cin >> N;
int i;
adj.resize(N);
for(i=0;i<N-1;i++) {
int a, b;
cin >>a >> b;
adj[a-1].push_back(b-1);
adj[b-1].push_back(a-1);
}
string s;
cin >> s;
for(i=0;i<N;i++) type[i] = s[i] == '1';
dfs(0, -1);
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... |