Cod sursa(job #2451603)

Utilizator ciutanpCiuta Andrei Calin ciutanp Data 27 august 2019 13:11:05
Problema Ciclu Eulerian Scor 100
Compilator cpp-64 Status done
Runda repost Marime 0.93 kb
#include<bits/stdc++.h>
using namespace std;

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

vector< pair< int, int > >G[ 500002 ];
bitset< 50000002 > viz;

int n, m, nr, ras[ 5000002 ];

void veri(int node)
{
    while( G[node]. size() != 0 )
    {
        int vec = G[node].back().first;
        int poz = G[node].back().second;
        G[node].pop_back();
        if(!viz[poz])
        {
            viz[poz] = 1;
            veri(vec);
        }
    }
    ras[ ++nr ] = node;
}

int main()
{
    f >> n >> m;
    for(int i = 1 ; i <= m ; ++i)
    {
        int x, y;
        f >> x >> y;
        G[ x ].push_back( {y, i} );
        G[ y ].push_back( {x, i} );
    }
    for(int i = 1 ; i <= n ; ++i)
    {
        if(G[i].size() % 2)
        {
            g << -1;
            return 0;
        }
    }
    veri(1);
    for(int i = 1 ; i < nr ; ++i)
        g << ras[ i ] << ' ';
}