Cod sursa(job #1022810)

Utilizator sziliMandici Szilard szili Data 5 noiembrie 2013 22:58:33
Problema Ciclu Eulerian Scor 100
Compilator cpp Status done
Runda Arhiva educationala Marime 1.55 kb
#include <iostream>
#include <stdio.h>
#include <stdlib.h>
#include <algorithm>
#include <vector>

using namespace std;

int n,m;

vector<int> adjacencyList[100001];
int solutionNr = 0;
int solution [500004];

int st[500001];


void dfs(int startNode){
    int stPointer = 0;
    st[stPointer] = startNode;

    while (stPointer >= 0){
        int currentNode = st[stPointer];

        if (adjacencyList[currentNode].size() > 0){
            int nextNode =  adjacencyList[currentNode].back();
            st[++stPointer] = nextNode;

            adjacencyList[currentNode].pop_back();
            adjacencyList[nextNode].erase(find(adjacencyList[nextNode].begin(), adjacencyList[nextNode].end(), currentNode));
        } else {
            stPointer--;
            solution[solutionNr++] = currentNode;
        }
    }

}

int isEuler(){

    for (int i=1; i<=n; i++){
        if (st[i] % 2 != 0){
            return 0;
        }
    }

    return 1;

}

int main()
{
    freopen("ciclueuler.in", "r", stdin);
    freopen("ciclueuler.out", "w", stdout);

    scanf("%d %d", &n, &m);

    int node1, node2;
    for (int i=0; i<m; i++){
        scanf("%d %d", &node1, &node2);
        adjacencyList[node1].push_back(node2);
        adjacencyList[node2].push_back(node1);
        st[node1]++;
        st[node2]++;
    }


    if (isEuler()){
        dfs(1);

        for (int i=0; i<solutionNr-1; i++){
            printf("%d ", solution[i]);
        }
    } else {
        printf("-1");
    }

    return 0;
}