이 제출은 이전 버전의 oj.uz에서 채점하였습니다. 현재는 제출 당시와는 다른 서버에서 채점을 하기 때문에, 다시 제출하면 결과가 달라질 수도 있습니다.
#include <bits/stdc++.h>
#define pb push_back
using namespace std;
const int maxN = 2e5 + 5;
int n, ans;
bool has[maxN];
int dp[maxN];
vector<int> adj[maxN];
void dfs(int u, int p = 1) {
for (auto v : adj[u]) {
if (v != p) {
dfs(v, u);
dp[u] += dp[v];
ans = max(ans, dp[v] + has[u]);
}
}
if (has[u])
dp[u] = max(dp[u] - 1, 1);
dp[u] = max(dp[u], 0);
ans = max(ans, dp[u]);
}
signed main() {
ios_base::sync_with_stdio(0); cin.tie(0);
cin >> n;
for (int i = 1; i < n; i++) {
int u, v; cin >> u >> v;
adj[u].pb(v); adj[v].pb(u);
}
for (int i = 1; i <= n; i++) {
char c; cin >> c;
has[i] = (c == '1');
}
dfs(1);
cout << ans;
}
| # | Verdict | Execution time | Memory | Grader output |
|---|
| Fetching results... |
| # | Verdict | Execution time | Memory | Grader output |
|---|
| Fetching results... |
| # | Verdict | Execution time | Memory | Grader output |
|---|
| Fetching results... |