Cod sursa(job #2008154)

Utilizator akaprosAna Kapros akapros Data 5 august 2017 15:32:10
Problema Santa Scor 100
Compilator cpp Status done
Runda Arhiva de probleme Marime 2.97 kb
#include <bits/stdc++.h>
#define maxN 45002

using namespace std;

FILE *fin = freopen("santa.in", "r", stdin);
FILE *fout = freopen("santa.out", "w", stdout);

/* ------------------------------------------ */
int n, m;
vector < int > V[maxN];
int S, D, Q;
/* ------------------------------------------ */
vector < int > art;
vector < vector < int > > bcc;
int st[maxN], disc[maxN], low[maxN];

bool used[maxN], inSt[maxN], onPath[maxN];
/* ------------------------------------------ */
vector < int > path;

void NoSol()
{
    printf("No chance\n");
    exit(0);
}
void Bcc(int nod, int son)
{
    if (!onPath[son])
    {
        while (st[0] > 0 && st[st[0]] != son)
        {
            inSt[st[st[0] --]] = 0;
        }
        inSt[st[st[0] --]] = 0;
        return ;
    }
    else ///
    {
        bcc.push_back(vector<int>());
        art.push_back(nod);
        while (st[0] > 0 && st[st[0]] != son)
        {
            inSt[st[st[0]]] = 0;
            bcc.back().push_back(st[st[0] --]);
        }
        bcc.back().push_back(st[st[0] --]);
        inSt[son] = 0;
        bcc.back().push_back(nod);
    }
}
void dfs(int u, int p)
{
    disc[u] = low[u] = disc[p] + 1;
    inSt[u] = 1;
    st[++ st[0]] = u;
    for (int v : V[u])
        if (!disc[v])
        {
            dfs(v, u);
            onPath[u] |= onPath[v];
            low[u] = min(low[u], low[v]);
            if (low[v] >= disc[u])
                Bcc(u, v);
        }
        else if (v != p && inSt[v])
            low[u] = min(low[u], disc[v]);
}
inline bool Back(int cnt, int sz, int x, int en)
{
    used[x] = true;
    if (cnt != 1)
        path.push_back(x);
    if (cnt == sz)
    {
        if (en == 0 || x == en)
            return true;
        used[x] = false;
        path.pop_back();
        return false;
    }
    for (int y : V[x])
        if (!used[y] && (y != en || cnt == sz - 1) && Back(cnt + 1, sz, y, en))
            return true;
    used[x] = false;
    path.pop_back();
    return false;
}

int main()
{
    scanf("%d %d", &n, &m);
    for (int i = 1; i <= m; ++ i)
    {
        int x, y;
        scanf("%d %d", &x, &y);
        V[x].push_back(y);
        V[y].push_back(x);
    }
    scanf("%d %d %d", &S, &D, &Q);
    art.push_back(S);
    onPath[S] = 1;
    dfs(D, 0);

    if (find(bcc[0].begin(), bcc[0].end(), Q) == bcc[0].end())
    {
        reverse(bcc.begin(), bcc.end());
        reverse(art.begin(), art.end());
    }

    if (!onPath[D] || find(bcc[0].begin(), bcc[0].end(), Q) == bcc[0].end())
        NoSol();
    art[0] = Q;
    art.back() = 0;
    path.push_back(Q);
    memset(used, 1, sizeof(used));
    for (int i = 0; i < bcc.size(); ++ i)
    {
        for (int j : bcc[i])
            used[j] = 0;
        if (!Back(1, bcc[i].size(), art[i], art[i + 1]))
            NoSol();
    }
    printf("%d\n", path.size());
    for (int i : path)
        printf("%d ", i);
    return 0;
}