Cod sursa(job #2543831)

Utilizator XXMihaiXX969Gherghinescu Mihai Andrei XXMihaiXX969 Data 11 februarie 2020 16:17:31
Problema Ciclu Eulerian Scor 100
Compilator cpp-64 Status done
Runda Arhiva educationala Marime 1.09 kb
#include <bits/stdc++.h>

using namespace std;

const int DIM = 5e5 + 7;


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

vector <pair <int,int> > l[DIM];

vector <int> ans;


int nr[DIM];

bool viz[DIM];

void dfs(int x)
{
   while(!l[x].empty())
   {
       int nod = l[x].back().first;
       int ind = l[x].back().second;
        l[x].pop_back();
       if(viz[ind] == false)
       {
           viz[ind] = true;
           dfs(nod);
       }
   }
   ans.push_back(x);
}
int main()
{
    int n, m;

    in >> n >> m;

    for(int i = 1; i <= m; i++)
    {
        int x, y;

        in >> x >> y;

        l[x].push_back(make_pair(y,i));
        l[y].push_back(make_pair(x,i));

        nr[x]++;
        nr[y]++;
    }

    for(int i = 1;i <= n; i++)
        if(nr[i] % 2 == 1 || nr[i] == 0)
        {
            out << -1;
            return 0;
        }

        dfs(1);

        ans.pop_back();

        for(int i = 0; i < ans.size(); i++)
            out << ans[i] << " ";

    in.close();
    out.close();
    return 0;
}