Cod sursa(job #2576335)

Utilizator sichetpaulSichet Paul sichetpaul Data 6 martie 2020 18:37:31
Problema Ciclu Eulerian Scor 50
Compilator cpp-64 Status done
Runda Arhiva educationala Marime 0.89 kb
#include <bits/stdc++.h>
#define Nmax 100005
using namespace std;

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

int N, M;
vector<pair<int, int> > G[Nmax];
vector<int> cycle;
bool vis[Nmax];

void Euler(int node) {
   while (!G[node].empty()) {
        pair<int, int> curr = G[node].back();
        G[node].pop_back();
        if (vis[curr.second] == 0) {
            vis[curr.second] = 1;
            Euler(curr.first);
        }
   }
     cycle.push_back(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 << '\n';
          return 0;
    }

    Euler(1);
    cycle.pop_back();
    for (auto it: cycle)
        g << it << " ";

    return 0;
}