Cod sursa(job #3332915)

Utilizator Mateixx1Trandafir Matei Mateixx1 Data 9 ianuarie 2026 20:19:23
Problema Santa Scor 46
Compilator cpp-64 Status done
Runda Arhiva de probleme Marime 3.03 kb
#include <vector>
#include <fstream>
#include <algorithm>
using namespace std;
ifstream f("santa.in");
ofstream g("santa.out");
const int NMAX=45100;
int n,m,x,y,Niv[NMAX],Nma[NMAX],pa[NMAX],rad,s,e,q,vf,S[NMAX],viz[NMAX],nrCB,inDrum[NMAX];
vector<int>G[NMAX],CB[NMAX],Drum;

void DFS(int nod,int tata) {
    S[++vf]=nod;
    viz[nod]=1;
    Niv[nod]=Niv[tata]+1;
    Nma[nod]=Niv[nod];
    for(const int&y:G[nod]) {
        if(y==tata) {
            continue;
        }
        if(viz[y]) {
            Nma[nod]=min(Nma[nod],Niv[y]);
        } else {
            DFS(y,nod);
            inDrum[nod]|=inDrum[y];
            Nma[nod]=min(Nma[nod],Nma[y]);
            if(Niv[nod]<=Nma[y]) {
                if(!inDrum[y]) {
                    while(S[vf]!=y) {
                        vf--;
                    }
                    vf--;
                } else {
                    ++nrCB;
                    pa[nrCB]=nod;
                    while(S[vf]!=y) {
                        CB[nrCB].push_back(S[vf--]);
                    }
                    vf--;
                    CB[nrCB].push_back(y);
                    CB[nrCB].push_back(nod);
                }
            }
        }
    }
}

bool gasim(int a,int care) {
    for(const int &q:CB[care]) {
        if(q==a) {
            return 1;
        }
    }
    return 0;
}

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

int main() {
    f>>n>>m;
    while(m--) {
        f>>x>>y;
        G[x].push_back(y);
        G[y].push_back(x);
    }
    f>>s>>e>>q;
    rad=s;
    inDrum[e]=1;
    DFS(rad,0);
    bool we=gasim(q,nrCB);
    if(!gasim(q,1)&&!we) {
        g<<"No chance";
        return 0;
    }
    if(we) {
        reverse(pa+1,pa+nrCB+1);
        reverse(CB+1,CB+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 int &j:CB[i]) {
            viz[j]=0;
        }
        viz[pa[i]]=1;
        bool posibi=bk(2,CB[i].size(),pa[i],pa[i+1]);;
        if(posibi==0) {
            g<<"No chance";
            return 0;
        }
    }
    for(const int &i:CB[nrCB]) {
        if(!viz[i]) {
            Drum.push_back(i);
            break;
        }
    }
    g<<Drum.size()<<'\n';
    for(const int &w:Drum) {
        g<<w<<' ';
    }
    f.close();
    g.close();
    return 0;
}