Pagini recente » Cod sursa (job #2533035) | Cod sursa (job #2372112) | Cod sursa (job #637322) | Cod sursa (job #1729839) | Cod sursa (job #2509365)
#include <iostream>
#include <fstream>
#include <queue>
#include <stack>
#include <string.h>
using namespace std;
ifstream f("ciclueuler.in");
ofstream g("ciclueuler.out");
struct muchie
{
int x, y;
int getTheOtherEnd(int nr)
{
if(nr != x)
return x;
else
return y;
}
};
bool viz[500005];
muchie p[500005];
vector<int> liste[500005];
int nod;
int n, m;
void citire()
{
int x, y;
f>>n>>m;
for(int i = 1; i<=m; ++i)
{
f>>x>>y;
p[i] = {x, y};
liste[x].push_back(i);
liste[y].push_back(i);
}
}
bool verifEuler()
{
for(int i = 1; i<=n; ++i)
{
if(liste[i].size()%2 == 1)
{
return false;
}
if(nod == 0 && !liste[i].empty())
{
nod = i;
}
}
if(nod == 0)
{
nod = 1;
return true;
}
queue<int> Q;
Q.push(nod);
while(!Q.empty())
{
int currNode = Q.front();
Q.pop();
for(int i = 0; i<liste[currNode].size(); ++i)
{
int otherNode = p[liste[currNode][i]].getTheOtherEnd(currNode);
if(viz[otherNode] == false)
{
viz[otherNode] = true;
Q.push(otherNode);
}
}
}
for(int i = 1; i<=n; ++i)
{
if(viz[i] == false && !liste[i].empty())
{
return false;
}
}
return true;
}
void solve()
{
memset(viz, 0, sizeof(viz));
stack<int> S;
S.push(nod);
while(!S.empty())
{
if(liste[S.top()].size() == 0)
{
if(S.size() != 1)
g<<S.top()<<" ";
S.pop();
}
else
{
int currNode = S.top();
while(!liste[currNode].empty() && viz[liste[currNode].back()] == true)
liste[currNode].pop_back();
if(liste[currNode].empty())
continue;
viz[liste[currNode].back()] = true;
S.push(p[liste[currNode].back()].getTheOtherEnd(currNode));
liste[currNode].pop_back();
}
}
}
int main()
{
citire();
if(verifEuler())
solve();
else
g<<-1;
return 0;
}