Pagini recente » Cod sursa (job #1466950) | Cod sursa (job #689347) | Cod sursa (job #2646132) | Cod sursa (job #333113) | Cod sursa (job #2064526)
#include <iostream>
#include <fstream>
#include <vector>
#include <queue>
#include <stack>
using namespace std;
ifstream fin("ciclueuler.in");
ofstream fout("ciclueuler.out");
const int NMAX = 100000 + 5;
const int MMAX = 500000 + 5;
queue <int> ans;
vector <int> graph[MMAX];
int n, m;
int st[MMAX], dr[MMAX];
bool vis[MMAX];
bool vis2[MMAX];
void read()
{
fin >> n >> m;
for (int i = 1; i <= m; ++i)
{
fin >> st[i] >> dr[i];
graph[st[i]].push_back(i);
graph[dr[i]].push_back(i);
}
}
void dfs()
{
stack<int>aux;
aux.push(1);
while (!aux.empty()){
int nod = aux.top();
vis2[nod] = true;
//eliminate bad edges
while (graph[nod].size() && vis[graph[nod].back()] == 1)
graph[nod].pop_back();
if (graph[nod].size())
{
int i = graph[nod].back();
//cout << nod << " " << i << "\n";
vis[i] = 1;
graph[nod].pop_back();
aux.push(st[i] + dr[i] - nod);
}
else
{
ans.push(nod);
aux.pop();
}
}
}
bool check()
{
for (int i = 1; i <= m; ++i)
if (!vis[i])
return 0;
for (int i = 1; i <= n; ++i)
if (!vis2[i])
return 0;
return 1;
}
void write()
{
ans.pop();
while (!ans.empty())
{
fout << ans.front() << " ";
ans.pop();
}
}
bool euler()
{
for (int i = 1; i <= n; ++i)
if (graph[i].size() % 2)
return 0;
return 1;
}
int main()
{
read();
if (euler())
dfs();
else
{
fout << -1;
return 0;
}
if (check())
write();
else
fout << -1;
return 0;
}