Cod sursa(job #2215190)

Utilizator AlexandruabcdeDobleaga Alexandru Alexandruabcde Data 21 iunie 2018 11:44:03
Problema Ciclu Eulerian Scor 60
Compilator cpp Status done
Runda Arhiva educationala Marime 0.94 kb
#include <bits/stdc++.h>

using namespace std;

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

vector <int> :: iterator it;
vector <int> G[100002];
list <int> Q;

int n, m, x, y, 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);
    }

    Q.push_back(1);
    while (!Q.empty())
    {
        x = Q.front();
        if (G[x].size() == 0)
        {
            if (nr < m)
            {
                g << x << " ";
                nr++;
            }
            Q.pop_front();
            continue;
        }

        y = G[x].back();
        Q.push_front(y);
        G[x].pop_back();
        for (it = G[y].begin(); it != G[y].end(); it++)
        {
            if (*it == x)
            {
                G[y].erase(it);
                break;
            }
        }
    }
    return 0;
}