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 MAX_N = 2e5 + 5;
vector<int> G[MAX_N];
int dp[MAX_N] = {};
string S;
int ans;
void DFS(int u, int p) {
if (S[u] == '0') {
for (int v : G[u]) {
if (v != p) {
DFS(v, u);
dp[u] += dp[v];
}
}
ans = max(ans, dp[u]);
} else {
for (int v : G[u]) {
if (v != p) {
DFS(v, u);
ans = max(ans, dp[v] + 1);
dp[u] += dp[v];
}
}
if (dp[u] > 1) {
dp[u] -= 1;
}
if (dp[u] == 0) {
dp[u] += 1;
}
ans = max(ans, dp[u]);
}
}
int main() {
cin.tie(nullptr)->sync_with_stdio(false);
int N;
cin >> N;
for (int i = 0; i < N - 1; i++) {
int A, B;
cin >> A >> B;
A--, B--;
G[A].push_back(B);
G[B].push_back(A);
}
cin >> S;
ans = 0;
DFS(0, -1);
cout << ans << "\n";
}
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |