Cod sursa(job #2900257)

Utilizator sichetpaulSichet Paul sichetpaul Data 10 mai 2022 17:11:23
Problema Ciclu Eulerian Scor 30
Compilator cpp-64 Status done
Runda Arhiva educationala Marime 0.89 kb
#include <bits/stdc++.h>
#define Nmax 200005
#define Mmax 500005
using namespace std;


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

void Euler(int node) {
    ans.push_back(node);
    while (G[node].size() > 0 && vis[G[node].back().second]) /// caut o muchie pe care sa continui traseul
        G[node].pop_back();

    if (G[node].size() == 0) return;
    int nxt = G[node].back().first, edge = G[node].back().second;
    G[node].pop_back();
    vis[edge] = 1;
    Euler(nxt);
}
int main()
{
    ifstream fin("ciclueuler.in");
    ofstream fout("ciclueuler.out");

    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);
    ans.pop_back();
    for (auto it: ans)
        fout << it << " ";


    return 0;
}