Cod sursa(job #1752505)

Utilizator TimitocArdelean Andrei Timotei Timitoc Data 4 septembrie 2016 11:19:46
Problema Santa Scor 100
Compilator cpp Status done
Runda Arhiva de probleme Marime 2.84 kb
#include <cstdio>
#include <vector>
#include <stack>
#include <algorithm>
#define MAXN 45050
#define inf 0x3f3f3f3f

using namespace std;

vector<int> graf[MAXN];
vector<vector<int> > compo;
vector<int> temp, sol;
stack<pair<int, int> > st;
int depth[MAXN], viz[MAXN], low[MAXN], parent[MAXN], need[MAXN], art[MAXN], inc[MAXN];
int n, m, s, e, q, goon = 1;

void citire()
{
    int x, y;
    scanf("%d %d", &n, &m);
    for (int i = 1; i <= m; i++) {
		scanf("%d %d", &x, &y);
        graf[x].push_back(y);
        graf[y].push_back(x);
    }
    scanf("%d %d %d", &s, &e, &q);
    need[s] = 1;
}

void addCompo(int nod, int must)
{
    if (!must) {
		while (st.top().first != nod)
			st.pop();
		st.pop();
		return ;
    }
    temp.clear();
    while (!st.empty()) {
		int cx = st.top().first;
		int cy = st.top().second;
		st.pop();
        temp.push_back(cy);
        if (cx == nod) break;
    }
    temp.push_back(nod);
    compo.push_back(temp);
    art[compo.size()] = nod;
}

void biconex(int nod, int d)
{
	low[nod] = depth[nod] = d;
	viz[nod] = 1;
    for (int y : graf[nod]) {
        if (parent[nod] == y) continue;
        if (viz[y])
			low[nod] = min(low[nod], depth[y]);
		else {
			parent[y] = nod;
			st.push(make_pair(nod, y));
            biconex(y, d+1);
            low[nod] = min(low[nod], low[y]);
            need[nod] |= need[y];
            if (d <= low[y]) { // nod e punct de articulatie
                addCompo(nod, need[y]);
            }
		}
    }
}

int prepare()
{
    if (!need[e]) return 0;
    if (find(compo[0].begin(), compo[0].end(), q) == compo[0].end()) {
		reverse(compo.begin(), compo.end());
        reverse(art, art+1+compo.size());
    }
    if (find(compo[0].begin(), compo[0].end(), q) == compo[0].end())
		return 0;
	art[0] = q;
	art[compo.size()] = -1;
	return 1;
}

void afisare()
{
    if (!goon) {
		printf("No chance");
		return;
    }
    printf("%d\n", sol.size());
    for (int nod : sol)
		printf("%d ", nod);
}

int hamilton(int crt, int pas, int npas, int fin) /// lant hamilton cu back
{
	viz[crt] = 1;
	sol.push_back(crt);
	if (pas == npas && fin == -1) return 1;
    if (pas == npas) return crt == fin;
    for (int y : graf[crt]) {
        if (viz[y] || !inc[y]) continue;
        if (pas+1 != npas && y == fin) continue;
        if (hamilton(y, pas+1, npas, fin))
			return 1;
		else {
			sol.pop_back();
			viz[y] = 0;
		}
    }
    return 0;
}

void buildSolution()
{
    for (int i = 0; i < compo.size() && goon; i++) {
		for (int y : compo[i])
			inc[y] = 1, viz[y] = 0;
        goon = hamilton(art[i], 0, compo[i].size()-1, art[i+1]);
        if (!sol.empty() && i+1 < compo.size()) sol.pop_back();
        for (int y : compo[i])
			inc[y] = 0;
    }
}

int main()
{
	freopen("santa.in", "r", stdin);
	freopen("santa.out", "w", stdout);

	citire();
    biconex(e, 1);
    goon = prepare();
    buildSolution();
    afisare();
	return 0;
}