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>
#define fo(i, d, c) for (int i = d; i <= c; i++)
#define fod(i, c, d) for (int i = c; i >= d; i--)
#define maxn 1000010
#define N 1010
#define fi first
#define se second
#define pb emplace_back
#define en cout << "\n";
#define int long long
#define inf (int)1e9
#define bitcount(x) __builtin_popcountll(x)
#define pii pair<int, int>
#define vii vector<pii>
#define lb(x) x & -x
#define bit(i, j) ((i >> j) & 1)
#define offbit(i, j) (i ^ (1LL << j))
#define onbit(i, j) (i | (1LL << j))
#define vi vector<int>
#define all(x) x.begin(), x.end()
#define ss(x) (int)x.size()
#define UNIQUE(v) v.erase(unique(all(v)),v.end())
template <typename T1, typename T2>
bool minimize(T1 &a, T2 b)
{
if (a > b)
{
a = b;
return true;
}
return false;
}
template <typename T1, typename T2>
bool maximize(T1 &a, T2 b)
{
if (a < b)
{
a = b;
return true;
}
return false;
}
using namespace std;
const int nsqrt = 450;
const int mod = 1e9 + 7;
void add(int &x, int k)
{
x += k;
x %= mod;
if(x < 0) x += mod;
}
void del(int &x, int k)
{
x -= k;
x %= mod;
if(x < 0) x += mod;
}
int n;
vi ke[maxn];
int a[maxn];
int dp[2][2][maxn];
void dfs(int u,int par = 0)
{
int tmp[2][2] = {};
fo(i,0,1) tmp[1][i] = inf;
for(int v : ke[u]) if(v != par)
{
dfs(v,u);
int now[2][2];
// odd or even,press or not
now[0][0] = min(tmp[0][0] + dp[0][0][v],tmp[1][0] + dp[0][1][v]);
now[0][1] = min(tmp[1][1] + dp[1][1][v],tmp[0][1] + dp[1][0][v]);
now[1][0] = min(tmp[0][0] + dp[0][1][v],tmp[1][0] + dp[0][0][v]);
now[1][1] = min(tmp[0][1] + dp[1][1][v],tmp[1][1] + dp[1][0][v]);
fo(i,0,1) fo(j,0,1) tmp[i][j] = now[i][j];
}
fo(i,0,1) fo(j,0,1)
{
int val = a[u] ^ i ^ j;
if(val) dp[i][j][u] = tmp[1][j] + j;
else dp[i][j][u] = j + tmp[0][j];
}
}
main()
{
#define name "TASK"
if (fopen(name ".inp", "r"))
{
freopen(name ".inp", "r", stdin);
freopen(name ".out", "w", stdout);
}
ios_base::sync_with_stdio(false);
cin.tie(NULL);
cin >> n;
fo(i,1,n - 1)
{
int u,v;
cin >> u >> v;
ke[u].pb(v);
ke[v].pb(u);
}
fo(i,1,n) cin >> a[i];
dfs(1);
int ans = min(dp[0][1][1],dp[0][0][1]);
if(ans >= inf) cout << "impossible";
else cout << ans;
}
Compilation message (stderr)
xanadu.cpp:84:1: warning: ISO C++ forbids declaration of 'main' with no type [-Wreturn-type]
84 | main()
| ^~~~
xanadu.cpp: In function 'int main()':
xanadu.cpp:89:16: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
89 | freopen(name ".inp", "r", stdin);
| ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~
xanadu.cpp:90:16: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
90 | freopen(name ".out", "w", stdout);
| ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~
# | 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... |