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 all(x) x.begin(), x.end()
using namespace std;
using ll = long long;
using pii = pair<int, int>;
const int MN = 2e5 + 5;
int N;
string s;
vector<int> g[MN];
int dp[MN], ans;
void dfs(int u, int p) {
int mx = 0;
for (int v : g[u]) if (v != p) {
dfs(v, u);
dp[u] += dp[v];
mx = max(mx, dp[v]);
}
if (s[u - 1] == '1') {
dp[u] = max(dp[u] - 1, 1);
ans = max(ans, mx + 1);
}
ans = max(ans, dp[u]);
}
int 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;
g[u].push_back(v);
g[v].push_back(u);
}
cin >> s;
dfs(1, -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... |