This submission is migrated from previous version of oj.uz, which used different machine for grading. This submission may have different result if resubmitted.
#define wiwihorz
#pragma GCC optimize("Ofast")
#pragma loop-opt(on)
#include <bits/stdc++.h>
#define rep(i, a, b) for(int i = a; i <= b; i ++)
#define rrep(i, a, b) for(int i = b; i >= a; i--)
#define all(x) x.begin(), x.end()
#define ceil(a, b) ((a + b - 1) / (b))
#define INF 1000000000ll
#define MOD 1000000007
#define eps (1e-9)
using namespace std;
#define int long long int
#define lld long double
#define pii pair<int, int>
#define random mt19937 rnd(chrono::steady_clock::now().time_since_epoch().count())
#ifdef wiwihorz
#define print(a...) cerr << "Line " << __LINE__ <<": ", kout("[" + string(#a) + "] = ", a)
void kout() {cerr << endl;}
void vprint(auto L, auto R) { cerr << *L << " \n"[next(L) == R], ++L;}
template<class T1, class ... T2> void kout(T1 a, T2 ... e) { cerr << a << " ", kout(e...); }
#else
#define print(...) 0
#define vprint(...) 0
#endif
namespace solver {
int n;
vector<int> a, dp[4];
vector<vector<int>> mp;
void init_(int _n) {
n = _n;
a.assign(n + 1, 0);
mp.assign(n + 1, vector<int>());
rep(i, 0, 3) dp[i].assign(n + 1, INF);
}
void dfs(int x, int par) {
if(a[x]) dp[2][x] = 1, dp[1][x] = 0;
else dp[3][x] = 1, dp[0][x] = 0;
int cnt = 0;
for(auto i : mp[x]) if(i != par) {
dfs(i, x), cnt++;
}
if(!cnt) return;
rep(i, 0, 3) {
int cur = 0, mn = INF, bs = (i >> 1) & 1;
int goal = ((i & 1) ^ ((i >> 1) & 1) ^ a[x]), y = 0;
for(auto j : mp[x]) if(j != par) {
if(dp[bs | 2][j] < dp[bs][j]) y ^= 1;
cur += min(dp[bs | 2][j], dp[bs][j]);
mn = min(mn, abs(dp[bs][j] - dp[bs | 2][j]));
}
if(goal ^ y) dp[i][x] = min(INF, cur + mn + ((i >> 1) & 1));
else dp[i][x] = min(cur + ((i >> 1) & 1), INF);
}
}
};
using namespace solver;
signed main() {
ios::sync_with_stdio(false), cin.tie(0);
int n; cin >> n;
init_(n);
rep(i, 1, n - 1) {
int x, y; cin >> x >> y;
mp[x].push_back(y);
mp[y].push_back(x);
}
rep(i, 1, n) cin >> a[i];
dfs(1 ,1);
int ans = min(dp[0][1], dp[2][1]);
if(ans == INF) cout << "impossible\n";
else cout << ans << "\n";
return 0;
}
Compilation message (stderr)
xanadu.cpp:3: warning: ignoring '#pragma loop ' [-Wunknown-pragmas]
3 | #pragma loop-opt(on)
|
xanadu.cpp:25:13: warning: use of 'auto' in parameter declaration only available with '-fconcepts-ts'
25 | void vprint(auto L, auto R) { cerr << *L << " \n"[next(L) == R], ++L;}
| ^~~~
xanadu.cpp:25:21: warning: use of 'auto' in parameter declaration only available with '-fconcepts-ts'
25 | void vprint(auto L, auto R) { cerr << *L << " \n"[next(L) == R], ++L;}
| ^~~~
# | 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... |