Pagini recente » Cod sursa (job #1749595) | Cod sursa (job #2149148) | Cod sursa (job #1633337) | Cod sursa (job #2750145) | Cod sursa (job #2073141)
#include <fstream>
#include <iostream>
#include <stack>
#include <vector>
#define MUCHII 500005
using namespace std;
ifstream in("ciclueuler.in");
ofstream out("ciclueuler.out");
struct shape
{
int x, y;
bool ap = false;
} v[500005];
vector <int> g[100005];
stack <int> stiva;
int n, m;
void citire()
{
in >> n >> m;
for(int i = 1; i <= m; i++)
{
in >> v[i].x >> v[i].y;
g[v[i].x].push_back(i);
g[v[i].y].push_back(i);
}
}
void parcurgere()
{
stiva.push(1);
while(!stiva.empty())
{
int nod = stiva.top();
if(g[nod].size())
{
int next = g[nod].back();
g[nod].pop_back();
if(v[next].ap == true)
continue;
v[next].ap = true;
if(v[next].x == nod)
stiva.push(v[next].y);
else
stiva.push(v[next].x);
}
else
{
out << stiva.top() << " ";
stiva.pop();
}
}
}
int main()
{
citire();
for(int i = 1; i <= n; i++)
if(g[i].size() % 2 != 0)
{
out << "-1";
return 0;
}
parcurgere();
return 0;
}