Cod sursa(job #2375284)

Utilizator circeanubogdanCirceanu Bogdan circeanubogdan Data 8 martie 2019 00:02:19
Problema Ciclu Eulerian Scor 80
Compilator cpp-64 Status done
Runda Arhiva educationala Marime 1.08 kb
#include <fstream>
#include <vector>
#define DIM 100002
#define DIME 500002

using namespace std;

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

int n, m, x, y;

bool viz[DIME];

struct edge{
    int x, y;
    bool visited;
};

vector<edge> edges;

struct edgeGraf{
    int node, index;
};

vector<edgeGraf> graf[DIM];

vector<int> sol;

void dfs(int nod){
    for(auto edg : graf[nod]){
        int nextNode = edg.node;
        int index = edg.index;
        if(viz[index])
            continue;
        viz[index] = true;
        dfs(nextNode);
        sol.push_back(nod);
    }
}

int main()
{
    in>>n>>m;
    for(int i = 1; i <= m; ++ i){
        in>>x>>y;
        edges.push_back({x, y, 0});
        graf[x].push_back({y, edges.size() - 1});
        graf[y].push_back({x, edges.size() - 1});
    }

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

    dfs(1);

    for(auto it : sol)
        out<<it<<" ";
    out<<sol.front();

    return 0;
}