이 제출은 이전 버전의 oj.uz에서 채점하였습니다. 현재는 제출 당시와는 다른 서버에서 채점을 하기 때문에, 다시 제출하면 결과가 달라질 수도 있습니다.
#include <bits/stdc++.h>
using namespace std;
template<class T, class U>
void ckmin(T &a, U b)
{
if (a > b) a = b;
}
template<class T, class U>
void ckmax(T &a, U b)
{
if (a < b) a = b;
}
#define MP make_pair
#define PB push_back
#define LB lower_bound
#define UB upper_bound
#define fi first
#define se second
#define FOR(i, a, b) for (auto i = (a); i < (b); i++)
#define FORD(i, a, b) for (auto i = (a) - 1; i >= (b); i--)
#define SZ(x) ((int) (x).size())
#define ALL(x) (x).begin(), (x).end()
const int MAXN = 1e5 + 13;
const int INF = 1e9 + 7;
typedef long long ll;
typedef long double ld;
typedef pair<int, int> pii;
typedef pair<ll, ll> pll;
typedef vector<int> vi;
typedef vector<ll> vl;
typedef vector<pii> vpi;
typedef vector<pll> vpl;
typedef array<array<int, 2>, 2> dparr;
int N;
vi edge[MAXN];
bitset<MAXN> flip;
dparr dp[MAXN]; //current state of vtx, whether vtx has been flipped.
int ans;
dparr comb(dparr l, dparr r)
{
dparr res;
FOR(i, 0, 2) FOR(j, 0, 2) res[i][j] = INF;
FOR(i, 0, 2)
{
FOR(j, 0, 2)
{
FOR(k, 0, 2)
{
ckmin(res[k ^ j][i], r[i][j] + l[k][i]);
}
}
}
return res;
}
void dfs(int u, int p)
{
for (int v : edge[u])
{
if (v == p) continue;
dfs(v, u);
}
FOR(i, 0, 2)
{
FOR(j, 0, 2)
{
dp[u][i][j] = INF;
}
}
dp[u][flip[u] ^ 1][1] = 1;
dp[u][flip[u]][0] = 0;
for (int v : edge[u])
{
if (v == p) continue;
dp[u] = comb(dp[u], dp[v]);
}
return;
}
int32_t main()
{
ios_base::sync_with_stdio(false); cin.tie(0);
cout << fixed << setprecision(12);
cerr << fixed << setprecision(4);
cin >> N;
FOR(i, 0, N - 1)
{
int u, v;
cin >> u >> v; u--; v--;
edge[u].PB(v);
edge[v].PB(u);
}
FOR(i, 0, N)
{
bool b; cin >> b;
flip[i] = b;
}
dfs(0, N);
ans = min(dp[0][0][1], dp[0][0][0]);
if (ans == INF)
{
cout << "impossible\n";
return 0;
}
cout << ans << '\n';
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... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |