Cod sursa(job #2215199)

Utilizator AlexandruabcdeDobleaga Alexandru Alexandruabcde Data 21 iunie 2018 11:53:56
Problema Ciclu Eulerian Scor 60
Compilator cpp Status done
Runda Arhiva educationala Marime 1.92 kb
#include <bits/stdc++.h>

using namespace std;

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

vector <int> :: iterator it;
vector <int> G[100002];
list <int> Q;

const int bsize = (1 << 16);

char ch[bsize+1];

int n, m, x, y, nr, pos;

bool ok = true;

void read (int &x)
{
    x = 0;
    while (!isdigit(ch[pos]))
    {
        pos++;
        if (pos == bsize)
        {
            pos = 0;
            fread(ch,1,bsize,stdin);
        }
    }
    while (isdigit(ch[pos]))
    {
        x = x * 10 + ch[pos] - '0';
        pos++;
        if (pos == bsize)
        {
            pos = 0;
            fread(ch,1,bsize,stdin);
        }
    }
}

int main()
{
    ios_base :: sync_with_stdio (false);
    freopen("ciclueuler.in","r",stdin);
    freopen("ciclueuler.out","r",stdout);
    pos = 0;
    fread(ch,1,bsize,stdin);
    read(n);
    read(m);

    for (int i = 1; i <= m; i++)
    {
        read(x);
        read(y);
        G[x].push_back(y);
        G[y].push_back(x);
    }

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

    if (ok == false) cout << "-1";
    else
    {

        Q.push_back(1);
        while (!Q.empty())
        {
            x = Q.front();
            if (G[x].size() == 0)
            {
                if (nr < m)
                {
                    g << x << " ";
                    nr++;
                }
                Q.pop_front();
                continue;
            }

            y = G[x].back();
            Q.push_front(y);
            G[x].pop_back();
            for (it = G[y].begin(); it != G[y].end(); it++)
            {
                if (*it == x)
                {
                    G[y].erase(it);
                    break;
                }
            }
        }
    }
    return 0;
}