Submission #1344089

#TimeUsernameProblemLanguageResultExecution timeMemory
1344089Jakub_WozniakThe Xana coup (BOI21_xanadu)C++20
100 / 100
43 ms16960 KiB
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef pair<int,int> pii;
#define st first
#define nd second
const ll maxn = (1e+5)+9;
ll dp[maxn][2][2];
vector <int> t[maxn];
bool stp[maxn];

void DFS(int v , int o)
{
    ll maxdif = maxn+13;
    ll aktM = 0 ,res = 0;

    for(auto p : t[v])
    {
        if(p == o)continue;
        DFS(p,v);
    }

    for(int j = 0 ;j < 2; j++)//dp[v][j]
    {
        //dp[v][j][0] - nie uruchamiam u siebie
        maxdif =maxn+13 , aktM = 0;
        res = 0;
        for(auto p : t[v])
        {
            if(p == o)continue;
            res += min(dp[p][0][0],dp[p][0][1]);
            if(dp[p][0][1] < dp[p][0][0])
            {
                aktM ^= 1;
            }
            maxdif=  min(maxdif ,  max(dp[p][0][0],dp[p][0][1])-min(dp[p][0][0],dp[p][0][1]));
        }
        if(((j^stp[v])^aktM) == 0) // (moj bit)^aktM jest ok
        {
            dp[v][j][0] = res;
        }
        else
        {
            res += maxdif;
            dp[v][j][0] = res;
        }



        //dp[v][j][1] -  uruchamiam u siebie
        maxdif =maxn+13 , aktM = 0;
        res = 1;
        for(auto p : t[v])
        {
            if(p == o)continue;
            res += min(dp[p][1][0],dp[p][1][1]);
            if(dp[p][1][1] < dp[p][1][0])
            {
                aktM ^= 1;
            }
            maxdif=  min(maxdif ,  max(dp[p][1][0],dp[p][1][1])-min(dp[p][1][0],dp[p][1][1]));
        }

        if(((j^stp[v]^1)^aktM) == 0) // (moj bit)^aktM jest ok
        {
            dp[v][j][1] = res;
        }
        else
        {
            res += maxdif;
            dp[v][j][1] = res;
        }

        dp[v][j][0] = min(dp[v][j][0] , maxn+141);
        dp[v][j][1] = min(dp[v][j][1] , maxn+141);
    }
}

int main()
{
    ios_base::sync_with_stdio(0);
    cin.tie(0);
    int n ,a , b;
    cin >> n;
    for(int i = 0 ; i < n-1 ; i++)
    {
        cin >> a >> b;
        t[a].push_back(b);
        t[b].push_back(a);
    }
    for(int i = 1; i <= n; i++)cin >> stp[i];

    DFS(1,0);

    int Ri = min(dp[1][0][0] , dp[1][0][1]);
    if(Ri > maxn)cout << "impossible\n";
    else cout << Ri << '\n'; 

    return 0;
}
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...