Cod sursa(job #1377482)

Utilizator Al3ks1002Alex Cociorva Al3ks1002 Data 5 martie 2015 22:09:24
Problema Ciclu Eulerian Scor 100
Compilator cpp Status done
Runda Arhiva educationala Marime 1.75 kb
#include<cstdio>
#include<fstream>
#include<iostream>
#include<iomanip>
#include<algorithm>
#include<vector>
#include<bitset>
#include<deque>
#include<queue>
#include<set>
#include<map>
#include<cmath>
#include<cstring>
#include<ctime>
#include<cstdlib>
#include<unordered_map>

#define ll long long
#define pb push_back
#define mp make_pair
#define pii pair<int,int>
#define pll pair<ll,ll>
#define all(x) (x).begin(), (x).end()
#define fi first
#define se second

using namespace std;

const int nmax = 100005;

int n, m, x, y, i;

vector<int> st, v[nmax];
bitset<nmax> viz;

void dfs(int x)
{
    viz[x] = 1;
    for(auto it : v[x])
        if(!viz[it])
            dfs(it);
}

void erase_edge(int x, int y)
{
    for(auto &it : v[x])
        if(it == y)
        {
            it = v[x].back();
            v[x].pop_back();
            return;
        }
}

int main()
{
    freopen("ciclueuler.in", "r", stdin);
    freopen("ciclueuler.out", "w", stdout);

    scanf("%d%d", &n, &m);

    for(; m; m--)
    {
        scanf("%d%d", &x, &y);
        v[x].pb(y);
        v[y].pb(x);
    }

    for(i = 1; i <= n; i++)
        if(v[i].size() & 1)
        {
            printf("-1\n");
            return 0;
        }

    dfs(1);

    for(i = 1; i <= n; i++)
        if(!viz[i])
        {
            printf("-1\n");
            return 0;
        }

    st.pb(1);
    while(!st.empty())
    {
        x = st.back();

        if(!v[x].empty())
        {
            y = v[x].back();
            st.pb(y);
            erase_edge(x, y);
            erase_edge(y, x);
        }
        else
        {
            printf("%d ", x);
            st.pop_back();
        }
    }

    return 0;
}