Pagini recente » Cod sursa (job #1307483) | Statistici TARCAU CRISTINA (cristinuta19) | Cod sursa (job #1708182) | Cod sursa (job #2885300) | Cod sursa (job #1243466)
#include <iostream>
#include <fstream>
#include <list>
#include <queue>
#include <stack>
#define MAX_N 100003
using namespace std;
stack <int> st;
list <int> nodes[MAX_N];
queue <int> q;
bool vis[MAX_N];
int n, m;
ofstream out("ciclueuler.out");
void BFS(int x)
{
vis[x] = 1;
q.push(x);
int cnode;
while (!q.empty())
{
cnode = q.front();
for(list<int>::iterator i = nodes[cnode].begin(); i != nodes[cnode].end(); i++)
{
if (!vis[*i])
{
vis[*i] = 1;
q.push(*i);
}
}
q.pop();
}
}
bool conex()
{
for (int i = 1; i <= n; ++i)
if (!vis[i])
return 0;
return 1;
}
int eulerian()
{
for (int i = 1; i <= n; ++i)
{
if (nodes[i].size() % 2 == 1)
return 0;
}
return 1;
}
void deleteEdge(int v, int w)
{
for(list<int>::iterator i = nodes[w].begin(); i != nodes[w].end(); i++)
{
if (*i == v)
{
i = nodes[w].erase(i);
break;
}
}
nodes[v].pop_front();
}
void euler(int x)
{
st.push(x);
//out << x; //fprintf(out, "%d", x);
int v, w;
bool first = 1;
int counter = 0;
while (counter < m && !st.empty())
{
v = st.top();
if (nodes[v].empty())
{
if (!first)
out << ' ' << v;
else
{
out << v;
first = 0;
}
st.pop();
counter++;
continue;
}
w = nodes[v].front();
deleteEdge(v, w);
st.push(w);
}
out << '\n';
//out << ' ' << w << '\n';//fprintf(out, " %d\n", w);
}
void solPrint()
{
if (conex() == false)
{
out << "-1\n"; //fprintf(out, "-1\n");
return;
}
int x = eulerian();
if (x == 1)
euler(1);
else
out << "-1\n"; //fprintf(out, "-1\n");
}
void read()
{
ifstream in("ciclueuler.in");
in >> n >> m; //fscanf(in, "%d%d", &n, &m);
int x, y;
for (int i = 1; i <= m; i++)
{
in >> x >> y; //fscanf(in, "%d%d", &x, &y);
nodes[x].push_back(y);
nodes[y].push_back(x);
}
}
int main()
{
read();
BFS(1);
solPrint();
return 0;
}