Cod sursa(job #615294)

Utilizator theodora_maneaManea Theodora Maria theodora_manea Data 9 octombrie 2011 11:44:57
Problema Ciclu Eulerian Scor 100
Compilator cpp Status done
Runda Arhiva educationala Marime 1.44 kb
#include <fstream>
#include <queue>
#include <stack>
#include <string.h>

#define max_n 100001
#define max_m 500001

using namespace std;

int x[max_m],y[max_m],g[max_n],nr[max_n];
vector <int> a[max_n];
bool ok;
queue <int> q;
stack <int> s;
int i,j,n,m,vf,edge;

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

void citire() {
 for (i=1; i<=m; i++)
   {
       in >> x[i] >> y[i];
       a[x[i]].push_back(i);
       a[y[i]].push_back(i);
       g[x[i]]++;
       g[y[i]]++;
   }
}

void euler() {
   s.push(1);
   int u=0;
   bool use[max_m];
   memset(use,false,sizeof(use));
   while (!s.empty())
   {
       vf=s.top();
       while (nr[vf]<g[vf] && vf+u!=m+1)
       {
           edge=a[vf][nr[vf]];
           nr[vf]++;
           if (!use[edge])
           {
               use[edge]=true;
               if (vf==x[edge]) s.push(y[edge]);
               else s.push(x[edge]);
           }
           vf=s.top();
       }
       q.push(s.top());
       s.pop();
   }
   u=q.size();
   if (u<m) out << "-1" << '\n';
   else {
       q.pop();
       while (!q.empty())
       {
           out << q.front() << ' ';
           q.pop();
       }
       out << '\n';
   }

}

int main () {
   in >> n >> m;
   citire();

   ok=true;

   for (i=1; i<=n; i++)
   if (g[i]%2) {
      ok=false;
      break;
    }

    if (ok) euler();
    else out << "-1" << '\n';
    return 0;
}