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 <map>
using namespace std;
int n;
vector<vector<int>> g;
string s;
vector<map<int, int>> dp;
vector<int> dp_sum;
int f(int a, int b) {
return (s[a] == '1' ? max(1, dp_sum[a] - dp[a][b] - 1) : dp_sum[a] - dp[a][b]);
}
int dfs1(int u, int par) {
dp[u][par] = 0;
for (int v : g[u]) {
if (v != par) {
dp[u][v] = dfs1(v, u);
dp_sum[u] += dp[u][v];
}
}
return f(u, par);
}
void dfs2(int u, int par) {
dp[u][par] = f(par, u);
dp_sum[u] += dp[u][par];
for (int v : g[u]) {
if (v != par) dfs2(v, u);
}
}
int solve() {
dp.resize(n), dp_sum.resize(n, 0);
for (int v : g[0]) {
dp[0][v] += dfs1(v, 0);
dp_sum[0] += dp[0][v];
}
for (int v : g[0]) {
dfs2(v, 0);
}
int ans = 0;
for (int u = 0; u < n; u++) {
for (int v : g[u]) {
ans = max(ans, f(u, v) + f(v, u));
}
}
return ans;
}
int main() {
ios_base::sync_with_stdio(false);
cin.tie(NULL);
cin >> n;
g.resize(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;
cout << max(1, solve()) << '\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... |