Cod sursa(job #1252469)

Utilizator okros_alexandruOkros Alexandru okros_alexandru Data 30 octombrie 2014 19:47:30
Problema Ciclu Eulerian Scor 100
Compilator cpp Status done
Runda Arhiva educationala Marime 1.41 kb
#include <fstream>
#include <vector>
#include <algorithm>
#include <stack>

#define Nmax 100100
#define Size Graph[Node].size()
#define Neighbour Graph[Node].front()

using namespace std;

vector <int> Graph[Nmax], Answer;
stack <int> Stack;
int N;

void deleteEdge(int Node, int Neigh) {

    Graph[Node][0] = Graph[Node][Size - 1];
    Graph[Node].pop_back();
    Graph[Neigh].erase(find(Graph[Neigh].begin(), Graph[Neigh].end(), Node));

}
void Solve() {

    int Node;

    for(Node = 1; Node <= N; Node++)
        if(Size & 1)
            return;

    Stack.push(1);

    while(!Stack.empty()) {

        Node = Stack.top();

        if(Size > 0) {
            Stack.push(Neighbour);
            deleteEdge(Node, Neighbour);
            }
        else {
            Answer.push_back(Node);
            Stack.pop();
            }

        }

}
void Read() {

    int x, y, M;
    ifstream in("ciclueuler.in");
    in >> N >> M;

    while(M--) {

        in >> x >> y;
        Graph[x].push_back(y);
        Graph[y].push_back(x);

        }

    in.close();

}
void Write() {

    ofstream out("ciclueuler.out");

    if(Answer.size() == 0)
        out << "-1";
    else
    for(int i = 0; i < Answer.size(); i++)
        out << Answer[i] << ' ';

    out << '\n';
    out.close();

}
int main() {

    Read();
    Solve();
    Write();

    return 0;

}