Cod sursa(job #2870696)

Utilizator anastasei_tudorAnastasei Tudor anastasei_tudor Data 12 martie 2022 15:10:25
Problema Ciclu Eulerian Scor 100
Compilator cpp-64 Status done
Runda Arhiva educationala Marime 1.52 kb
#include <bits/stdc++.h>
#define NMAX 100004
#define MMAX 500004

using namespace std;
ifstream fin ("ciclueuler.in");
ofstream fout ("ciclueuler.out");

struct Muchie
{
    int x, y;
    bool viz;
};
Muchie LM[MMAX];

int n, m, d[NMAX];
vector <int> G[NMAX], sol;

void citire();
bool verificare();
void euler(int start);

int main()
{
    citire();
    if (verificare() == 0)
    {
        fout << -1;
        return 0;
    }
    euler(1);
    for (int i = 0; i < sol.size() - 1; i++)
        fout << sol[i] << ' ';
    return 0;
}

void citire()
{
    int x, y;
    fin >> n >> m;
    for (int i = 1; i <= m; i++)
    {
        fin >> x >> y;
        LM[i].x = x;
        LM[i].y = y;
        G[x].push_back(i);
        G[y].push_back(i);
        d[x]++;
        d[y]++;
    }
}

void euler(int start)
{
    stack <int> S;
    S.push(start);

    while (!S.empty())
    {
        int nod = S.top();
        if (G[nod].size() == 0)
        {
            S.pop();
            sol.push_back(nod);
        }
        else
        {
            int edge = G[nod].back();
            G[nod].pop_back();
            if (LM[edge].viz == 0)
            {
                LM[edge].viz = 1;
                if (nod == LM[edge].x)
                    S.push(LM[edge].y);
                else
                    S.push(LM[edge].x);
            }
        }
    }
}

bool verificare()
{
    for (int i = 1; i <= n; i++)
        if (d[i] % 2)
            return 0;
    return 1;
}