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;
#define int long long
#define endl '\n'
int n;
int cnt;
string s;
vector<int> adj[100005];
bool pw[100005];
int dp[100005][2];
void dfs(int v, int p = -1) {
if (s[v] == '1') dp[v][0] = 1;
int tot = 0, mx = 0;
for (int u : adj[v]) {
if (u == p) continue;
dfs(u, v);
tot += dp[u][0];
mx = max(mx, dp[u][0]);
}
dp[v][0] = max(tot - dp[v][0], dp[v][0]);
if (s[v] == '1') dp[v][1] = mx + 1;
}
signed main() {
ios_base::sync_with_stdio(0);
cin.tie(0);
cin >> n;
for (int i = 1; i < n; i++) {
int u, v; cin >> u >> v;
u--; v--;
adj[u].push_back(v);
adj[v].push_back(u);
}
cin >> s;
for (int i = 0; i < n; i++) {
if (s[i] == '1') cnt++;
}
int ans = 0;
dfs(0);
for (int i = 0; i < n; i++) {
ans = max({ans, dp[i][0], dp[i][1]});
}
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... |