Cod sursa(job #3292904)

Utilizator Mihai_999Diaconeasa Mihai Mihai_999 Data 9 aprilie 2025 18:50:32
Problema Santa Scor 0
Compilator cpp-64 Status done
Runda Arhiva de probleme Marime 3.44 kb

#include <iostream>
#include <fstream>
#include <vector>
#include <algorithm>
#define nl '\n'

using namespace std;

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

const int NMAX = 45005;

int n, m, s, e, q, k, cb, fr[NMAX], ord[NMAX], low[NMAX], st[NMAX], top, art[NMAX];
bool reach[NMAX];
vector<int> adj[NMAX], comp[NMAX], ans;

void dfs(int i, int p)
{
    fr[i] = 1;
    ord[i] = low[i] = ++k;
    st[++top] = i;
    for (int j : adj[i])
    {
        if (j == p)
            continue;
        if (fr[j])
            low[i] = min(low[i], ord[j]);
        else
        {
            dfs(j, i);
            low[i] = min(low[i], low[j]);
            reach[i] |= reach[j];
            if (low[j] >= ord[i])
            {
                if (!reach[j])
                {
                    while (st[top] != j)
                        top--;
                    top--;
                }
                else
                {
                    cb++;
                    while (st[top] != j)
                        comp[cb].push_back(st[top--]);
                    comp[cb].push_back(j);
                    top--;
                    comp[cb].push_back(i);
                    art[cb] = i;
                }
            }
        }
    }
    return;
}

bool bck(int step, int sz, int i, int target)
{
    if (step == sz)
    {
        for (int j : adj[i])
        {
            if (!fr[j])
            {
                if (j == target || target == -1)
                {
                    fr[j] = 1;
                    ans.push_back(j);
                    return 1;
                }
            }
        }
    }
    else
    {
        for (int j : adj[i])
        {
            if (!fr[j])
            {
                if (j == target)
                    continue;
                fr[j] = 1;
                ans.push_back(j);
                bool ok = bck(step+1, sz, j, target);
                if (ok)
                    return 1;
                ans.pop_back();
                fr[j] = 0;
            }
        }
    }
    return 0;
}

bool searchFor(int nod, int c)
{
    for (int i : comp[c])
        if (i == nod)
            return 1;
    return 0;
}

int main()
{
    fin >> n >> m;
    for (int i = 1; i <= m; i++)
    {
        int x, y;
        fin >> x >> y;
        adj[x].push_back(y);
        adj[y].push_back(x);
    }
    fin >> s >> e >> q;
    reach[e] = 1;
    dfs(s, 0);
    if (!reach[s])
    {
        fout << "No chance";
        return 0;
    }
    bool inE = searchFor(q, 1);
    if (!inE)
    {
        bool inS = searchFor(q, cb);
        if (!inS)
        {
            fout << "No chance";
            return 0;
        }
        reverse(comp+1, comp+1+cb);
        reverse(art+1, art+1+cb);//
        for (int i = 1; i <= n; i++)
            art[i-1] = art[i];
    }
    for (int i = 1; i <= n; i++)
        fr[i] = 1;
    art[0] = q;
    ans.push_back(q);
    for (int i = 1; i <= cb; i++)
    {
        for (int j : comp[i])
            fr[j] = 0;
        fr[art[i-1]] = 1;
        int target = art[i];
        if (i == cb)
            target = -1;
        bool ok = bck(2, comp[cb].size(), art[i-1], target);
        if (!ok)
        {
            fout << "No chance";
            return 0;
        }
    }
    fout << ans.size() << nl;
    for (int i : ans)
        fout << i << ' ';
    return 0;
}