Cod sursa(job #2242021)

Utilizator mirelPmirel p mirelP Data 17 septembrie 2018 16:32:23
Problema Ciclu Eulerian Scor 60
Compilator cpp Status done
Runda Arhiva educationala Marime 1.55 kb
#include <fstream>
#include <stack>
#include <vector>
#include <algorithm>

using namespace std;
ifstream fin("ciclueuler.in");
ofstream fout("ciclueuler.out");
vector<int>G[100005];
vector<int>::iterator it;
stack<int>st;
int n,m,i,j,k,l,r,x,y,s,smax,viz[100005];
bool ok;
void dfs(int x)
{
    int q;
    viz[x]=1;
    for(int v=0;v<G[x].size();v++)
    {
        q=G[x][v];
        if(!viz[q])
        {
            dfs(q);
        }
    }
}
void euler(int node)
{
    int other;

    while(!G[node].empty())
    {
        st.push(node);
        other=G[node].back();
       // it=find(G[other].begin(),G[other].end(),node);
        G[other].erase(find(G[other].begin(),G[other].end(),node));
        G[node].pop_back();
      //  euler(other);
        node=other;
    }



}
int main()
{
    fin>>n>>m;
    for(i=1;i<=m;i++)
    {
        fin>>x>>y;
        G[x].push_back(y);
        G[y].push_back(x);

    }

            dfs(1);
            for(i=1;i<=n;i++)
            if(!viz[i])
                ok=1;
        //for(i=1;i<=n;i++)
          //  fout<<viz[i]<<" ";
    if(!ok)
    {
        //st.push(1);
        euler(1);
        fout<<1<<" " ;
        int node=1;

       do
        {
            euler(node);
            node=st.top();
            fout<<node<<" ";
            st.pop();
            /*
            if(G[st.top()].size()>0)
                euler(st.top());*/
        }
        while(st.size()>1);

    }
    else
        fout<<-1;
    // fout<<st.size();

    return 0;
}