Cod sursa(job #2863359)

Utilizator domistnSatnoianu Dominic Ioan domistn Data 6 martie 2022 16:42:03
Problema Santa Scor 100
Compilator cpp-64 Status done
Runda Arhiva de probleme Marime 3 kb
#include <bits/stdc++.h>

#define NMAX 45005

using namespace std;

int n, m, s, e, q, niv[NMAX], nra[NMAX], nrCompBiconexe;
bool possiblePath[NMAX], v[NMAX];
vector<int> adj[NMAX], pctArticulatie, ans;
stack<int> st;
set<int> compBiconexe[NMAX];

void pupici() {
    cout << "No chance";
    exit(0);
}

inline void dfs(int nod, int tata = 0) {
    v[nod] = 1;
    niv[nod] = niv[tata] + 1;
    nra[nod] = niv[nod];
    st.push(nod);
    for(const auto &el : adj[nod]) {
        if(el == tata) continue;
        if(v[el]) {
            nra[nod] = min(nra[nod], niv[el]);
        } else {
            dfs(el, nod);
            nra[nod] = min(nra[nod], nra[el]);
            possiblePath[nod] |= possiblePath[el];
            if(niv[nod] <= nra[el]) {
                if(!possiblePath[el]) {
                    while(st.top() != el)
                        st.pop();
                    st.pop();
                } else {
                    ++nrCompBiconexe;
                    while(st.top() != el) {
                        compBiconexe[nrCompBiconexe].insert(st.top());
                        st.pop();
                    }
                    compBiconexe[nrCompBiconexe].insert(el);
                    st.pop();
                    compBiconexe[nrCompBiconexe].insert(nod);
                    pctArticulatie.push_back(nod);
                }
            }
        }
    }
}

inline bool bt(int poz, int sz, int nod, int en) {
    for(const auto &el : adj[nod]) {
        if(!v[el]) {
            v[el] = 1;
            ans.push_back(el);
            if(poz == sz - 1) return (el == en || !en);
            else if(el != en) {
                if(bt(poz + 1, sz, el, en)) return 1;
            }
            ans.pop_back();
            v[el] = 0;
        }
    }
    return 0;
}

int main()
{
    freopen("santa.in", "r", stdin);
    freopen("santa.out", "w", stdout);
    scanf("%d%d", &n, &m);
    for(int i = 1, x, y; i <= m; ++i) {
        scanf("%d%d", &x, &y);
        adj[x].push_back(y);
        adj[y].push_back(x);
    }
    scanf("%d%d%d", &s, &e, &q);

    pctArticulatie.push_back(s);
    possiblePath[e] = 1;
    dfs(s);

    if(!possiblePath[s]) pupici();

    if(compBiconexe[1].find(q) == compBiconexe[1].end()) {
        reverse(compBiconexe + 1, compBiconexe + nrCompBiconexe + 1);
        reverse(pctArticulatie.begin(), pctArticulatie.end());
    }
    if(compBiconexe[1].find(q) == compBiconexe[1].end()) {
        pupici();
    }
    pctArticulatie[0] = q;
    for(int i = 1; i <= n; ++i)
        v[i] = 1;
    ans.push_back(q);
    for(int i = 1; i <= nrCompBiconexe; ++i) {
        for(const auto &el : compBiconexe[i])
            v[el] = 0;
        v[pctArticulatie[i - 1]] = 1;
        if(!bt(1, compBiconexe[i].size(), pctArticulatie[i - 1], (i == nrCompBiconexe ? 0 : pctArticulatie[i])))
            pupici();
    }
    printf("%d\n", (int)ans.size());
    for(const auto &el : ans)
        printf("%d ", el);
    return 0;
}