Cod sursa(job #1758680)

Utilizator blackoddAxinie Razvan blackodd Data 17 septembrie 2016 17:30:30
Problema Ciclu Eulerian Scor 60
Compilator cpp Status done
Runda Arhiva educationala Marime 0.89 kb
#include <fstream>
#include <vector>
#include <stack>
#include <algorithm>
using namespace std;

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

#define MaxN 100001

vector<int> G[MaxN], ans;
stack<int> st;
int n, m, x, y;

int main() {

    fin >> n >> m;

    for ( int i = 1; i <= m; ++i ) {
        fin >> x >> y;
        G[x].push_back(y);
        G[y].push_back(x);
    }

    st.push(1);

    while(!st.empty()) {
        int x = st.top();
        if ( G[x].size() ) {
            int y = G[x].back();
            G[x].pop_back();
            G[y].erase(find(G[y].begin(), G[y].end(), x));
            st.push(y);
        }
        else {
            ans.push_back(x);
            st.pop();
        }
    }

    for ( int i = 0; i < ans.size() - 1; ++i )
        fout << ans[i] << ' ';

    fin.close();
    fout.close();
    return 0;
}