Cod sursa(job #2734337)

Utilizator rapidu36Victor Manz rapidu36 Data 31 martie 2021 18:44:08
Problema Ciclu Eulerian Scor 80
Compilator cpp-64 Status done
Runda Arhiva educationala Marime 1.57 kb
#include <fstream>
#include <vector>
#include <stack>

using namespace std;

const int M = 500001;
const int N = 100001;

struct muchie
{
    int x, y;
    bool folosit;
};

muchie e[M];
vector <int> a[N], c;
int stiva[M];


void euler()
{
    stack <int> stiva;
    stiva.push(1);
    while (!stiva.empty())
    {
        int x = stiva.top();
        bool gasit_muchie = false;
        for (int j = a[x].size() - 1; j >= 0; j--)
        {
            int i = a[x][j];
            a[x].pop_back();
            if (e[i].folosit)
            {
                continue;
            }
            e[i].folosit = true;
            int y = e[i].x + e[i].y - x;
            stiva.push(y);
            gasit_muchie = true;
            break;
        }
        if (!gasit_muchie)
        {
            c.push_back(x);
            stiva.pop();
        }
    }
}

bool toate_pare(int n)
{
    for (int i = 1; i <= n; i++)
    {
        if (a[i].size() % 2 != 0)
        {
            return false;
        }
    }
    return true;
}

int main()
{
    ifstream in("ciclueuler.in");
    ofstream out("ciclueuler.out");
    int n, m;
    in >> n >> m;
    for (int i = 0; i < m; i++)
    {
        in >> e[i].x >> e[i].y;
        a[e[i].x].push_back(i);
        a[e[i].y].push_back(i);
    }
    in.close();
    euler();
    if (toate_pare(n))
    {
        c.erase(c.begin() + m);
        for (auto x: c)
        {
            out << x << " ";
        }
    }
    else
    {
        out << -1;
    }
    out.close();
    return 0;
}