Cod sursa(job #1341497)

Utilizator Darius15Darius Pop Darius15 Data 12 februarie 2015 19:50:33
Problema Ciclu Eulerian Scor 80
Compilator cpp Status done
Runda Arhiva educationala Marime 1.22 kb
#include <fstream>
#include <bitset>
#include <vector>
#include <queue>
#include <stack>
using namespace std;
ifstream f("ciclueuler.in");
ofstream g("ciclueuler.out");
int n,m,i,j,x,y;
bool ok;
bitset <100001> viz;
vector <int> v[100001];
queue <int> q;
stack <int> st;
void ciclueulerian()
{
    int e,nu,sel;
  st.push(1);
  while(!st.empty())
  {
    e=st.top();
    if (v[e].size()>0)
    {
        nu=v[e].front();
        st.push(nu);
        v[e].erase(v[e].begin());
        for (j=0;j<v[nu].size();j++)
            if (v[nu][j]==e) {sel=j;break;}
        v[nu].erase(v[nu].begin()+sel);
    }
    else
    {
        g<<e<<' ';
       st.pop();
    }
  }
}
int main()
{
    f>>n>>m;
    for (i=1;i<=m;i++)
    {
        f>>x>>y;
        v[x].push_back(y);
        v[y].push_back(x);
    }
    q.push(1);
    while(!q.empty())
    {
        x=q.front();
        q.pop();
        viz[x]=true;
        for (j=0;j<v[x].size();j++)
        if (viz[v[x][j]]==false)
         q.push(v[x][j]);
    }
    ok=true;
    for (j=1;j<=n;j++)
        if ((viz[j]==false) || (v[j].size()%2==1))
                ok=false;
    if (ok==false)
    g<<-1;
    else ciclueulerian();
    return 0;
}