Cod sursa(job #2072165)

Utilizator cameleonGeorgescu Dan cameleon Data 21 noiembrie 2017 15:17:22
Problema Ciclu Eulerian Scor 100
Compilator cpp Status done
Runda Arhiva educationala Marime 1.4 kb
#include <fstream>
#include <vector>
#include <stack>
using namespace std;
const int N = 100005;
const int M = 500005;
ifstream f("ciclueuler.in");
ofstream g("ciclueuler.out");

struct adiacent{
    int nod, muc;
};
vector <adiacent> V[N];
stack <int> s;
vector <int> vect;
int n, m;
bool viz[M];
int nrviz;


void euler()
{
    s.push(1);
    while(!s.empty())
    {
        int vf = s.top();
        while(V[vf].size() > 0 && viz[V[vf].back().muc] == 1)
                V[vf].pop_back();

        if(V[vf].size() > 0)
        {
            int nx = V[vf].back().nod;
            viz[V[vf].back().muc] = 1;
            V[vf].pop_back();

            s.push(nx);
            nrviz++;
        }
        else
        {
            vect.push_back(vf);
            s.pop();
        }
    }
}
int main()
{
    f>>n>>m;
    for(int i = 1; i<= m; i++)
    {
        int x,y;
        f>>x>>y;
        V[x].push_back({y,i});
        V[y].push_back({x,i});
    }
    bool ok = 1;
    for(int i = 1; i<= n; i++)
    {
        int val = V[i].size();
        if(val%2 ==1)
        {
            g<<"-1";
            return 0;
        }
    }

    euler();
    if(nrviz != m)
    {
        g<<"-1";
        return 0;
    }
    int sz = vect.size();
    for(int i =0; i< sz-1; i++)
    {
        g<<vect[i]<<" ";
    }
    f.close();
    g.close();
    return 0;
}