Cod sursa(job #2258034)

Utilizator AlexandruabcdeDobleaga Alexandru Alexandruabcde Data 10 octombrie 2018 19:30:44
Problema Ciclu Eulerian Scor 10
Compilator cpp Status done
Runda Arhiva educationala Marime 0.99 kb
#include <bits/stdc++.h>
#define pf push_front
#define pb push_back

using namespace std;

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

const int NMax = 100005;

vector <int> G[NMax];
list <int> Q;

int n, m, x, y;

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

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

    Q.pf(1);
    while (!Q.empty())
    {
        x = Q.front();
        if (G[x].empty() == true)
        {
            if (Q.size() == 1) break;

            g << x << " ";
            Q.pop_front();

            continue;
        }
        else
        {
            int aux = G[x].back();
            Q.pf(aux);
            G[x].pop_back();
            for (int i = 0; i < G[aux].size(); i++)
            {
                if (G[aux][i] == x)
                {
                    G[aux].erase(G[aux].begin() + i);
                }
            }
        }
    }
    return 0;
}