Cod sursa(job #2206110)

Utilizator andrei32576Andrei Florea andrei32576 Data 21 mai 2018 10:58:58
Problema Ciclu Eulerian Scor 100
Compilator cpp Status done
Runda Arhiva educationala Marime 1.34 kb
#include <fstream>
#include <set>
#include <vector>
#include <stack>
using namespace std;

int n,m,i,x,y;
vector<int> G[100005];
bool usedEdge[500005];
int from[500005],to[500005];

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

bool posibil()
{
    for(int i=1;i<=n;i++)
        if(G[i].size()%2==1) return 0;
    return 1;
}


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

    if(posibil())
    {

        vector<int> ans;
        vector<int> stk;
        stk.push_back(1);

        while (!stk.empty())
        {
            int node = stk.back();
            if (!G[node].empty())
            {
                int e = G[node].back();
                G[node].pop_back();
                if (!usedEdge[e])
                {
                    usedEdge[e] = true;
                    int to = ::from[e] ^ ::to[e] ^ node;
                    stk.push_back(to);
                }
            }
            else
            {
                stk.pop_back();
                ans.push_back(node);
            }
        }

        for (i=0;i<ans.size()-1;i++)
            g<<ans[i]<<" ";
    }
    else
        g<<-1;


    f.close();
    g.close();
    return 0;
}