-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathDuyet-TPLT.cpp
52 lines (52 loc) · 1015 Bytes
/
Duyet-TPLT.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
#include <stdio.h>
#include <conio.h>
#define MAX 50
#define TRUE 1
#define FALSE 0
int A[MAX][MAX], n,chuaxet[MAX], solt=0;
void Init(void){
int i,j;FILE *fp;
fp=fopen("dothi_tplt.in","r");
fscanf(fp,"%d",&n);
printf("\n So dinh do thi:%d",n);
for(i=1; i<=n; i++){
printf("\n");chuaxet[i]=TRUE;
for(j=1; j<=n; j++){
fscanf(fp,"%d",&A[i][j]);
printf("%3d",A[i][j]);
}
}
}
void BFS(int u){
int queue[MAX],low=1,high=1, s,t;
queue[low]=u;chuaxet[u]=FALSE;
while(low<=high){
s = queue[low];low=low+1;
printf("%3d", s);
for(t=1; t<=n; t++){
if(A[s][t] && chuaxet[t]){
high = high+1;
queue[high]=t;
chuaxet[t]=FALSE;
}
}
}
}
void DFS(int u){
int v;printf("%3d",u);
chuaxet[u]=FALSE;
for(v=1; v<=n; v++){
if(A[u][v] && chuaxet[v])
DFS(v);
}
}
int main(void){
int u;
Init();
for(u=1;u<=n; u++){
if(chuaxet[u]){ solt++;
printf("\n TP.Lien thong %d:", solt);
BFS(u);//DFS(u);
}
}
}