Cod sursa(job #1293596)

Utilizator alexb97Alexandru Buhai alexb97 Data 16 decembrie 2014 08:59:09
Problema Ciclu Eulerian Scor 0
Compilator cpp Status done
Runda Arhiva educationala Marime 1.2 kb
#include <fstream>
#include <vector>
#include <bitset>

#define MaxN 100050
#define MaxM 500050
using namespace std;

ifstream is("cicluleuler.in");
ofstream os("cicluleuler.out");


struct Edge {
	int x, y;
};

int n, m;
vector<vector<int>> g;
vector<int> cycle;
Edge e[MaxM];
bitset<MaxM> viz;

int IsEuler();
void Dfs(int x);

int main()
{
    is >> n;
    is >> m;
    int x, y;
    g= vector<vector<int>>(MaxN);
    for(int i = 1; i <= m; ++i)
    {
        is >> x >> y;
        g[x].push_back(i);
        g[y].push_back(i);
        e[i] = {x, y};

    }
    if(IsEuler())
    {
        Dfs(1);
        for(size_t i = 0; i < cycle.size() - 1; ++i)
            os << cycle[i] << ' ';
        os << '\n';

    }
    else
            os << -1;
    is.close();
    os.close();
    return 0;
}

int IsEuler()
{
    for(int i = 1; i <= n; ++i)
        if(g[i].size() & 1) return 0;
    return 1;
}

void Dfs(int x)
{
    while(g[x].size())
    {
        if(viz[g[x].back()])
        {
            g[x].pop_back();
            continue;
        }
        viz[g[x].back()] = 1;
        Dfs(e[g[x].back()].x + e[g[x].back()].y - x);
    }
    cycle.push_back(x);
}