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>
typedef long long ll;
using namespace std;
string s;
vector<int> graph[200001];
int dp[200001], ans = 1;
void dfs(int node = 1, int parent = 0) {
int mx = 0;
for (int i : graph[node]) if (i != parent) {
dfs(i, node);
dp[node] += dp[i];
mx = max(mx, dp[i]);
}
dp[node] = max(s[node - 1] - '0', dp[node] - s[node - 1] + '0');
ans = max(ans, max(mx + s[node - 1] - '0', dp[node]));
}
int main() {
cin.tie(0)->sync_with_stdio(0);
int n;
cin >> n;
for (int i = 1; i < n; i++) {
int a, b;
cin >> a >> b;
graph[a].push_back(b);
graph[b].push_back(a);
}
cin >> s;
dfs();
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... |