Cod sursa(job #2174095)

Utilizator stefii_predaStefania Preda stefii_preda Data 16 martie 2018 10:44:08
Problema Ciclu Eulerian Scor 100
Compilator cpp Status done
Runda Arhiva educationala Marime 1.21 kb
#include <fstream>
#include <vector>

using namespace std;

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

const int N = 100005;
const int M = 500005;

vector <int> g[N], s;
int from[M], to[M];
bool folosit[M];

int main()
{
    int n, m, i, x, y;
    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;
        }
    s.push_back(1);
    int nod, muchie, vec;
    while(!s.empty())
    {
        nod = s.back();
        if(!g[nod].empty())
        {
            muchie = g[nod].back();
            g[nod].pop_back();//!!!!
            if(folosit[muchie] == false)
            {
                folosit[muchie] = true;
                vec = to[muchie];
                if(vec == nod)
                    vec = from[muchie];
                s.push_back(vec);
            }
        }
        else
        {
            s.pop_back();
            if(!s.empty())
                out << nod <<" ";
        }
    }

    return 0;
}