Pagini recente » Cod sursa (job #638501) | Cod sursa (job #2002277) | Cod sursa (job #2914752) | Cod sursa (job #1054218) | Cod sursa (job #331418)
Cod sursa(job #331418)
#include<fstream>
#include<queue>
#include<stack>
#include<list>
#include<cstring>
using namespace std;
ifstream f("ciclueuler.in");
ofstream g("ciclueuler.out");
const int maxn=100005;
queue<int>Q;
stack<int>S;
int n,m,i,j,x,y,grade[maxn],answer[maxn],rd;
bool passed[maxn];
list<int>a[maxn];
bool pos()
{
int k=0,x;
S.push(1);
passed[1]=true;
while(!S.empty())
{
x=S.top();
S.pop();
++k;
for(list<int>::iterator it=a[x].begin();it!=a[x].end();++it)
if(passed[*it]==false)
passed[*it]=true,S.push(*it);
}
if(k!=n) return 0;
for(i=1;i<=n;++i)
if(grade[i]&1)
return 0;
return 1;
}
void drop(int x,int y)
{
--grade[x];
--grade[y];
for(list<int>::iterator it=a[x].begin();it!=a[x].end();++it)
if(*it==y)
{
a[x].erase(it);
break;
}
}
void go(int x)
{
int w;
while(!a[x].empty())
{
w=a[x].front();
a[x].pop_front();
S.push(x);
drop(w,x);
x=w;
}
}
int main()
{
f>>n>>m;
for(i=1;i<=m;++i)
{
f>>x>>y;
a[x].push_back(y);
a[y].push_back(x);
++grade[x];
++grade[y];
}
if(pos()==0)
{
g<<-1<<"\n";
f.close();
g.close();
return 0;
}
return 0;
x=1;
do
{
go(x);
x=S.top();
answer[++rd]=S.top();
S.pop();
}
while(!S.empty());
for(i=rd;i;--i)
g<<answer[i]<<" ";
g<<"\n";
f.close();
g.close();
return 0;
}