Cod sursa(job #1644020)

Utilizator lacraruraduRadu Matei Lacraru lacraruradu Data 9 martie 2016 21:14:08
Problema Ciclu Eulerian Scor 60
Compilator cpp Status done
Runda Arhiva educationala Marime 1.62 kb
#include <fstream>

using namespace std;

ifstream in("ciclueuler.in");
ofstream out("ciclueuler.out");

#define N 100005
#define M 500005

struct muchie
{
    int x, y;
};

int n, m;
int lst[N], vf[2 * M], urm[2 * M], nr;
muchie muchii[M];
bool ok[M];

int stiva[M], st;

int ciclu[M], nCE;

int gasesteVecin(int x, muchie e)
{
    return (x == e.x) ? e.y : e.x;
}

int main()
{
    in >> n >> m;

    for(int i = 1; i <= m; i++)
    {
        int x, y;
        in >> x >> y;
        muchii[i].x = x;
        muchii[i].y = y;

        vf[++nr] = i;
        urm[nr] = lst[x];
        lst[x] = nr;
        vf[++nr] = i;
        urm[nr] = lst[y];
        lst[y] = nr;
    }

    for(int i = 1; i <= n; i++)
    {
        stiva[++st] = i;
        while(st)
        {
            int x = stiva[st];
            bool blocat = true;

            for(int i = lst[x]; i; i = urm[i])
            {
                int e = vf[i];
                if(!ok[e])
                {
                    int y = gasesteVecin(x, muchii[e]);
                    stiva[++st] = y;
                    ok[e] = 1;
                    blocat = false;
                    break;
                }
            }

            if(blocat)
            {
                st--;
                ciclu[++nCE] = x;
            }

        }
    }

    for(int i = 1; i <= m; i++)
        if(!ok[i])
        {
            out << -1 << '\n';
            return 0;
        }

    if(m < nCE)
        nCE = m;
    for(int i = 1; i <= nCE; i++)
        out << ciclu[i] << ' ';
    out << '\n';
    return 0;
}