Cod sursa(job #1406226)

Utilizator gabib97Gabriel Boroghina gabib97 Data 29 martie 2015 16:39:09
Problema Ciclu Eulerian Scor 100
Compilator cpp Status done
Runda Arhiva educationala Marime 1.29 kb
#include <stdio.h>
#include <vector>
#include <algorithm>
#include <queue>
#include <stack>
using namespace std;
int n,m,i,x,y,nr[100001];
bool o[100001];
queue<int> q;
stack<int> s;
vector<int> G[100001],r;
void DFS()
{
    int x,y;
    s.push(1);
    while (!s.empty())
    {
        x=s.top();
        if (!G[x].empty())
        {
            y=G[x].back();
            G[x].pop_back();
            G[y].erase(find(G[y].begin(),G[y].end(),x));
            s.push(y);
        }
        else
        {
            r.push_back(x);
            s.pop();
        }
    }
}
void conex()
{
    int i,s;
    o[1]=1; q.push(1);
    while (!q.empty())
    {
        s=q.front(); q.pop();
        for (i=0;i<G[s].size();i++)
        if (!o[G[s][i]]) o[G[s][i]]=1,q.push(G[s][i]);
    }
}
int main()
{
    freopen ("ciclueuler.in","r",stdin);
    freopen ("ciclueuler.out","w",stdout);
    scanf("%i%i",&n,&m);
    for (i=1;i<=m;i++)
    {
        scanf("%i%i",&x,&y);
        G[x].push_back(y);
        G[y].push_back(x);
        nr[x]++; nr[y]++;
    }
    conex();
    for (i=1;i<=n;i++)
        if (nr[i]%2!=0||!o[i]) {printf("-1"); return 0;}
    DFS();
    for (i=0;i<r.size()-1;i++) printf("%i ",r[i]);
    fclose(stdin);
    fclose(stdout);
    return 0;
}