Cod sursa(job #2215188)

Utilizator andreiomd1Onut Andrei andreiomd1 Data 21 iunie 2018 11:42:20
Problema Ciclu Eulerian Scor 10
Compilator cpp Status done
Runda Arhiva educationala Marime 0.89 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;

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);
    }

    Q.push_back(1);

    g<<1<<' ';

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

        if(!G[x].size() ) {
            if(x!=1)
                g<<x<<' ';
            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;
                }
        }
    }

    g<<'\n';

    return 0;
}