Cod sursa(job #2905639)

Utilizator sichetpaulSichet Paul sichetpaul Data 22 mai 2022 20:38:02
Problema Ciclu Eulerian Scor 30
Compilator cpp-64 Status done
Runda Arhiva educationala Marime 0.87 kb
#include <bits/stdc++.h>
#define Nmax 100005
#define Mmax 400005
using namespace std;

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

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

int nxt(int node) {
    return G[node].back().first;
}
int edge(int node) {
   return G[node].back().second;
}

void Euler(int node) {
   ans.push_back(node);
   while (!G[node].empty() && vis[edge(node)])
        G[node].pop_back();

   if (G[node].empty()) return;
   int node2 = nxt(node);
   vis[edge(node)] = 1;
   G[node].pop_back();
   Euler(node2);
}
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});
    }

    Euler(1);
    for (auto it: ans)
        fout << it << " ";

    return 0;
}