# | Time | Username | Problem | Language | Result | Execution time | Memory |
---|---|---|---|---|---|---|---|
1028095 | hasan2006 | The Xana coup (BOI21_xanadu) | C++17 | 71 ms | 23636 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 TL ios::sync_with_stdio(0),cin.tie(0),cout.tie(0);
#define rall(s) s.rbegin(),s.rend()
#define all(s) s.begin(),s.end()
#define pb push_back
#define se second
#define fi first
#define ll long long
#define ld long double
#define YES cout<<"YES\n"
#define Yes cout<<"Yes\n"
#define yes cout<<"yes\n"
#define NO cout<<"NO\n"
#define No cout<<"No\n"
#define no cout<<"no\n"
const int N = 1e5 + 9 , mod = 1e9 + 7;
ll dp[N][3][3] , a[N] , b[N];
vector<int>v[N];
void dfs(int n , int p =0){
for(auto to : v[n])
if(to != p)
dfs(to , n);
for(int x = 0; x < 2; x++){
for(int y = 0; y < 2; y++){
b[0] = b[1] = b[3] = b[4] = 0;
b[2] = 1e18;
for(auto to : v[n]){
if(to == p)
continue;
if(dp[to][y][0] != -1 && dp[to][y][1] != -1){
b[2] = min({b[2] , abs(dp[to][y][0] - dp[to][y][1])});
if(dp[to][y][0] >= dp[to][y][1])
b[1]++ , b[0] += dp[to][y][1];
else
b[4] += dp[to][y][0];
}else if(dp[to][y][0] == -1 && dp[to][y][1] == -1){
b[3]++;
}else if(dp[to][y][1] != -1)
b[1]++ , b[0] += dp[to][y][1];
else
b[4] += dp[to][y][0];
}
int f = abs(x - ((a[n] + y) % 2));
if(b[3])
dp[n][x][y] = -1;
else if(b[1] % 2 == f || b[2] != 1e18)
dp[n][x][y] = b[4] + b[0] + y + (b[1] % 2 != f) * (b[2]);
else
dp[n][x][y] = -1;
}
}
}
void solve()
{
ll n , q , i , j ,l ,r , x , y , s = 0 , f , k , m , mn = 1e18, mx = 0 ;
cin>>n;
for(i = 1; i < n; i++){
cin>>l>>r;
v[l].pb(r) , v[r].pb(l);
}
for(i = 1; i <= n; i++)
cin>>a[i];
dfs(1 , 0);
if(dp[1][0][0] == -1 && dp[1][0][1] == -1)
cout<<"impossible";
else if((dp[1][0][0] != -1 && dp[1][0][1] != -1 && dp[1][0][0] < dp[1][0][1]) || (dp[1][0][1] == -1))
cout<<dp[1][0][0];
else
cout<<dp[1][0][1];
}
int main(){
TL;
/*#ifndef ONLINE_JUDGE
freopen("input.txt", "r", stdin);
freopen("output.txt", "w", stdout);
#endif*/
int t = 1;
// cin>>t;
while(t--)
{
solve();
}
}
// Author : حسن
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... |