Cod sursa(job #2575301)

Utilizator uvIanisUrsu Ianis Vlad uvIanis Data 6 martie 2020 12:45:33
Problema Ciclu Eulerian Scor 100
Compilator cpp-64 Status done
Runda Arhiva educationala Marime 1.04 kb
#include <bits/stdc++.h>
using namespace std;

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

int N, M;
vector<vector<pair<int, int>>> graph(1e5 + 1, vector<pair<int, int>>());

stack<int> answer;
vector<bool> visited(5e5 + 1, false);

void dfs_euler(int node)
{
    while(graph[node].size())
    {
        pair<int, int> next = graph[node].back();
        graph[node].pop_back();

        if(visited[next.second] == true) continue;

        visited[next.second] = true;
        dfs_euler(next.first);
    }

    answer.push(node);
}


int main()
{
    fin >> N >> M;

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

        graph[x].push_back({y, i});
        graph[y].push_back({x, i});
    }

    for(int i = 1; i <= N; ++i)
    {
        if((graph[i].size() & 1) == false) continue;

        fout << -1;
        return 0;
    }


    dfs_euler(1);

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