Cod sursa(job #331418)

Utilizator freak93Adrian Budau freak93 Data 13 iulie 2009 21:52:20
Problema Ciclu Eulerian Scor 0
Compilator cpp Status done
Runda Arhiva educationala Marime 1.69 kb
#include<fstream>
#include<queue>
#include<stack>
#include<list>
#include<cstring>

using namespace std;

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

const int maxn=100005;

queue<int>Q;
stack<int>S;

int n,m,i,j,x,y,grade[maxn],answer[maxn],rd;

bool passed[maxn];

list<int>a[maxn];

bool pos()
{
    int k=0,x;
    S.push(1);
    passed[1]=true;
    while(!S.empty())
    {
        x=S.top();
        S.pop();
        ++k;
        for(list<int>::iterator it=a[x].begin();it!=a[x].end();++it)
            if(passed[*it]==false)
                passed[*it]=true,S.push(*it);
    }

    if(k!=n) return 0;
    for(i=1;i<=n;++i)
        if(grade[i]&1)
            return 0;
    return 1;
}

void drop(int x,int y)
{
    --grade[x];
    --grade[y];
    for(list<int>::iterator it=a[x].begin();it!=a[x].end();++it)
        if(*it==y)
        {
            a[x].erase(it);
            break;
        }
}

void go(int x)
{
    int w;
    while(!a[x].empty())
    {
        w=a[x].front();
        a[x].pop_front();
        S.push(x);
        drop(w,x);
        x=w;
    }
}

int main()
{
    f>>n>>m;
    for(i=1;i<=m;++i)
    {
        f>>x>>y;
        a[x].push_back(y);
        a[y].push_back(x);
        ++grade[x];
        ++grade[y];
    }

    if(pos()==0)
    {
        g<<-1<<"\n";
        f.close();
        g.close();

        return 0;
    }

    return 0;
    x=1;

    do
    {
        go(x);
        x=S.top();
        answer[++rd]=S.top();
        S.pop();
    }
    while(!S.empty());

    for(i=rd;i;--i)
        g<<answer[i]<<" ";
    g<<"\n";

    f.close();
    g.close();

    return 0;
}