Cod sursa(job #2156616)

Utilizator mlc_oficialBoris Barca mlc_oficial Data 8 martie 2018 21:05:10
Problema Ciclu Eulerian Scor 80
Compilator cpp Status done
Runda Arhiva educationala Marime 0.96 kb
#include <fstream>
#include <algorithm>
#include <vector>
using namespace std;

const int kMaxN = 1e5, kMaxM = 5e5;

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

vector<int> graph[kMaxN];
int edge_nodes[kMaxM];
bool used_edge[kMaxM];

void Df(const int node) {
    for (auto&& edge : graph[node]) {
        if (used_edge[edge]) {
            continue;
        }
        
        used_edge[edge] = true;
        Df(edge_nodes[edge] ^ node);
        cout << node + 1 << ' ';
    }
}

int main() {
    int n, m; cin >> n >> m;
    for (int i = 0; i < m; ++i) {
        int x, y; cin >> x >> y; --x; --y;
        graph[x].push_back(i);
        graph[y].push_back(i);
        edge_nodes[i] = x ^ y;
        used_edge[i] = false;
    }
    
    if (any_of(graph, graph + n, [](const vector<int>& adj_list) 
            { return adj_list.size() % 2 == 1; } )) {
        cout << -1 << endl;
        return 0;
    }
    
    Df(0);
}