Pagini recente » Cod sursa (job #2086977) | Cod sursa (job #2917508) | Cod sursa (job #1575834) | Cod sursa (job #436851) | Cod sursa (job #978165)
Cod sursa(job #978165)
#include <fstream>
#include <algorithm>
#include <vector>
#include <stack>
using namespace std;
int n, m;
vector <int> G[100010];
stack <int> st;
inline void Read()
{
ifstream f("ciclueuler.in");
f>>n>>m;
int i, x, y;
for (i=1; i<=m; i++)
{
f>>x>>y;
G[x].push_back(y);
G[y].push_back(x);
}
f.close();
}
inline bool Verificare()
{
for (int i=1; i<=n; i++)
if (G[i].size() & 1)
return true;
return false;
}
inline void Solve()
{
ofstream g("ciclueuler.out");
st.push(1);
int x, y;
vector <int>::iterator it;
bool gata = false;
while (!gata)
{
gata = true;
x = st.top();
if (G[x].size())
{
gata = false;
y = G[x].back();
G[x].pop_back();
for (it = G[y].begin(); it!=G[y].end(); it++)
{
if (*it == x)
{
*it = G[y].back();
G[y].pop_back();
break;
}
}
st.push(y);
}
}
while (!st.empty())
{
g<<st.top()<<" ";
st.pop();
}
g<<"\n";
g.close();
}
int main()
{
Read();
if (Verificare())
{
ofstream g("ciclueuler.out");
g<<"-1\n";
g.close();
return 0;
}
Solve();
return 0;
}