Cod sursa(job #2884193)

Utilizator hurjui12AlexandruHurjui Alexandru-Mihai hurjui12Alexandru Data 2 aprilie 2022 15:55:10
Problema Ciclu Eulerian Scor 100
Compilator cpp-64 Status done
Runda Arhiva educationala Marime 1.37 kb
#include <iostream>
#include <fstream>
#include <vector>
using namespace std;

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

struct muchel {int x, y; bool fol;} much[500001];
vector <int> v[100001];
bool ok[100001];

int s[500001];
int rasp[500001];

void dfs (int x)
{
    int i, y;
    ok[x] = 1;

    for (i = 0; i<v[x].size(); i++)
    {
        y = x ^ much[v[x][i]].x ^ much[v[x][i]].y;
        if (ok[y] == 0)
            dfs (y);
    }
}

int main()
{
    int n, m, i, x, y;

    fin >> n >> m;
    for (i = 1; i<=m; i++)
    {
        fin >> x >> y;
        much[i] = {x, y, 0};
        v[x].push_back (i);
        v[y].push_back (i);
    }

    dfs (1);
    for (i = 1; i<=n; i++)
        if (ok[i] == 0 || v[i].size() % 2 == 1)
        {
            fout << -1;
            return 0;
        }

    s[0] = s[1] = 1;
    while (s[0] > 0)
    {
        x = s[s[0]];
        if (v[x].empty() == 0)
        {
            y = v[x].back();
            v[x].pop_back();

            if (much[y].fol == 0)
            {
                much[y].fol = 1;
                s[++s[0]] = x ^ much[y].x ^ much[y].y;
            }
        }
        else
        {
            rasp[++rasp[0]] = x;
            s[0]--;
        }
    }

    for (i = 1; i<rasp[0]; i++)
        fout << rasp[i] << ' ';
    return 0;
}