Cod sursa(job #1753881)

Utilizator coteanusebastianCoteanu coteanusebastian Data 7 septembrie 2016 11:47:06
Problema Ciclu Eulerian Scor 30
Compilator cpp Status done
Runda Arhiva educationala Marime 1.17 kb
#include <fstream>
#include <vector>
#include <stack>
#include <list>
using namespace std;
ifstream f("ciclueuler.in");
ofstream g("ciclueuler.out");
int n,m,x,y,ok = 1;
vector <int> G[100007];
vector <int>::iterator it;
stack <int> S;
list <int> Q;
void erasePoint(int t,int s)
{
    for(it = G[t].begin(); it != G[t].end(); ++it)
    {
        if(*it == s)
        {
            G[t].erase(it);
            break;
        }
    }
}
void DFS()
{
    int nod, vecin;
    while(!S.empty())
    {
        nod = S.top();
       // g<<nod<<'\n';
        if(G[nod].empty())
        {
            S.pop();
            Q.push_back(nod);
        }
        else {
            vecin = G[nod].back();
           // g << nod << " " << vecin << '\n';
            erasePoint(nod,vecin);
            erasePoint(vecin,nod);
            S.push(vecin);
        }
    }
}

int main()
{
    f>>n>>m;
    while(m--)
    {
        f>>x>>y;
        G[x].push_back(y);
        G[y].push_back(x);
    }
    S.push(1);
    DFS();
    while(Q.size() != 2){
        g << Q.front() << " ";
        Q.pop_front();
    }
    g << Q.front() << '\n';
    return 0;
}