Submission #823884

# Submission time Handle Problem Language Result Execution time Memory
823884 2023-08-13T09:36:23 Z tset The Xana coup (BOI21_xanadu) C++14
10 / 100
57 ms 27456 KB
#include<bits/stdc++.h>
using namespace std;
#define int long long

const int INF = 1e18 + 42;

vector<vector<int> > nxt;
vector<vector<int>> filsDe;
vector<int> pereDe;
vector<vector<vector<int > > > dp;
vector<int > initState;
void dfsInit(int nde,int prc )
{   
    pereDe[nde] = prc;
    for(int iV: nxt[nde])
    {
        if(iV != prc)
        {
            filsDe[nde].push_back(iV);
            dfsInit(iV, nde);
        }
    }
}

int fDP(int isParentT, int isMeT, int nde)
{
    if(dp[isParentT][isMeT][nde] != INF)
    {
        return dp[isParentT][isMeT][nde];
    }
    int nbFils = filsDe[nde].size();
    int totT = isParentT + isMeT + initState[nde];
    int ans = INF;
    if(totT %2 == 0) ////////////////////////////////////////
    {//We need to toggle 0 or two child(ren).
        if(nbFils == 0)
        {
            ans = 0;
        }
        if(nbFils == 1)
        {
            ans = fDP(isMeT, 0, filsDe[nde][0]);
        }
        if(nbFils == 2)
        {
            ans = min(fDP(isMeT, 0, filsDe[nde][0]) + fDP(isMeT, 0, filsDe[nde][1]),
                      fDP(isMeT, 1, filsDe[nde][0]) + fDP(isMeT, 1, filsDe[nde][1]) + 2);
        }
    }
    else
    {
        //We need to toggle only one child.
        if(nbFils == 0)
        {
            ans = INF;
        }
        if(nbFils == 1)
        {
            ans = fDP(isMeT, 1, filsDe[nde][0]) + 1;
        }
        if(nbFils == 2)
        {
            ans = min(fDP(isMeT, 0, filsDe[nde][0]) + fDP(isMeT, 1, filsDe[nde][1]) + 1,
                      fDP(isMeT, 1, filsDe[nde][0]) + fDP(isMeT, 0, filsDe[nde][1]) + 1);
        }
    }
    dp[isParentT][isMeT][nde] = ans;
    return dp[isParentT][isMeT][nde];
}

signed main()
{
    int N;
    scanf("%lld", &N);
    int nbArretes = N-1;
    nxt.resize(N+1);
    filsDe.resize(N+1);
    pereDe.resize(N+1);
    for(int iA = 0; iA < nbArretes; iA++)
    {
        int u, v;
        scanf("%lld%lld", &u, &v);
        nxt[u].push_back(v);
        nxt[v].push_back(u);
    }
    dfsInit(1, 1);

    initState.resize(N+1);
    for(int iNde = 1; iNde <=N; iNde++)
    {
        scanf("%lld", &initState[iNde]);
    }
    dp.resize(2, vector<vector<int>>(2, vector<int>(N+1, INF)));
    int ans = fDP(0, 0, 1);
    ans = min(ans, fDP(0, 1, 1) + 1);
    if(ans > INF)
        printf("impossible\n");
    else 
        printf("%lld", ans);
}

Compilation message

xanadu.cpp: In function 'int main()':
xanadu.cpp:74:10: warning: ignoring return value of 'int scanf(const char*, ...)' declared with attribute 'warn_unused_result' [-Wunused-result]
   74 |     scanf("%lld", &N);
      |     ~~~~~^~~~~~~~~~~~
xanadu.cpp:82:14: warning: ignoring return value of 'int scanf(const char*, ...)' declared with attribute 'warn_unused_result' [-Wunused-result]
   82 |         scanf("%lld%lld", &u, &v);
      |         ~~~~~^~~~~~~~~~~~~~~~~~~~
xanadu.cpp:91:14: warning: ignoring return value of 'int scanf(const char*, ...)' declared with attribute 'warn_unused_result' [-Wunused-result]
   91 |         scanf("%lld", &initState[iNde]);
      |         ~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~
# Verdict Execution time Memory Grader output
1 Incorrect 1 ms 300 KB Output isn't correct
2 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Incorrect 1 ms 300 KB Output isn't correct
2 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Correct 55 ms 27272 KB Output is correct
2 Correct 57 ms 26972 KB Output is correct
3 Correct 52 ms 27456 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 54 ms 27340 KB Output is correct
2 Correct 52 ms 26952 KB Output is correct
3 Correct 50 ms 27420 KB Output is correct
4 Incorrect 43 ms 17008 KB Output isn't correct
5 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Incorrect 1 ms 300 KB Output isn't correct
2 Halted 0 ms 0 KB -