Cod sursa(job #2816963)

Utilizator oporanu.alexAlex Oporanu oporanu.alex Data 12 decembrie 2021 16:35:09
Problema Ciclu Eulerian Scor 100
Compilator cpp-64 Status done
Runda Arhiva educationala Marime 1.01 kb
#include <bits/stdc++.h>

using namespace std;
const int nmax = 500005;
ifstream fin("ciclueuler.in");
ofstream fout("ciclueuler.out");

int L[nmax], R[nmax];
bool used[nmax];
vector<int> G[nmax];
vector<int> sol;
void dfs(int v){
    while(G[v].size())
    {
        int e = G[v].back();
        G[v].pop_back();
        if(used[e] == false){
            used[e] = true;
            int other = v ^ L[e] ^ R[e];
            dfs(other);
        }
    }
    if(G[v].size() == 0)
        sol.push_back(v);
}
int main()
{
    int V, E;
    fin >> V >> E;

    for(int i = 1; i <= E; ++i)
    {
        int x, y;
        fin >> x >> y;
        G[x].push_back(i);
        G[y].push_back(i);
        L[i] = x; R[i] = y;
    }
       for(int i = 1; i <= V; ++i){
        if(G[i].size() % 2)
            {
                fout << "-1\n";
                return 0;
            }
    }

    dfs(1);
    for(int i = 0; i < sol.size() - 1; ++i)
        fout << sol[i] << ' ';
    return 0;
}