Cod sursa(job #1980028)

Utilizator tifui.alexandruTifui Ioan Alexandru tifui.alexandru Data 11 mai 2017 22:51:28
Problema Ciclu Eulerian Scor 30
Compilator cpp Status done
Runda Arhiva educationala Marime 1.24 kb
#include <bits/stdc++.h>
#define Nmax 100005
using namespace std;
ifstream f("ciclueuler.in");
ofstream g("ciclueuler.out");
int grd[Nmax];
struct Graph
{
    vector <int> v;
};
Graph G[Nmax];
int sol[Nmax];
stack <int> st;
int N;
void DFS(int nod)
{
    int i,w,x;
    st.push(nod);
    while(!st.empty())
    {
        x=st.top();
        if(G[x].v.size())
        {
            w=G[x].v.back();
            G[x].v.pop_back();
            for(i=0;i<G[w].v.size();i++)
                if(G[w].v[i]==x)
                {
                    swap(G[w].v[i],G[w].v.back());
                    G[w].v.pop_back();
                    i=G[w].v.size();
                }
            st.push(w);
        }
        else
        {
            sol[++N]=x;
            st.pop();
        }
    }
}
int main()
{
    int n,m,i,j,k;
    f>>n>>m;
    for(k=1;k<=m;k++)
    {
        f>>i>>j;
        G[i].v.push_back(j);
        G[j].v.push_back(i);
        grd[i]++;
        grd[j]++;
        if(i==j)
            G[i].v.pop_back();
    }
    for(i=1;i<=n;i++)
        if(grd[i]%2)
        {
            g<<-1;
            return 0;
        }
    DFS(1);
    for(i=1;i<=m;i++)
        g<<sol[i]<<' ';

    return 0;
}