Cod sursa(job #2653973)

Utilizator Dragono63Stanciu Rares Stefan Dragono63 Data 29 septembrie 2020 17:08:28
Problema Ciclu Eulerian Scor 100
Compilator cpp-64 Status done
Runda Arhiva educationala Marime 1.93 kb
#include<bits/stdc++.h>
#define NMAX 100005

using namespace std;

/**********************************************/
ifstream f("ciclueuler.in");
ofstream g("ciclueuler.out");
/**********************************************/
int n, m, count = 0;
int ans[NMAX];
bool vizitat[ 5 * NMAX];
vector < pair <int, int> > edges[NMAX];
vector < int > result;
/**********************************************/
///-------------------------------------------------------------
inline void readInput()
{
    f >> n >> m;
    for(int i = 0 ; i < m ; ++ i)
    {
        int a, b;
        f >> a >> b;
        edges[a].push_back({b, i});
        edges[b].push_back({a, i});
    }
}
///-------------------------------------------------------------
inline void Output(bool isntCycle)
{
    if(isntCycle) g << "-1";
    else
        for(int i = 0 ; i < result.size() ; ++i) g<< result[i] << " ";
}
///-------------------------------------------------------------
inline void dfs(int start)
{
    stack<int> st;
    st.push(start);
    while(!st.empty())
    {
        int node = st.top();
        st.pop();
        if(edges[node].size() == 0) result.push_back(node);
        else
        {
            int next = edges[node].back().first;
            int tip = edges[node].back().second;

            edges[node].pop_back();
            st.push(node);

            if(!vizitat[tip])
            {
                vizitat[tip] = true;
                st.push(next);
            }
        }
    }
}
///-------------------------------------------------------------
inline void Solution()
{
    for(int i = 1 ; i <= n ; ++i)
    {
        if(edges[i].size() % 2 != 0)
        {
            Output(true);
            return;
        }
    }
    dfs(1);
    result.pop_back();
    Output(false);
}
///-------------------------------------------------------------
int main()
{
    readInput();
    Solution();
	return 0;
}