Cod sursa(job #1915867)

Utilizator stefii_predaStefania Preda stefii_preda Data 8 martie 2017 22:53:09
Problema Ciclu Eulerian Scor 100
Compilator cpp Status done
Runda Arhiva educationala Marime 1.28 kb
#include <fstream>
#include <vector>

using namespace std;
ifstream in ("ciclueuler.in");
ofstream out ("ciclueuler.out");
const int NMAX = 100005;
const int MMAX = 500005;

vector <int> g[NMAX], stiva, sol;
bool folosit[MMAX];
int from[MMAX], to[MMAX];

int main()
{
    int n, m, x, y, i;
    in >> n >> m;
    for(i = 1; i <= m; i++)
    {
        in >> x >> y;
        g[x].push_back(i);
        g[y].push_back(i);
        from[i] = x;
        to[i] = y;
    }
    for(i = 1; i <= n; i++)
        if(g[i].size() %2 == 1)
        {
            out <<"-1";
            return 0;
        }
    stiva.push_back(1);
    while(!stiva.empty())
    {
        int nod = stiva.back();
        if(!g[nod].empty())
        {
            int e = g[nod].back();
            g[nod].pop_back();
            if(folosit[e] == false)
            {
                folosit[e] = true;
                int x = from[e];
                if(x == nod)
                    x = to[e];
                stiva.push_back(x);
            }
        }
        else
        {
            stiva.pop_back();
            sol.push_back(nod);
        }

    }
    vector <int> :: iterator it;
    for(it = sol.begin(); it < sol.end() -1; ++it)
        out << *it << " ";
    return 0;
}