Cod sursa(job #2576329)

Utilizator sichetpaulSichet Paul sichetpaul Data 6 martie 2020 18:36:05
Problema Ciclu Eulerian Scor 0
Compilator cpp-64 Status done
Runda Arhiva educationala Marime 0.94 kb
#include <bits/stdc++.h>
#define Nmax 100005
#define Mmax 500005
#define pb push_back
#define nxt first
#define edge second
using namespace std;

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

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

void Euler(int node) {
    while (!G[node].empty()) {
        pair<int,int> curr = G[node].back();
        G[node].pop_back();

        if (!vis[curr.edge]) {
            vis[curr.edge] = 1;
            Euler(curr.nxt);
        }
    }
    ans.push_back(node);
}
int main()
{
    f >> N >> M;
    for (int i = 1; i <= M; ++i) {
        int x, y;
        f >> x >> y;
        G[x].pb({y, i});
        G[y].pb({x, i});
    }

 /*   for (int i = 1; i <= N; ++i)
    if (G[i].size() % 2) {
        g << -1;
        return 0;
    }*/

    Euler(0);
    ans.pop_back();
    for (auto it: ans)
        g << it << " ";

    return 0;
}