이 제출은 이전 버전의 oj.uz에서 채점하였습니다. 현재는 제출 당시와는 다른 서버에서 채점을 하기 때문에, 다시 제출하면 결과가 달라질 수도 있습니다.
//Challenge: Accepted
#include <iostream>
#include <algorithm>
#include <vector>
#include <utility>
#include <queue>
#include <set>
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>
using namespace std;
//using namespace __gnu_pbds;
void debug() {cout << endl;}
template<class T, class ... U> void debug(T a, U ... b) {cout << a << " ", debug(b ...);}
template<class T> void pary(T l, T r) {
while (l != r) cout << *l << " ", l++;
cout << endl;
}
#define int long long
#define maxn 100005
#define pii pair<int, int>
#define ff first
#define ss second
#define io ios_base::sync_with_stdio(0);
const int inf = 1<<30;
vector<int> adj[maxn];
int a[maxn], dp[maxn][2][2];
void build(vector<int> &v, int &even, int &odd) {
int cur = even;
for (int i = 0;i < v.size();i++) {
cur += v[i];
if (i % 2 == 0) odd = min(odd, cur);
else even = min(even, cur);
}
}
void dfs(int n, int par) {
dp[n][0][0] = dp[n][0][1] = dp[n][1][0] = dp[n][1][1] = inf;
int s0 = 0, s1 = 0;
vector<int> c0, c1;
for (int v:adj[n]) {
if (v != par) {
dfs(v, n);
s0 += dp[v][0][0], s1 += dp[v][0][1];
c0.push_back(dp[v][1][0] - dp[v][0][0]);
c1.push_back(dp[v][1][1] - dp[v][0][1]);
}
}
sort(c0.begin(), c0.end()), sort(c1.begin(), c1.end());
int even = s0, odd = inf;
build(c0, even, odd);
dp[n][0][0] = min(dp[n][0][0], a[n] ? odd : even), dp[n][0][1] = min(dp[n][0][1], a[n] ? even : odd);
even = s1, odd = inf;
build(c1, even, odd);
dp[n][1][0] = min(dp[n][1][0], 1 + (a[n] ? even : odd)), dp[n][1][1] = min(dp[n][1][1], 1 + (a[n] ? odd : even));
}
signed main() {
io
int n;
cin >> n;
for (int i = 0;i < n - 1;i++) {
int u, v;
cin >> u >> v;
adj[u].push_back(v);
adj[v].push_back(u);
}
for (int i = 1;i <= n;i++) cin >> a[i];
dfs(1, 0);
int ans = min(dp[1][0][0], dp[1][1][0]);
if (ans > n) cout << "impossible\n";
else cout << ans << "\n";
}
/*
5
1 2
1 3
2 4
2 5
0 1 0 1 1
*/
컴파일 시 표준 에러 (stderr) 메시지
xanadu.cpp: In function 'void build(std::vector<long long int>&, long long int&, long long int&)':
xanadu.cpp:29:19: warning: comparison of integer expressions of different signedness: 'long long int' and 'std::vector<long long int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
29 | for (int i = 0;i < v.size();i++) {
| ~~^~~~~~~~~~
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |