Pagini recente » Cod sursa (job #106553) | Cod sursa (job #538213) | Cod sursa (job #601015) | Cod sursa (job #2604937) | Cod sursa (job #1236094)
#include <fstream>
#include <vector>
#include <stack>
#include <list>
using namespace std;
ifstream ka("ciclueuler.in");
ofstream ki("ciclueuler.out");
const int N_MAX = 100000;
const int M_MAX = 500000;
list <int> muchii[N_MAX + 1];
bool folosita[M_MAX + 1], vazut[N_MAX + 1];
int grad[N_MAX + 1];
stack <int> stiva;
vector <int> raspuns;
void sterge(int x, int y)
{
muchii[x].pop_front();
for(list<int>::iterator it = muchii[y].begin(); it != muchii[y].end(); ++it)
if(*it == x)
{
muchii[y].erase(it);
break;
}
}
int n, m;
int main()
{
ka >> n >> m;
int x, y;
for(int i = 1; i <= m; i++)
{
ka >> x >> y;
muchii[x].push_back(y);
muchii[y].push_back(x);
grad[x]++;
grad[y]++;
}
stiva.push(x);
while(!stiva.empty())
{
int curent = stiva.top();
vazut[curent] = true;
stiva.pop();
if(!muchii[curent].empty())
{
stiva.push(curent);
stiva.push(muchii[curent].front());
sterge(curent, muchii[curent].front());
raspuns.push_back(curent);
}
}
bool euler = true;
for(int i = 1; i <= n && euler; i++)
{
if(!vazut[i] || grad[i] % 2 != 0)
euler = false;
}
if(euler)
for(int i = 0; i < raspuns.size(); i++)
ki << raspuns[i] << " ";
else
ki << -1;
}