Cod sursa(job #3001909)

Utilizator Vali_nnnValentin Nimigean Vali_nnn Data 14 martie 2023 02:54:16
Problema Ciclu Eulerian Scor 100
Compilator cpp-64 Status done
Runda Arhiva educationala Marime 1.05 kb
#include <fstream>
#include <stack>
#include <vector>
using namespace std;
ifstream f("ciclueuler.in");
ofstream g("ciclueuler.out");
vector <int> v[100011];
stack <int> q;
bool fost[500011];
int n,m,i,x,y,from[500011],to[500011],k,p,kanye;
int main()
{
    f>>n>>m;
    for(i=1;i<=m;i++)
    {
        f>>x>>y;
        v[x].push_back(i);
        v[y].push_back(i);
        from[i]=x;
        to[i]=y;
    }
    for(i=1;i<=n;i++)
        if(v[i].size()%2==1)
    {
        g<<-1;
        return 0;
    }
    q.push(1);
    while(q.empty()==false)
    {
        k=q.top();
        if(v[k].empty()==false)
        {
            kanye=v[k].back();
            v[k].pop_back();
            if(fost[kanye]==0)
            {
                fost[kanye]=1;
                if(from[kanye]==k)
                    p=to[kanye];
                else
                    p=from[kanye];
                q.push(p);
            }
        }
        else
        {
            q.pop();
            g<<k<<" ";
        }
    }
    return 0;
}