Cod sursa(job #1292799)

Utilizator Al3ks1002Alex Cociorva Al3ks1002 Data 14 decembrie 2014 19:50:27
Problema Ciclu Eulerian Scor 100
Compilator cpp Status done
Runda Arhiva educationala Marime 1.66 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>

using namespace std;

const int nmax = 100005;

int i, n, m, x, y, g[nmax];

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

void dfs(int x)
{
    viz[x] = 1;

    for(auto it : v[x])
        if(!viz[it])
            dfs(it);
}

bool check()
{
    dfs(1);

    for(i = 1; i <= n; i++)
        if(!viz[i] || (g[i] & 1))
            return 0;

    return 1;
}

void erase(int x, int y)
{
    for(auto &it : v[x])
        if(it == y)
        {
            swap(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);
        g[x]++;
        g[y]++;
    }

    if(!check())
    {
        printf("-1\n");
        return 0;
    }

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

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

    return 0;
}