Cod sursa(job #2573469)

Utilizator natrovanCeval Marius natrovan Data 5 martie 2020 17:44:50
Problema Ciclu Eulerian Scor 0
Compilator cpp-64 Status done
Runda Arhiva educationala Marime 1.52 kb
#include <iostream>
#include <fstream>
#include <vector>
#include <queue>
#include <stack>

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

int N, M, x, y;
vector<int>graf[100];
stack<int>s;
stack<int>snou;
vector<int>grafNou[100];
int used[100];


void scoate(int x, int v)
{
    vector<int>::iterator it;

    for(it = graf[x].begin(); it != graf[x].end(); it++)
        if(*it == v)
        {
            graf[x].erase(it);
            --used[x];
            break;
        }

    for(it = graf[v].begin(); it != graf[v].end(); it++)
        if(*it == x)
        {
            graf[v].erase(it);
            --used[v];
            break;
        }
}

void eulerDFS(int nod){
    int nextNode = graf[nod][0];
    scoate(nod, nextNode);
    s.push(nextNode);
    eulerDFS(nextNode);
}

int main()
{
    fin >> N >> M;
    for(int i = 1; i <= M; i++){
        fin >> x >> y;
        if(x != y){
            graf[x].push_back(y);
            graf[y].push_back(x);
            used[x]++;used[y]++;
        }else{
            graf[x].push_back(x);
            used[x]++;
        }
    }

    s.push(1);
    eulerDFS(1);

    while(!s.empty()){
        int nodCurent = s.top();
        if(used[nodCurent]){
            eulerDFS(nodCurent);
        }
        else{
            s.pop();
            snou.push(nodCurent);
        }
    }

    while(!snou.empty()){
        fout << snou.top() << ' ';
        snou.pop();
    }

    return 0;
}