Cod sursa(job #970686)

Utilizator alex_ovidiunituAlex Ovidiu Nitu alex_ovidiunitu Data 7 iulie 2013 16:07:38
Problema Ciclu Eulerian Scor 50
Compilator cpp Status done
Runda Arhiva educationala Marime 1.47 kb
#include <iostream>
#include <fstream>
#include <queue>
#include <vector>
#include <bitset>
#include <algorithm>
using namespace std;
int n,m,grad[100005];
vector <int> v[100005];
bitset <100005> apar;
fstream f,g;
void bfs (int vf)
{
    queue<int> q;
    q.push(vf);
    int i;
    while (!q.empty())
    {
        vf=q.front();
        q.pop();
        apar[vf]=1;
        for (i=0;i<v[vf].size();i++)
            if (apar[v[vf][i]]==0)
                q.push(v[vf][i]);
    }

}
void sterge(int a,int vf)
{
    int i;
    for (i=0;i<v[a].size();i++)
        if (v[a][i]==vf)
            break;
    if (i!=v[a].size())
        v[a].erase(v[a].begin()+i,v[a].begin()+i+1);
}
void euler (int vf)
{
    int i,vfcrt;
    for (i=0;i<v[vf].size();i++)
    {
        vfcrt=v[vf][i];
        v[vf].erase(v[vf].begin()+i,v[vf].begin()+1+i);
        sterge(vfcrt,vf);
        euler(vfcrt);
    }
    g<<vf<<" ";
}
int main()
{
    int i,x,y;
    f.open("ciclueuler.in",ios::in);
    g.open("ciclueuler.out",ios::out);
    f>>n>>m;
    for (i=1;i<=m;i++)
    {
        f>>x>>y;
        grad[x]++;
        grad[y]++;
        v[x].push_back(y);
        v[y].push_back(x);
    }
    bfs(1);
    for(i=1;i<=n;i++)
        if (apar[i]==0 || grad[i]%2==1)
        {
            g<<-1;
            return 0;
        }
     for(i=1;i<=n;i++)
        if (apar[i]==0)
        {
            g<<-1;
            return 0;
        }
    euler(1);
}