이 제출은 이전 버전의 oj.uz에서 채점하였습니다. 현재는 제출 당시와는 다른 서버에서 채점을 하기 때문에, 다시 제출하면 결과가 달라질 수도 있습니다.
#include <bits/stdc++.h>
using namespace std;
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>
using namespace __gnu_pbds;
template<class T> using oset =
tree<T, null_type, less<T>, rb_tree_tag, tree_order_statistics_node_update>;
#define ll long long
#define ld long double
#define ar array
#define vi vector<int>
#define vii vector<vector<int>>
#define pii pair<int, int>
#define pb push_back
#define all(x) x.begin(), x.end()
#define f first
#define s second
#define endl "\n"
const int MOD = 1e9+7;
const int inf = 1e9;
const ll linf = 1e18;
const int d4i[4]={-1, 0, 1, 0}, d4j[4]={0, 1, 0, -1};
const int d8i[8]={-1, -1, 0, 1, 1, 1, 0, -1}, d8j[8]={0, 1, 1, 1, 0, -1, -1, -1};
// -------------------------------------------------- Main Code --------------------------------------------------
const int N = 1e5+5;
int n, initial[N];
vi g[N];
int memo[N][2][2];
int sol(int idx, int sum, vector<array<int, 2>> &child, int reqSum, vii &dp) {
if (idx == child.size()) {
if (sum == reqSum) return 0;
return inf;
}
if (dp[idx][sum] != -1) return dp[idx][sum];
int ans = sol(idx+1, sum, child, reqSum, dp) + child[idx][0];
ans = min(ans, sol(idx+1, (sum+1)%2, child, reqSum, dp) + child[idx][1]);
return dp[idx][sum] = ans;
}
int dfs(int src, int par, int lastSwitched, int currSwitched) {
if (memo[src][lastSwitched][currSwitched] != -1) return memo[src][lastSwitched][currSwitched];
vector<array<int, 2>> child; // child[i][0] = childNotSwitched else childSwitched
for (auto ch : g[src]) {
if (ch == par) continue;
child.pb({dfs(ch, src, currSwitched, false), 1+dfs(ch, src, currSwitched, true)});
}
int x = (initial[src] + lastSwitched + currSwitched)%2;
vii dp(child.size()+1, vi(2, -1));
return memo[src][lastSwitched][currSwitched] = sol(0, 0, child, x, dp);
}
void sol() {
cin >> n;
for (int i = 0; i < n-1; i++) {
int a, b; cin >> a >> b;
--a; --b;
g[a].pb(b);
g[b].pb(a);
}
for (int i = 0; i < n; i++) cin >> initial[i];
memset(memo, -1, sizeof memo);
int ans = min(dfs(0, -1, 0, 0), 1+dfs(0, -1, 0, 1));
if (ans > n) cout << "impossible" << endl;
else cout << ans << endl;
}
int main () {
ios_base::sync_with_stdio(false);
cin.tie(NULL);
int t = 1;
// cin >> t;
while (t--) {
sol();
}
return 0;
}
컴파일 시 표준 에러 (stderr) 메시지
xanadu.cpp: In function 'int sol(int, int, std::vector<std::array<int, 2> >&, int, std::vector<std::vector<int> >&)':
xanadu.cpp:40:13: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<std::array<int, 2> >::size_type' {aka 'long unsigned int'} [-Wsign-compare]
40 | if (idx == child.size()) {
| ~~~~^~~~~~~~~~~~~~~
# | 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... |