이 제출은 이전 버전의 oj.uz에서 채점하였습니다. 현재는 제출 당시와는 다른 서버에서 채점을 하기 때문에, 다시 제출하면 결과가 달라질 수도 있습니다.
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef long double ld;
typedef pair<ll,ll> pl;
typedef pair<int,int> pii;
typedef tuple<int,int,int> tt;
#define all(a) a.begin(), a.end()
#define filter(a) a.erase(unique(all(a)), a.end())
const int mn = 2e5 + 5;
int dp[2][mn], power[mn];
vector<int> adj[mn];
void dfs (int u, int p) {
for (int v : adj[u]) {
if (v == p) continue;
dfs(v, u);
dp[0][u] += max(0, dp[0][v]);
dp[1][u] = max(dp[1][u], dp[0][v]);
}
dp[1][u] += power[u];
dp[0][u] -= power[u];
if (power[u])
dp[0][u] = max(dp[0][u], 1);
}
int main()
{
ios::sync_with_stdio(0);
cin.tie(0);
int n; cin >> n;
for (int i = 1; i < n; i++) {
int a, b; cin >> a >> b;
adj[a].push_back(b);
adj[b].push_back(a);
}
for (int i = 1; i <= n; i++) {
char c; cin >> c;
power[i] = c - '0';
}
dfs(1, 1);
int ans = 0;
for (int i = 1; i <= n; i++)
for (int j = 0; j < 2; j++)
ans = max(ans, dp[j][i]);
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... |