01/31 - Parcurgerea Grafurilor
#include<iostream>
#include<fstream>
using namespace std;
int a[20][20],n,m,v[20];
void citire()
{ifstream f("date");
f>>n>>m;
int x,y;
for(int i=1;i<=m;i++)
{f>>x>>y;
a[x][y]=a[y][x]=1;}
}
void DF(int x)
{cout<<x<<" ";
v[x]=1;
for(int i=1;i<=n;i++)
if(a[x][i]==1 && v[i]==0)
DF(i);
}
int main()
{citire();
for(int i=1;i<=n;i++)
{DF(i);
for(int j=1;j<=n;j++)
v[j]=0;
cout<<endl;}
}
Fisier
8 13
1 2
1 3
1 6
1 8
3 6
3 4
3 5
4 5
5 6
5 7
6 8
6 7
7 8
luni, 30 ianuarie 2012
by DlMuresan
Categories:
Graf
|