M¹nh D¬ng Biªn So¹n
Bé M«n CÊu Tróc D÷ LiÖu
Gi¶i ThuËt 2012
§ª Sè 19 Trong Bé §Ò ¤n TËp
#include<iostream.h>
#include<conio.h>
#include<fstream.h>
ifstream Tepvao("C:/cau truc du lieu/dethi/dothi19.in.txt");
ofstream Tepra("C:/cau truc du lieu/dethi/cay19.out.txt");
/*
Dung Thuat toan DFS de tao 1 cay khung
*/
int a[100][100],n,x[100],dem,aa[100],bb[100],m;
void NhapDL(int &n,int &m)
{
Tepvao>>n>>m;
for(int i=1;i<=n;i++)
for(int j=1;j<=n;j++)
Tepvao>>a[i][j];
}
void DFS(int i)
{
x[i]=1;
for(int j=1;j<=n;j++)
if(x[j]==0&&a[i][j]==1)
{
dem++;
aa[dem]=i;
bb[dem]=j;
DFS(j);
}
}
void HienThi()
{
dem=0;
DFS(m);
Tepra<<n<<"\t"<<dem<<endl;
for(int i=1;i<=dem;i++)
Tepra<<aa[i]<<" "<<bb[i]<<endl;
}
main()
{
Trang 2NhapDL(n,m); HienThi(); }