이 제출은 이전 버전의 oj.uz에서 채점하였습니다. 현재는 제출 당시와는 다른 서버에서 채점을 하기 때문에, 다시 제출하면 결과가 달라질 수도 있습니다.
#include <bits/stdc++.h>
using namespace std;
#define ll long long
#define pb push_back
#define pii pair<int,int>
#define all(x) x.begin(), x.end()
const int N = 200000;
vector <int> adj[N];
int sz[N], dp[N], ans[N];
string s;
void dfs(int v, int pa) {
dp[v] = s[v] == '1';
int tot = 0, mx = 0;
for (int u : adj[v]) if (u != pa) {
dfs(u, v);
tot += dp[u], mx = max(mx, dp[u]);
}
dp[v] = max(dp[v], tot - (s[v] == '1'));
ans[v] = max(dp[v], mx + (s[v] == '1'));
}
int main() {
ios::sync_with_stdio(false), cin.tie(0);
int n;
cin >> n;
for (int i = 0, u, v; i < n - 1; ++i) {
cin >> u >> v, --u, --v;
adj[u].pb(v), adj[v].pb(u);
}
cin >> s;
dfs(0, -1);
cout << *max_element(ans, ans + n) << '\n';
}
| # | Verdict | Execution time | Memory | Grader output |
|---|
| Fetching results... |
| # | Verdict | Execution time | Memory | Grader output |
|---|
| Fetching results... |
| # | Verdict | Execution time | Memory | Grader output |
|---|
| Fetching results... |