Cod sursa(job #2560721)

Utilizator BogdanGhGhinea Bogdan BogdanGh Data 28 februarie 2020 11:07:14
Problema Ciclu Eulerian Scor 80
Compilator cpp-64 Status done
Runda Arhiva educationala Marime 1.08 kb
#include <bits/stdc++.h>

using namespace std;
ofstream g("ciclueuler.out");
list <int >v[100005];
int n,m,x,y,i,buff_size=1<<15+1,pos,nr;
char buff[1<<15+1];
inline void read(int &nr)
{
    while(buff[pos] < '0' || buff[pos] > '9')
        if(++pos == buff_size)
            fread(buff,  1,buff_size, stdin), pos = 0;
    nr = 0;
    while('0' <= buff[pos] && buff[pos] <= '9')
    {
        nr = nr * 10 + buff[pos] - '0';
        if(++pos == buff_size)
            fread(buff, 1, buff_size, stdin), pos = 0;
    }
}
void dfs(int nod)
{
    int x;
    while(!v[nod].empty())
    {
        x=v[nod].front();
        v[nod].pop_front();
        v[x].erase(find(v[x].begin(),v[x].end(),nod));
        dfs(x);
    }
    g<<nod<<' ';
}
int main()
{
    freopen("ciclueuler.in","r",stdin);
    read(n);
    read(m);
    for(i=1;i<=m;i++)
    {
        read(x);
        read(y);
        v[x].push_back(y);
        v[y].push_back(x);
    }
    for(i=1;i<=n;i++)
        if(v[i].size()&1)
    {
        g<<-1;
        return 0;
    }
    dfs(1);
    return 0;
}