Cod sursa(job #2568651)

Utilizator ursu0406Ursu Ianis-Vlad ursu0406 Data 4 martie 2020 09:00:39
Problema Ciclu Eulerian Scor 30
Compilator cpp-64 Status done
Runda Arhiva educationala Marime 1.32 kb
#include <bits/stdc++.h>
using namespace std;

const int N_MAX = 1e5 + 1;

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

struct pair_hash{
    size_t operator ()(const pair<int, int>& p) const
    {
        return hash<int>()(p.first) ^ hash<int>()(p.second);
    }
};

unordered_map<pair<int, int>, int, pair_hash> edge_cnt;

int N, M;
vector<vector<int>> graph(N_MAX, vector<int>());

stack<int> sol;

void dfs_euler_cycle(int node)
{
    for(auto& next : graph[node])
    {
        if(edge_cnt[{node, next}] != 0)
        {
             edge_cnt[{node, next}]--;
             edge_cnt[{next, node}]--;

             dfs_euler_cycle(next);
        }
    }

    sol.push(node);
}

int main()
{
    ios_base::sync_with_stdio(false);
    fin.tie(0);
    fout.tie(0);

    fin >> N >> M;

    for(int i = 1; i <= M; ++i)
    {
        int x, y;
        fin >> x >> y;

        graph[x].push_back(y);
        graph[y].push_back(x);

        edge_cnt[{x, y}]++;
        edge_cnt[{y, x}]++;
    }

    for(int i = 1; i <= N; ++i)
    {
        if(graph[i].size() & 1)
        {
            cout << -1;
            return 0;
        }
    }

    dfs_euler_cycle(1);

    while(sol.size() != 1)
    {
        fout << sol.top() << ' ';
        sol.pop();
    }
}