Cod sursa(job #1604536)

Utilizator alexmisto342Turdean Alexandru alexmisto342 Data 18 februarie 2016 13:14:12
Problema Ciclu Eulerian Scor 0
Compilator cpp Status done
Runda Arhiva educationala Marime 1.08 kb
#include <fstream>
#include <vector>
#include <string>
#include <algorithm>
#include <stack>
#include <queue>
#define inf 1<<30
#define x first
#define y second
using namespace std;
ifstream fin("ciclueuler.in");
ofstream fout("ciclueuler.out");
stack <pair<int,int> > st;
vector <pair<int,int> > v[100050];
int muchii[500050];
int i,a,b,j,n,m,maxi,l,k,lungime;
int main()
{
    fin >> n >> m;
    for(i = 1; i <= m; i++)
    {
        fin >> a >> b;
        v[a].push_back(make_pair(b,i));
        v[b].push_back(make_pair(a,i));
    }
    st.push(make_pair(1,0));
    while(st.size())
    {
        int ok = 1;
        for(;st.top().y < v[st.top().x].size() && ok; st.top().y++)
            if(!muchii[v[st.top().x][st.top().y].y])
            {
                ok = 0;
                muchii[v[st.top().x][st.top().y].y] = 1;
                a=st.top().x;
                st.push(make_pair(v[st.top().x][st.top().y].x,0));
            }
        if(ok && st.size() > 1)
            fout<<a<<" ";
        if(ok)
            st.pop();
    }

    return 0;
}