Cod sursa(job #1435154)

Utilizator batistaUPB-Oprea-Cosmin-Dumitru batista Data 12 mai 2015 12:08:59
Problema Ciclu Eulerian Scor 100
Compilator cpp Status done
Runda Arhiva educationala Marime 1.16 kb
#include<fstream>
#include<algorithm>
#include<list>
#include<deque>
#define N 100010
using namespace std;
ofstream g("ciclueuler.out");
list<int> v[N];
list<int>::iterator it;
deque<int> Q;
int n, m, i, x, y, ok;
void euler(int nd)
{
    int nod, nod2;
    Q.push_front(nd);
    while(!Q.empty())
    {
        nod = Q.front();
        if(v[nod].size()==0)
        {
            g << nod << " ";
            Q.pop_front();
            continue;
        }
        nod2 = v[nod].front();
        v[nod].pop_front();
        Q.push_front(nod2);
        for(it = v[nod2].begin(); it != v[nod2].end(); it++)
            if(*it == nod)
            {
                v[nod2].erase(it);
                break;
            }
    }
}
int main()
{
    ifstream f("ciclueuler.in");
    f >> n >> m;
    for(i = 1; i <= m; i++)
    {
        f >> x >> y;
        v[x].push_back(y);
        v[y].push_back(x);
    }
    ok=1;
    for(i = 1; i <= n; i++)
        if(v[i].size() & 1)
        {
            ok=0;
            break;
        }

    if(!ok) g << "-1\n";
    else
        euler(1);
    f.close();
    g.close();
    return 0;
}