# | Time | Username | Problem | Language | Result | Execution time | Memory |
---|---|---|---|---|---|---|---|
696921 | Tien_Noob | The Xana coup (BOI21_xanadu) | C++17 | 68 ms | 23172 KiB |
This submission is migrated from previous version of oj.uz, which used different machine for grading. This submission may have different result if resubmitted.
//Make CSP great again
//Vengeance
#include <bits/stdc++.h>
#define TASK "TESTCODE"
using namespace std;
const int N = 1e5;
int dp[N + 1][2][2];
vector<int> adj[N + 1];
int a[N + 1], n;
void read()
{
cin >> n;
for (int i = 1; i < n; ++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];
}
}
void DFS(int u, int p)
{
memset(dp[u], 0x3f, sizeof(dp[u]));
dp[u][a[u]][0] = 0;
dp[u][1 ^ a[u]][1] = 1;
for (int v : adj[u])
{
if (v == p)
{
continue;
}
DFS(v, u);
int nw[2][2];
memset(nw, 0x3f, sizeof(nw));
for (int i1 = 0; i1 < 2; ++ i1)
{
for (int j1 = 0; j1 < 2; ++j1)
{
for (int i2 = 0; i2 < 2; ++ i2)
{
for (int j2 = 0; j2 < 2; ++ j2)
{
int t = j1 ^ j2;
if (i2 ^ j1)
{
continue;
}
nw[i1 ^ j2][j1] = min(nw[i1 ^ j2][j1], dp[u][i1][j1] + dp[v][i2][j2]);
}
}
}
}
for (int i1 = 0; i1 < 2; ++ i1)
{
for (int j1 = 0; j1 < 2; ++ j1)
{
dp[u][i1][j1] = nw[i1][j1];
}
}
}
/*cerr << u << '\n';
for (int i1 = 0; i1 < 2; ++ i1)
{
for (int j1 = 0; j1 < 2; ++ j1)
{
cerr << dp[u][i1][j1] << ' ';
}
}
cerr << '\n';*/
}
void solve()
{
DFS(1, 0);
int res = min(dp[1][0][0], dp[1][0][1]);
if (res <= n)
{
cout << res;
return ;
}
cout << "impossible";
}
int main()
{
ios_base::sync_with_stdio(false);
cin.tie(nullptr);
if (fopen(TASK".INP", "r"))
{
freopen(TASK".INP", "r", stdin);
//freopen(TASK".OUT", "w", stdout);
}
int t = 1;
bool typetest = false;
if (typetest)
{
cin >> t;
}
for (int __ = 1; __ <= t; ++ __)
{
//cout << "Case " << __ << ": ";
read();
solve();
}
}
Compilation message (stderr)
# | 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... |