Pagini recente » Cod sursa (job #2147716) | Cod sursa (job #1100826) | Cod sursa (job #2189877) | Cod sursa (job #1600815) | Cod sursa (job #1162585)
#include <fstream>
#include <algorithm>
#include <vector>
//#include <stack>
#include <queue>
using namespace std;
ifstream fin("ciclueuler.in");
ofstream fout("ciclueuler.out");
int N, M;
vector<pair<int, int> > V[100001];
//stack<int> ST;
queue<int> Q;
bool instack[100001], used[500002];
int is_euler(int nod)
{
//ST.push(nod);
Q.push(nod);
instack[nod] = true;
//while (!ST.empty())
while (!Q.empty())
{
//int now = ST.top();
int now = Q.front();
//ST.pop();
Q.pop();
for (vector<pair<int, int> >::iterator it = V[now].begin(); it != V[now].end(); ++it)
{
if (!instack[it->first])
{
//ST.push(it->first);
Q.push(it->first);
instack[it->first] = true;
}
}
}
int ok = 1;
for (int i = 1; i <= N; ++i)
{
if (V[i].size() % 2 == 1 || !instack[i])
{
ok = 0;
break;
}
}
return ok;
}
void solve_euler(int nod)
{
//ST.push(nod);
Q.push(nod);
// while (!ST.empty())
while (!Q.empty())
{
//int now = ST.top();
int now = Q.front();
bool ok = true;
while (V[now].size() > 0)
{
pair<int, int> neighbour = V[now].back();
V[now].pop_back();
if (!used[neighbour.second])
{
used[neighbour.second] = true;
//ST.push(neighbour.first);
Q.push(neighbour.first);
ok = false;
break;
}
}
if (ok)
{
fout << now << ' ';
//ST.pop();
Q.pop();
}
}
}
int main()
{
fin >> N >> M;
for (int i = 1, nod1, nod2; i <= M; ++i)
{
fin >> nod1 >> nod2;
V[nod1].push_back(make_pair(nod2, i));
V[nod2].push_back(make_pair(nod1, i));
}
if (!is_euler(1))
{
fout << -1 << '\n';
fin.close();
fout.close();
return 0;
}
solve_euler(1);
fin.close();
fout.close();
return 0;
}