Pagini recente » Cod sursa (job #2891308) | Cod sursa (job #2536407) | Cod sursa (job #1670596) | Cod sursa (job #1914392) | Cod sursa (job #1470300)
#include <iostream>
#include <fstream>
#include <stdlib.h>
#include <time.h>
using namespace std;
int a[50][50],viz[50],n,m,gr[50];
void depth (int vf)
{
int k;
viz[vf]=1;
for (k=1;k<=n;k++)
{
if (a[vf][k]!=0 && viz[k]==0)
depth (k);
}
}
void nestergere (int x, int y)
{
gr[x]++;
gr[y]++;
a[x][y]++;
a[y][x]++;
}
void stergere(int x, int y)
{
gr[x]--;
gr[y]--;
a[x][y]--;
a[y][x]--;
}
int main()
{
int i,j,c[50],x,y,kv=0;
srand(time(NULL)) ;
ofstream g;
ifstream f;
f.open("ciclueuler.in");
g.open("ciclueuler.out");
f >> n >> m;
for (i=1;i<=m;i++)
{
viz[i]=0;
f>>x>>y;
a[y][x]++;
a[x][y]++;
gr[x]++;
gr[y]++;
}
for (i=1;i<=n;i++)
{
for (j=1;j<=n;j++)
{
c[i]=0;
cout << a[i][j] << " ";
}
cout<< endl;
}
depth(1);
for (i=1;i<=n;i++)
if (viz[i]==0)
{
g<<-1;
return 0;
}
for (i=1;i<=n;i++)
if (gr[i]%2==1)
{
g<<-1;
return 0;
}
c[m]=1;
y=m;
while (m>0)
{
for (j=1;j<=n;j++)
if(a[c[m]][j]>0 && j!=kv)
if(gr[j]>1 || m==1)
{
c[m-1]=j;
stergere(c[m],j);
break;
}
if (c[m-1]==0 && m>1)
{
m++;
nestergere(c[m],c[m-1]);
kv=c[m-1];
c[m-1]=0;
}
else
{
m--;
kv=0;
}
}
for (i=y;i>0;i--)
g<< c[i]<< " ";
}