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;
typedef long long ll;
const int N = (int) 2e5 + 7;
int n;
int sol;
int dp[N];
int sum[N];
int is[N];
vector<int> g[N];
string s;
void compute(int a, int p = 0) {
dp[a] = 0;
for (auto &b : g[a]) {
if (b != p) {
dp[a] += max(dp[b], is[b]);
}
}
dp[a] -= is[a];
}
void dfs(int a, int p = 0) {
for (auto &b : g[a]) {
if (b != p) {
dfs(b, a);
}
}
compute(a, p);
}
void rer(int p, int a) {
dp[a] -= max(dp[p], is[p]);
dp[p] += max(dp[a], is[a]);
}
void solve(int a, int p = 0) {
sol = max(sol, dp[a]);
for (auto &b : g[a]) {
if (b != p) {
rer(b, a);
solve(b, a);
rer(a, b);
}
}
}
signed main() {
ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0);
///freopen("input", "r", stdin);
cin >> n;
for (int i = 1; i < n; i++) {
int a, b;
cin >> a >> b;
g[a].push_back(b);
g[b].push_back(a);
}
cin >> s;
int cnt = 0;
for (int i = 1; i <= n; i++) {
is[i] = s[i - 1] - '0';
cnt += is[i];
}
sol = min(cnt, 2);
dfs(1);
solve(1);
cout << sol << "\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... |