Cod sursa(job #2215223)

Utilizator andreiomd1Onut Andrei andreiomd1 Data 21 iunie 2018 12:17:01
Problema Ciclu Eulerian Scor 80
Compilator cpp Status done
Runda Arhiva educationala Marime 1.16 kb
#include <bits/stdc++.h>

using namespace std;

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

const int NMAX = 100005;

int poz;

vector <int> G[NMAX];

list <int> Q;

int n, x, y, m, nr;

int main()
{
    f>>n>>m;

    for(int i=1; i<=m; i++) {
        f>>x>>y;
        G[x].push_back(y);
        G[y].push_back(x);
    }

    bool ok=true;

    for(int i=1; i<=n; i++)
        if(G[i].size()%2==1) {
            ok=false;
            break;
        }

    if(ok==true) {

        Q.push_back(1);

        nr++;

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

            if(!G[x].size() ) {
                if(nr<=m)
                    g<<x<<' ', nr++;
                Q.pop_front();
            }

            else {
                y=G[x].back();
                Q.push_front(y);
                G[x].pop_back();

                for(poz=0; poz<G[y].size(); poz++)
                    if(G[y][poz]==x) {
                        G[y].erase(G[y].begin() + poz);
                        break;
                    }
            }
        }
    }

    else g<<-1;

    g<<'\n';

    return 0;
}