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 <iostream>
#include <vector>
#include <algorithm>
#include <string>
#include <set>
#include <map>
#include <unordered_map>
#include <unordered_set>
using namespace std;
#define x first
#define y second
const int maxn = 2e5 + 1;
int dp[maxn][3];
vector<vector<int>> g;
string s;
void dfs(int v, int p) {
for (int u : g[v])
if (u != p)
dfs(u, v);
dp[v][0] = 0;
if (s[v] == '1')
dp[v][1] = 1;
int sm = 0;
for (int u : g[v]) {
if (u != p) {
if (s[v] == '1')
dp[v][2] = max(dp[v][2], dp[u][1] + 1);
dp[v][2] = max(dp[v][2], dp[u][2] - (s[v] == '1'));
sm += max(dp[u][1], 0);
}
}
dp[v][1] = max(dp[v][1], sm - (s[v] == '1'));
}
signed main() {
ios::sync_with_stdio(false);
cin.tie(0);
int n;
cin >> n;
g.resize(n);
for (int i = 0; i < n; i++)
dp[i][0] = dp[i][1] = dp[i][2] = -1e9;
for (int i = 0; i < n - 1; i++) {
int u, v;
cin >> u >> v;
u--;
v--;
g[u].push_back(v);
g[v].push_back(u);
}
cin >> s;
dfs(0, -1);
int ans = 0;
for (int i = 0; i < n; i++) {
ans = max(ans, dp[i][1]);
ans = max(ans, dp[i][2]);
}
cout << ans;
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... |