Cod sursa(job #2242798)

Utilizator andreistanStan Andrei andreistan Data 19 septembrie 2018 16:03:56
Problema Ciclu Eulerian Scor 0
Compilator cpp Status done
Runda Arhiva educationala Marime 1.19 kb
#include <iostream>
#include <fstream>
#include <vector>
#include <stack>
using namespace std;

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

const int NMAX = 100000;

int N, k;

vector<int> G[NMAX + 1], L;
vector<pair<int, int> >M;
vector<bool> elim;
stack<int> S;

void euler()
{
    S.push(1);
    while(!S.empty())
    {
        int v = S.top();
        if(G[v].size() > 0)
        {
            int x = G[v].back();
            G[v].pop_back();
            if(!elim[x])
            {
                elim[x] = 1;
                int p = M[x].second;
                if(p == v)
                    p = M[x].first;
                S.push(p);
            }
        }
        else
        {
            L.push_back(v);
            S.pop();
        }
    }
}

int main()
{
    int x, y;
    int m;
    f >> N >> m;
    while(f >> x >> y)
    {
        M.push_back({x, y});
        elim.push_back(0);
        G[x].push_back(M.size() - 1); // indicele muchiei
        G[y].push_back(M.size() - 1);
    }
    euler();
    //g << L.size() << '\n';
    for(vector<int>::iterator i = L.begin() ; i+1 != L.end(); i++)
        g << *i << ' ';
    return 0;
}