이 제출은 이전 버전의 oj.uz에서 채점하였습니다. 현재는 제출 당시와는 다른 서버에서 채점을 하기 때문에, 다시 제출하면 결과가 달라질 수도 있습니다.
#include <bits/stdc++.h>
using namespace std;
#define pb push_back
const int MAX_N = 2e5;
int dp[1 + MAX_N];
vector <int> gr[1 + MAX_N];
int dfs_calc (int node, int par, string &gen) {
int mx = 0;
int is_gen = gen[node] - '0';
int ans = 0;
for (int son : gr[node])
if (son != par) {
ans = max (ans, dfs_calc (son, node, gen));
dp[node] += dp[son];
mx = max (mx, dp[son]);
}
dp[node] = max (is_gen, dp[node] - is_gen);
ans = max ({ans, mx + is_gen, dp[node]});
return ans;
}
int main() {
ios::sync_with_stdio (false);
cin.tie (0); cout.tie (0);
int n;
cin >> n;
for (int i = 1; i < n; i++) {
int x, y;
cin >> x >> y;
gr[x].pb (y);
gr[y].pb (x);
}
string generators;
cin >> generators; generators = " " + generators;
cout << dfs_calc (1, 0, generators) << "\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... |