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;
const int nmax = 1e5 + 1;
int v[nmax], dp[nmax], ans;
bool f[nmax];
vector<vector<int>> adj(nmax);
void dfs(int i, int tata) {
for(auto fiu : adj[i]) {
if(fiu != tata) {
dfs(fiu, i);
}
}
int sum = 0, maxx = 0;
for(auto fiu : adj[i]) {
sum += dp[fiu];
maxx = max(maxx, dp[fiu]);
}
if(f[i]) {
ans = max({ans, maxx + 1, sum - 1});
} else {
ans = max({ans, maxx, sum});
}
dp[i] = max(f[i] * 1, sum - f[i]);
}
int32_t main() {
ios_base::sync_with_stdio(false);
cin.tie();
cout.tie();
int n;
cin >> n;
adj.resize(n + 1);
for(int i = 1; i < n; i ++) {
int a, b;
cin >> a >> b;
adj[a].push_back(b);
adj[b].push_back(a);
}
for(int i = 1; i <= n; i ++) {
char ch;
cin >> ch;
f[i] = ch - '0';
}
dfs(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... |