Cod sursa(job #1377950)

Utilizator maribMarilena Bescuca marib Data 6 martie 2015 09:35:10
Problema Ciclu Eulerian Scor 100
Compilator cpp Status done
Runda Arhiva educationala Marime 1.65 kb
#include <fstream>
#include <vector>
#include <queue>
#include <stack>
#define DIM 100002

using namespace std;


ifstream in("ciclueuler.in");
ofstream out("ciclueuler.out");

vector <int> node[DIM];

queue <int> q;

stack <int> st;

int vis[DIM], n, m;

void sterge(int x, int y)
{
    for(int j=0; j<node[x].size(); ++j)
    {
        if(node[x][j]==y)
        {
            node[x].erase(node[x].begin()+j);
            break;
        }
    }
}

void euler()
{
    int next, vfc;
    st.push(1);
    while(!st.empty())
    {
        vfc=st.top();
        if(node[vfc].size())
        {
            next=node[vfc][0];
            sterge(next, vfc);
            node[vfc].erase(node[vfc].begin());
            st.push(next);
            continue;
        }
        st.pop();
        if(!st.empty())
            out<<vfc<<" ";
    }
}

int check()
{
    int vfc, visited=0;
    q.push(1);
    vis[1]=1;
    while(!q.empty())
    {
        vfc=q.front();
        if(node[vfc].size()%2==0)
            visited++;
        q.pop();
        for(int i=0; i<node[vfc].size(); ++i)
        {
            if(vis[node[vfc][i]]==0)
            {
                vis[node[vfc][i]]=1;
                q.push(node[vfc][i]);
            }
        }
    }
    if(visited==n)
        return 1;
    else return 0;
}

int main()
{
    in>>n>>m;
    while(m--)
    {
        int a, b;
        in>>a>>b;
        node[a].push_back(b);
        node[b].push_back(a);
    }
    if(check())
    {
        euler();
    }
    else
        out<<"-1";
    out<<"\n";
    in.close();
    out.close();
    return 0;
}