Cod sursa(job #2750171)

Utilizator anetAneta Anghel anet Data 10 mai 2021 06:08:34
Problema Ciclu Eulerian Scor 100
Compilator cpp-64 Status done
Runda Arhiva educationala Marime 1.16 kb
#include <algorithm>
#include <fstream>
#include <vector>
#include <stack>
using namespace std;
	
 ifstream fin("ciclueuler.in");	
ofstream fout("ciclueuler.out");

const int NMAX = 100005, MMAX = 500005;
vector<int> G[NMAX];
bool usedEdge[MMAX];
int from[MMAX], to[MMAX]; 
	
int main() 
{	
    int n, m, x, y;	
    fin >> n >> m;
	
    for (int i = 0; i < m; ++i) 
    {
        fin >> x >> y;
        G[x].push_back(i);
        G[y].push_back(i);
        from[i] = x;
        to[i] = y;
    }
	bool eulerian = true;
	for (int i = 1; i <= n; ++i) 
		if (G[i].size() & 1) 
		{
			eulerian = false;
			break;
		}

	if (!eulerian)
		fout << "-1\n";
	else
	{
		vector<int> cycle;	
		stack<int> stk;
		
		stk.push(1);
		while (!stk.empty()) 
		{
			int x = stk.top();
		
			if (G[x].empty()) 
			{
				stk.pop();
				cycle.push_back(x);
				continue;
			}
			
			int e = G[x].back();
			G[x].pop_back();

			if (!usedEdge[e]) 
			{
				usedEdge[e] = true;
				int y = from[e] ^ to[e] ^ x;
				stk.push(y);
			}		 
		}
		
		for (size_t i = 0; i < cycle.size() - 1; ++i) 
			fout << cycle[i] << ' ';			
	}
    fout.close();
}