Pagini recente » Cod sursa (job #2373224) | Cod sursa (job #1660047) | Cod sursa (job #335131) | Cod sursa (job #1629642) | Cod sursa (job #1383866)
#include <fstream>
#include <vector>
#include <stack>
#include <algorithm>
#define Max_Size 100010
using namespace std;
ifstream fin ("ciclueuler.in");
ofstream fout ("ciclueuler.out");
int N, M;
vector < int > V[Max_Size];
stack < int > Stack, Sol;
int main()
{
fin >> N >> M;
for (int i = 1, x, y; i <= M; i++)
{
fin >> x >> y;
V[x].push_back(y);
V[y].push_back(x);
}
for (int i = 1; i <= N; i++)
{
if (V[i].size() % 2)
{
fout << "-1\n";
fout.close();
return 0;
}
}
Stack.push(1);
while (!Stack.empty())
{
int nod = Stack.top();
if (!V[nod].empty())
{
int vecin = V[nod][0];
Stack.push(vecin);
V[nod].erase(V[nod].begin());
V[vecin].erase( find(V[vecin].begin(), V[vecin].end(), nod) );
}
else
{
Stack.pop();
Sol.push(nod);
}
}
if (Sol.size() - 1 != M)
{
fout << "-1\n";
fout.close();
return 0;
}
else
{
while (Sol.size() > 1)
{
fout << Sol.top() << ' ';
Sol.pop();
}
fout << '\n';
fout.close();
}
return 0;
}