Pagini recente » Cod sursa (job #2743636) | Cod sursa (job #1778760) | Cod sursa (job #2980038) | Cod sursa (job #1205170) | Cod sursa (job #2425032)
#include <iostream>
#include <fstream>
#include <vector>
#include <algorithm>
#include <map>
#include <stack>
#include <queue>
using namespace std;
ifstream fin("ciclueuler.in");
ofstream fout("ciclueuler.out");
int N, M;
map<int, vector<int>> G;
vector<int> sol;
vector<pair<int, int>> edges;
map<int, bool> visited;
void Euler()
{
stack<int> stack;
stack.push(1);
while (!stack.empty())
{
int node = stack.top();
if (G[node].size())
{
int next = G[node].back();
G[node].pop_back();
if (visited[next])
{
if (edges[next].first == node)
stack.push(edges[next].second);
else
stack.push(edges[next].first);
visited[next] = false;
}
}
else
{
fout << node << " ";
stack.pop();
}
}
}
int main()
{
fin >> N >> M;
for (int i = 0; i < M; i++)
{
int x, y;
fin >> x >> y;
G[x].push_back(i);
G[y].push_back(i);
edges.push_back({ x, y });
visited[i] = true;
}
for (int i = 1; i <= N; i++)
if (G[i].size() % 2 == 1)
{
fout << "-1";
return 0;
}
Euler();
return 0;
}