Cod sursa(job #1924453)

Utilizator DragosCDragos Corleanca DragosC Data 12 martie 2017 12:53:20
Problema Ciclu Eulerian Scor 100
Compilator cpp Status done
Runda Arhiva educationala Marime 1.26 kb
#include <iostream>
#include <fstream>
#include <vector>
#include <stack>
#define nmax 100001
#define mmax 500001
using namespace std;
int n,m;
vector<int> graf[nmax];
int fr[nmax];
stack<int> st;
ofstream g("ciclueuler.out");
struct muchii
{
    int x,y;
    bool del=0;
} v[mmax];

void ciclu()
{

    st.push(1);
    while(!st.empty())
    {
        int topp=st.top();
        if(graf[topp].size())
        {
            int next=graf[topp].back();
            graf[topp].pop_back();
            if(v[next].del)
                continue;
            v[next].del=1;
            if(v[next].x==topp)
                st.push(v[next].y);
            else st.push(v[next].x);
        }
        else
        {
            g<<st.top()<<" ";
            st.pop();
        }
    }
}

int even()
{
    for(int i=1; i<=n; i++)
        if(fr[i]%2==1)
            return 0;
    return 1;
}

int main()
{

    ifstream f("ciclueuler.in");

    f>>n>>m;
    for(int i=1; i<=m; i++)
    {
        f>>v[i].x>>v[i].y;
        graf[v[i].x].push_back(i);
        graf[v[i].y].push_back(i);
        fr[v[i].x]++;
        fr[v[i].y]++;
    }
    if(!even())
    {
        g<<-1;
        return 0;
    }
    ciclu();
    return 0;
}