Cod sursa(job #2571017)

Utilizator vmnechitaNechita Vlad-Mihai vmnechita Data 4 martie 2020 20:29:25
Problema Ciclu Eulerian Scor 80
Compilator cpp-64 Status done
Runda Arhiva educationala Marime 0.93 kb
#include <bits/stdc++.h>
#define NMAX 100005

using namespace std;

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

struct A
{
    int ind, dest;
};

vector < A > v[NMAX];
bitset < 5 * NMAX > viz;

void dfs ( int nod );

int main()
{
    int n, m, i, x, y;

    fin >> n >> m;
    for ( i = 1 ; i <= m ; i++ )
    {
        fin >> x >> y;
        v[x].push_back( { i, y } );
        v[y].push_back( { i, x } );
    }

    for ( i = 1 ; i <= n ; i++ )
        if ( v[i].size() % 2 == 1 )
        {
            fout << -1;
            return 0;
        }

    dfs ( 1 );

    return 0;
}

void dfs ( int nod )
{
    vector < A > :: iterator it;
    A x;

    for ( it = v[nod].begin() ; it != v[nod].end() ; it++ )
    {
        x = *it;

        if ( viz[x.ind] == 0 )
        {
            viz[x.ind] = 1;
            dfs ( x.dest );
        }
    }

    fout << nod << ' ';
}