Cod sursa(job #3259210)

Utilizator PsyDuck1914Feraru Rares-Serban PsyDuck1914 Data 25 noiembrie 2024 15:59:39
Problema Ciclu Eulerian Scor 0
Compilator cpp-64 Status done
Runda Arhiva educationala Marime 1.07 kb
#include <bits/stdc++.h>

using namespace std;

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

const int NMAX = 1e5;
const int MMAX = 5e5;

struct muchie{
  
  int id, to;
    
};

vector<muchie> adj[NMAX+1];

int n, m;

bool deleted[MMAX+1];

vector<int> rez;

int nextEdge(muchie edge){
    
    while(!adj[edge.to].empty() and deleted[adj[edge.to].back().id])
        adj[edge.to].pop_back();
        
    if(adj[edge.to].empty())
        return -1;
    
    deleted[adj[edge.to].back().id] = true;
    
    return adj[edge.to].back().to;
    
}

void euler(int nod){
    
    for(auto edge : adj[nod]){
        
        int vecin = nextEdge(edge);
        
        if(vecin != -1)
            euler(vecin);
        
    }
    
    rez.push_back(nod);
    
}

int main()
{
    
    f >> n >> m;
    
    for(int i=1; i<=m; i++){
        
        int x, y;
        f >> x >> y;
        
        adj[x].push_back({i, y});
        adj[y].push_back({i, x});
        
    }
    
    euler(1);
    
    for(int i=0; i<rez.size(); i++)
        g << rez[i] << " ";

    return 0;
}