Cod sursa(job #1640240)

Utilizator serbanSlincu Serban serban Data 8 martie 2016 16:37:26
Problema Ciclu Eulerian Scor 60
Compilator cpp Status done
Runda Arhiva educationala Marime 0.94 kb
#include <fstream>
#include <vector>
#include <stack>
#define oo 1111111

using namespace std;

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

bool vm[500005];
vector< pair<int, int> > L[100005];
vector< pair<int, int> > M[500005];
stack<int> s;

int main()
{
    int n, m, x, y; f >> n >> m;
    for(int i = 0; i < m; i ++) {
        f >> x >> y;
        L[x].push_back({y, i});
        L[y].push_back({x, i});
        M[i].push_back({x, y});
    }

    s.push(1);
    while(!s.empty()) {
        int top = s.top(), i;
        for(i = 0; i < L[top].size(); i ++) {
            int cur = L[top][i].first;
            int muc = L[top][i].second;
            if(!vm[muc]) s.push(cur), vm[muc] = true, i = oo;
        }
        if(i != oo + 1) {
            if(s.size() == 1) s.pop();
            else {
                g << top << " ";
                s.pop();
            }
        }
    }
    return 0;
}