private static Scanner scanner;
import java.util.Scanner;
public class Dfs {
public static void dfs(int a[][],int m[],int i,int n)
{
int j;
m[i]=1;
for ( j = 0; j < n && i<n ; j++ )
{
if ( a[i][j] == 1 && m[j]==0)
{
dfs(a,m,j,n);
}
}
}
public static void main(String[] args) {
int n;
int sum=0;
int counter=0;
scanner = new Scanner(System.in);
n=scanner.nextInt();
int [][] graph=new int[n][n];
int i,j;
int m[]=new int[n];
for(i=0;i<n;i++){
m[i]=0;
}
for(i=0;i<n;i++){
for(j=0;j<n;j++){
graph[i][j]=scanner.nextInt();
}
}
}
}
for(i=0;i<n;i++){
if(m[i]==0){
counter++;
dfs(graph, m,i, n);
}
}
System.out.println(counter);
}
}
}
i want this programfirst print number of the components of the graph topology and then at other line the tops of in each component topology for example:
input:
3
0 1 0
1 0 0
0 0 0
output:
2
1 2
3