Cod sursa(job #2215210)

Utilizator avramraresAvram Rares Stefan avramrares Data 21 iunie 2018 12:02:02
Problema Ciclu Eulerian Scor 30
Compilator cpp Status done
Runda Arhiva educationala Marime 1.24 kb
#include <bits/stdc++.h>
using namespace std;
ifstream f("ciclueuler.in");
ofstream g("ciclueuler.out");
int n,m,x,y,i,l,sol;
bool ok;
vector < int > G[1000];
vector < int > :: iterator it;
list < int > Q;
int main()
{
    f>>n>>m;
    for(i=1; i<=m; i++)
    {
        f>>x>>y;
        G[x].push_back(y);
        G[y].push_back(x);
    }
    ok=true;
    for(i=1; i<=n; i++)
    {
        x=G[i].size();
        if(x%2==1)
        {
            ok=false;
            break;
        }
    }
    if(!ok)
        g<<-1<<" ";
    else
    {
        Q.push_back(1);
        while(!Q.empty())
        {
            x=Q.front();
            if(!G[x].size())
            {
                if(sol<m)
                    g<<x<<" ";
                sol++;
                Q.pop_front();
            }
            else
            {
                y=G[x].back();
                Q.push_front(y);
                G[x].pop_back();
                for(it= G[y].begin() ; it!=G[y].end() ; it++)
                {
                    if(*it==x)
                    {
                        G[y].erase(it);
                        break;
                    }
                }
            }
        }
    }

    return 0;
}