Cod sursa(job #2143460)

Utilizator razvanlgu31Razvan Lungu razvanlgu31 Data 25 februarie 2018 23:12:47
Problema Ciclu Eulerian Scor 100
Compilator cpp Status done
Runda Arhiva educationala Marime 1.24 kb
#include <fstream>
#include <vector>

using namespace std;
ifstream fin("ciclueuler.in");
ofstream fout("ciclueuler.out");

int n, m, i, x, y, viz[500010];
vector < int > from, to, g[100010], s, sol;
int main()
{
    fin >> n >> m;
    for(i = 0 ; i < m ; i++)
    {
        fin >> x >> y;
        g[x].push_back(i);
        g[y].push_back(i);
        from.push_back(x);
        to.push_back(y);
    }
    for(i = 1 ; i <= n ; i++)
        if(g[i].size() % 2 == 1)
        {
            fout << -1;
            return 0;
        }
    s.push_back(1);
    while(!s.empty())
    {
        int nod = s.back();
        if(!g[nod].empty())
        {
            int muchie = g[nod].back();
            g[nod].pop_back();
            if(viz[muchie] == 0)
            {
                int nod1 = 0;
                if(from[muchie] == nod)
                    nod1 = to[muchie];
                else
                    nod1 = from[muchie];
                s.push_back(nod1);
                viz[muchie] = 1;
            }
        }
        else
        {
            sol.push_back(nod);
            s.pop_back();
        }
    }

    for(i = 0; i < sol.size() - 1; i++)
        fout << sol[i] << " ";
    return 0;
}