Cod sursa(job #3328115)

Utilizator bogdan1479Luca Bogdan Alexandru bogdan1479 Data 6 decembrie 2025 11:20:21
Problema Santa Scor 100
Compilator cpp-64 Status done
Runda Arhiva de probleme Marime 3.34 kb
#include <bits/stdc++.h>

using namespace std;

const int NMAX = 45001;

ifstream fin("santa.in");
ofstream fout("santa.out");

int N, M, s, e, q, Rad, Niv[NMAX], Nma[NMAX], nrCB, PA[NMAX];
bool viz[NMAX], inDrum[NMAX];

vector<int> G[NMAX], CB[NMAX], Drum;
stack<int> S;

void citire()
{
    fin >> N >> M;
    int x, y;
    for(int i = 1; i <= M; i++)
    {
        fin >> x >> y;
        G[x].push_back(y);
        G[y].push_back(x);
    }
    fin >> s >> e >> q;
}

void DFS(int nod, int tata)
{
    S.push(nod);
    viz[nod] = 1;
    Niv[nod] = Niv[tata] + 1;
    Nma[nod] = Niv[nod];
    for(const auto &x : G[nod])
    {
        if(x == tata) continue;
        if(viz[x]) Nma[nod] = min(Nma[nod], Niv[x]);
        else
        {
            DFS(x, nod);
            inDrum[nod] |= inDrum[x];
            Nma[nod] = min(Nma[nod], Nma[x]);
            if(Niv[nod] <= Nma[x])
            {
                if(!inDrum[x])
                {
                    while(S.top() != x)
                        S.pop();
                    S.pop();
                }
                else
                {
                    ++nrCB;
                    while(S.top() != x)
                    {
                        CB[nrCB].push_back(S.top());
                        S.pop();
                    }
                    CB[nrCB].push_back(x);
                    S.pop();
                    CB[nrCB].push_back(nod);
                    PA[nrCB] = nod;
                }
            }
        }
    }
}

bool findNod(int nod, int cb)
{
    for(const auto &x : CB[cb])
        if(x == nod) return 1;
    return 0;
}

bool Backtracking(int k, int n, int nod, int fin)
{
    if(k == n)
    {
        for(const auto &x : G[nod])
            if(!viz[x])
                if(x == fin || fin == -1)
                {
                    viz[x] = 1;
                    Drum.push_back(x);
                    return 1;
                }
    }
    else
    {
        for(const auto &x : G[nod])
        {
            if(!viz[x])
            {
                if(x == fin) continue;
                Drum.push_back(x);
                viz[x] = 1;
                bool vizTot = Backtracking(k + 1, n, x, fin);
                if(vizTot) return 1;
                Drum.pop_back();
                viz[x] = 0;
            }
        }
    }
    return 0;
}

int main()
{
    citire();
    Rad = s;
    inDrum[e] = 1;
    DFS(Rad, 0);
    if(!findNod(q, 1) && !findNod(q, nrCB)) fout << "No chance";
    else
    {
        if(findNod(q, nrCB))
        {
            reverse(CB + 1, CB + nrCB + 1);
            reverse(PA + 1, PA + nrCB + 1);
        }
        else
            for(int i = nrCB - 1; i >= 1; i--) PA[i + 1] = PA[i];
        for(int i = 1; i <= N; i++) viz[i] = 1;
        PA[1] = q;
        PA[nrCB + 1] = -1;
        Drum.push_back(q);
        for(int i = 1; i <= nrCB; i++)
        {
            for(const auto &x : CB[i]) viz[x] = 0;
            viz[PA[i]] = 1;
            bool vizTot = Backtracking(2, CB[i].size(), PA[i], PA[i + 1]);
            if(!vizTot)
            {
                fout << "No chance";
                return 0;
            }
        }
        fout << Drum.size() << '\n';
        for(const auto &x : Drum) fout << x << ' ';
    }
    return 0;
}