Cod sursa(job #2559495)

Utilizator s.gabi7Dumitrescu Daniel s.gabi7 Data 27 februarie 2020 12:45:20
Problema Ciclu Eulerian Scor 100
Compilator cpp-64 Status done
Runda Arhiva educationala Marime 0.87 kb
#include <bits/stdc++.h>
#define N 500005
using namespace std;
bitset <N> seen;
array <vector <pair <int, int>>, N> G;
vector <int> S;

void DFS (int x) {
    while (!G[x].empty()) { //SA NU STERGI AFGIDS89YUU7GIS89DHIU
        auto curr=G[x].back();
        G[x].pop_back();
        if (!seen[curr.first])
            seen[curr.first]=1,
            DFS(curr.second);
    }
    S.push_back(x);
}
int main () {
    ifstream fin ("ciclueuler.in");
    ofstream fout ("ciclueuler.out");
    int n, m, i, j, k;
    fin >> n >> m;
    for (k=0; k<m; k++) {
        fin >> i >> j;
        G[i].emplace_back(make_pair(k, j));
        G[j].emplace_back(make_pair(k, i));
    }

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

    DFS(i);
    for (auto i:S)
        fout << i << ' ';
    return 0;
}