Cod sursa(job #1621924)

Utilizator ArkinyStoica Alex Arkiny Data 29 februarie 2016 22:59:57
Problema Ciclu Eulerian Scor 10
Compilator cpp Status done
Runda Arhiva educationala Marime 1.3 kb
#include<fstream>
#include<vector>
#include<bitset>
using namespace std;

ifstream in("ciclueuler.in");
ofstream out("ciclueuler.out");
#define p first
#define c second
typedef pair<int, int>  Node;
vector<Node> G[100010];
bitset<100010> viz;
int A[100010],N,M;
inline void swap(Node &e1, Node &e2)
{
	Node t = e1;
	e1 = e2;
	e2 = t;
}
void DFS(int node)
{
	viz[node] = 1;

	for (int i = 0; i < G[node].size(); ++i)
		if (viz[G[node][i].p] == 0)
			DFS(G[node][i].p);
}
void delete_edge(int x, int y)
{
	int lst = G[x].size() - 1;
	swap(G[y][G[x][lst].c], G[y][G[y].size() - 1]);
	Node el = G[y][G[x][lst].c];
	G[el.p][el.c].c = G[x][lst].c;

	G[y].pop_back();
	G[x].pop_back();
}
void ciclu_euler(int node)
{
	while (G[node].size())
	{
		out << node<<" ";
		int el = G[node][G[node].size() - 1].p;
		delete_edge(node,el);
		ciclu_euler(el);
	}
}
int main()
{
	in >> N >> M;

	for (int i = 1; i <= M; ++i)
	{
		int x, y;
		in >> x >> y;
		A[x]++;
		A[y]++;
		G[x].push_back(make_pair(y,G[y].size()));
		G[y].push_back(make_pair(x,G[x].size()-1));
	}

	for (int i = 1; i <= N;++i)
		if (A[i])
		{
			DFS(i);
			break;
		}
	for (int i = 1; i <= N;++i)
		if ((viz[i] == 0 && A[i]) || A[i] % 2 != 0)
		{
			out << -1;
			return 0;
		}
	ciclu_euler(1);
	return 0;
}