Cod sursa(job #2905873)

Utilizator sichetpaulSichet Paul sichetpaul Data 24 mai 2022 09:06:34
Problema Ciclu Eulerian Scor 50
Compilator cpp-64 Status done
Runda Arhiva educationala Marime 0.92 kb
#include <bits/stdc++.h>
#define Nmax 100005
#define Mmax 500005
using namespace std;

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

int N, M;
vector<pair<int, int> > G[Nmax];
vector<int> ans;
bool vis[Mmax];
int main()
{
    fin >> N >> M;
    for (int i = 1; i <= M; ++i) {
        int x, y;
        fin >> 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) {
        fout << -1;
        return 0;
    }

       int u = 1;
   while (1) {
       ans.push_back(u);
       while (!G[u].empty() && vis[G[u].back().second])
            G[u].pop_back();

       if (G[u].empty()) break;
       int v = G[u].back().first;
       int edge = G[u].back().second;
       vis[edge] = 1;
       u = v;
   }

    ans.pop_back();
    for (auto it: ans)
        fout << it << " ";


    return 0;
}