Cod sursa(job #798501)

Utilizator IoanaMarMarussi Ioana IoanaMar Data 16 octombrie 2012 18:22:03
Problema Ciclu Eulerian Scor 100
Compilator cpp Status done
Runda Arhiva educationala Marime 1.1 kb
#include <fstream>
#include <vector>
#include <list>
using namespace std;

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

vector <int> v[500000];
list <int> q;
int n,m,x,y,nr;

vector <int> :: iterator it;
int main()
{
    int i;
    f>>n>>m;
    for(i=1;i<=m;i++)
    {
        f>>x>>y;
        v[y].push_back(x);
        v[x].push_back(y);
    }
    for(i=1;i<=n;i++)
        if(v[i].size()%2!=0)
            g<<"-1";

        nr=0;
        q.push_front(1);
        while(!q.empty())
        {
            x=q.front();
            if(v[x].empty())
            {
                nr++;
                q.pop_front();
                if(nr<=m)
                    g<<x<<" ";
            }
            else
            {
                y=v[x].back();
                q.push_front(y);
                v[x].pop_back();
                for(it=v[y].begin();it<=v[y].end();it++)
                    if(*it==x)
                    {
                        v[y].erase(it);
                        break;
                    }

        }
    }

    return 0;
}