Cod sursa(job #2326487)

Utilizator elenaisaiaElena Isaia elenaisaia Data 23 ianuarie 2019 16:37:30
Problema Ciclu Eulerian Scor 80
Compilator cpp-64 Status done
Runda Arhiva educationala Marime 1.29 kb
#include <iostream>
#include <fstream>
#include <vector>
#include <stack>
#include <algorithm>
#include <bitset>

using namespace std;

int n,m,nsol;
vector<int>g[100005];
vector<int>sol;
stack<int>s;
bitset<500005>b;
vector<int>v[100005];

void citire()
{
    ifstream fin("ciclueuler.in");
    fin>>n>>m;
    int x,y;
    for(int i=0;i<m;i++)
    {
        fin>>x>>y;
        g[x].push_back(y);
        g[y].push_back(x);
        v[x].push_back(i);
        v[y].push_back(i);
    }
}

void rez()
{
    s.push(1);
    while(!s.empty())
    {
        int x=s.top(),sizee=g[x].size();
        if(sizee==0)
        {
            nsol++;
            sol.push_back(x);
            s.pop();
        }
        else
        {
            if(b[v[x][sizee-1]]==0)
            {
                b[v[x][sizee-1]]=1;
                s.push(g[x][sizee-1]);
            }
            g[x].pop_back();
        }
    }
}

void verif()
{
    ofstream fout("ciclueuler.out");
    int nr;
    for(int i=1;i<=n;i++)
    {
        nr=g[i].size();
        if(nr%2)
        {
            fout<<"-1";
            return;
        }
    }
    rez();
    for(int i=0;i<nsol-1;i++)
        fout<<sol[i]<<" ";
}

int main()
{
    citire();
    verif();
    return 0;
}