Pagini recente » Cod sursa (job #1655277) | Cod sursa (job #1701443) | Cod sursa (job #818671) | Cod sursa (job #2383778) | Cod sursa (job #775242)
Cod sursa(job #775242)
#include<fstream>
const int white=0;
const int gray=1;
const int black=2;
const int tree=0;
const int back=1;
using namespace std;
ifstream f("ciclueuler.in");
ofstream g("ciclueuler.out");
int n,m,i,j,color[100001],viz[500001],type[500001],grad[100001];
struct lista
{int nod;
int index;
lista* next;};
lista* a[500001];
void add(int xx, int yy, int ind)
{lista* q = new lista;
q->nod=yy;
q->index=ind;
q->next=a[xx];
a[xx]=q;}
void depth(int x)
{lista* q=a[x];
color[x]=gray;
while(q)
{if(color[q->nod]==white && viz[q->index]==0)
{viz[q->index]=1; depth(q->nod);
type[q->index]=tree;}
if(color[q->nod]!=white && viz[q->index]==0)
{viz[q->index]=1;
type[q->index]=back;
}
q=q->next;}
color[x]=black;
}
int conex()
{for(j=1; j<=n; j++)
if(color[j]==white)
return 0;
return 1;
}
int eulerian()
{
for(j=1; j<=n; j++)
if(grad[j]%2==1)
return -1;
return 0;
}
int sx,sy,nextx;
void eulerian_path(int x)
{
lista*q;
lista* w;
m--;
while(m)
{q=a[x];
while(q)
{if(viz[q->index]==1)
{w=q;
if(type[q->index]==back)
break;}
q=q->next;}
nextx=w->nod;
sx=x;
sy=w->nod;
viz[w->index]=2;
g<<sy<<" ";
x=nextx;
m--;}
}
char buff[50],nrc;
int get_number()
{int nrn;
nrn=0;
while(buff[nrc]<='9' && buff[nrc]>='0')
{nrn=10*nrn+buff[nrc]-'0';
nrc++;}
return nrn;
}
int main()
{f>>n>>m;
int aa,bb;
f.getline(buff,50);
for(i=1; i<=m; i++)
{nrc=0;
f.getline(buff,50);
aa=get_number();
nrc++;
bb=get_number();
add(aa,bb,i);
add(bb,aa,i);
grad[aa]++;
grad[bb]++;}
depth(1);
if(conex()==0 || eulerian()==-1)
g<<"-1";
else
{
g<<"1 ";
eulerian_path(1);
}
f.close();
g.close();
return 0;
}