Cod sursa(job #2371394)

Utilizator anamaria41Raicu Ana anamaria41 Data 6 martie 2019 17:27:06
Problema Ciclu Eulerian Scor 80
Compilator cpp-64 Status done
Runda Arhiva educationala Marime 1.24 kb
#include <cstdio>
#include <list>
#include <vector>
#define N 100050
using namespace std;

vector <int> g[N];
list <int> q;
int n,m,i,x,y,nr;
bool ok;

void euler(int x)
{

    q.push_back(x);
    vector <int>::iterator it;

    while ( !q.empty() )
    {
        x=q.front ();

        if(g[x].empty())
        {
            q.pop_front();
            if (!q.empty())  printf("%d ", x);
        }

        else
        {
            int nod=g[x].back();
            q.push_front(nod);
            g[x].pop_back();
            for ( it = g[nod].begin(); it!=g[nod].end(); it++)
                if (*it==x)
                {
                    g[nod].erase(it);
                    break;
                }

        }

    }
}

int main()
{

    freopen ("ciclueuler.in", "r",stdin );
    freopen ("ciclueuler.out", "w", stdout);

    scanf ("%d%d", &n,&m);
    for (i=1; i<=m; i++)
    {
        scanf ("%d%d", &x, &y);
        g[x].push_back(y);
        g[y].push_back(x);
    }
    ok=true;
    for (i=1; i<=n; i++)
        if (g[i].size()%2==1)
        {
            ok=false;
            break;
        }


    if (!ok)
        printf ("-1\n");
    else
        euler (1);



    return 0;
}