# | Time | Username | Problem | Language | Result | Execution time | Memory |
---|---|---|---|---|---|---|---|
1096938 | andrei_iorgulescu | The Xana coup (BOI21_xanadu) | C++14 | 109 ms | 27476 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.
#include <bits/stdc++.h>
using namespace std;
#define int long long
int inf = 1e9;
int n, v[100005];
vector<int> g[100005], f[100005];
bool viz[100005];
int dp[100005][2][2];
void dfs(int nod)
{
viz[nod] = true;
for (auto it : g[nod])
if (!viz[it])
f[nod].push_back(it), dfs(it);
}
void solve(int nod)
{
for (auto fiu : f[nod])
solve(fiu);
for (int sf = 0; sf < 2; sf++)
{
for (int cf = 0; cf < 2; cf++)
{
vector<vector<int>> d(f[nod].size() + 1);
for (int i = 0; i < d.size(); i++)
d[i].resize(2);
d[0][0] = cf;
d[0][1] = inf;
for (int i = 1; i < d.size(); i++)
{
int fiu = f[nod][i - 1];
for (int cr = 0; cr < 2; cr++)
{
d[i][cr] = inf;
for (int uu = 0; uu < 2; uu++)
d[i][cr] = min(d[i][cr], d[i - 1][cr ^ uu] + dp[fiu][cf][uu]);
}
}
dp[nod][sf][cf] = d[d.size() - 1][sf ^ cf ^ v[nod]];
}
}
}
signed main()
{
cin >> n;
for (int i = 1; i < n; i++)
{
int x, y;
cin >> x >> y;
g[x].push_back(y);
g[y].push_back(x);
}
for (int i = 1; i <= n; i++)
cin >> v[i];
dfs(1);
solve(1);
int kk = min(dp[1][0][0], dp[1][0][1]);
if (kk >= inf)
cout << "impossible";
else
cout << kk;
return 0;
}
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... |