#include <bits/stdc++.h>
using namespace std;
const int kN = 2e5;
vector<int> g[1 + kN];
string s;
int dp[1 + kN];
void maxSelf(int &x, int y) {
if (x < y) {
x = y;
}
}
void dfs(int u, int par) {
dp[u] = s[u] - '0';
int sum = 0;
for (int v : g[u]) {
if (v != par) {
dfs(v, u);
sum += dp[v];
}
}
maxSelf(dp[u], sum - (par && (s[u] - '0')));
}
void testCase() {
int n;
cin >> n;
for (int i = 1; i < n; ++i) {
int u, v;
cin >> u >> v;
g[u].emplace_back(v);
g[v].emplace_back(u);
}
cin >> s;
s = '$' + s;
int ans = 0;
for (int v = 1; v <= n; ++v) {
if (s[v] == '1') {
dfs(v, 0);
maxSelf(ans, dp[v]);
}
}
cout << ans << '\n';
}
int main() {
ios_base::sync_with_stdio(false);
cin.tie(nullptr);
int tests = 1;
for (int tc = 0; tc < tests; ++tc) {
testCase();
}
return 0;
}
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
3 ms |
4940 KB |
Output is correct |
2 |
Correct |
2 ms |
4940 KB |
Output is correct |
3 |
Incorrect |
3 ms |
4940 KB |
Output isn't correct |
4 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
3 ms |
4940 KB |
Output is correct |
2 |
Correct |
2 ms |
4940 KB |
Output is correct |
3 |
Incorrect |
3 ms |
4940 KB |
Output isn't correct |
4 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
3 ms |
4940 KB |
Output is correct |
2 |
Correct |
2 ms |
4940 KB |
Output is correct |
3 |
Incorrect |
3 ms |
4940 KB |
Output isn't correct |
4 |
Halted |
0 ms |
0 KB |
- |