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 ll long long
#define ld long double
#define sp ' '
#define en '\n'
#define smin(a, b) a = min(a, b)
#define smax(a, b) a = max(a, b)
using namespace std;
const int N = 2e5 + 2;
const int inf = 2e9;
vector<int> g[N];
int dp[N][2];
int str[N];
void Dfs(int s, int e) {
dp[s][0] = str[s];
dp[s][1] = -inf;
int sum = 0;
for (int u : g[s]) if (u != e) {
Dfs(u, s);
sum += dp[u][0];
smax(dp[s][1], dp[u][1] - str[s]);
if (str[s] && dp[u][0]) smax(dp[s][1], dp[u][0] + 1);
}
smax(dp[s][0], sum - str[s]);
int fs = sum;
for (int u : g[s]) if (u != e) {
if (str[s] || sum > dp[u][0]) fs += max(0, dp[u][1] - 2 - dp[u][0]);
}
smax(dp[s][0], fs - str[s]);
}
int main() {
ios::sync_with_stdio(false);
cin.tie(0);
cout.tie(0);
int n; cin >> n;
for (int i = 0; i < n - 1; i++) {
int u, v;
cin >> u >> v;
u -= 1;
v -= 1;
g[u].push_back(v);
g[v].push_back(u);
}
string sr;
cin >> sr;
for (int i = 0; i < n; i++) str[i] = sr[i] - '0';
Dfs(0, -1);
cout << max(dp[0][0], dp[0][1]) << en;
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... |