Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import java.util.LinkedList;
- import java.util.Scanner;
- public class t111839 {
- public static void BFS_comp(int[][] g, int n){
- LinkedList queue = new LinkedList<Integer>();
- int[] d = new int[n];
- String res = "";
- int quantity = 0;
- for (int i=0;i<n;i++)
- d[i]=-1;
- for(int j=0;j<n;j++) {
- if(d[j]==-1) {
- String result = "";
- quantity++;
- int kol=1;
- d[j]=0;
- queue.addLast(j);
- while (!queue.isEmpty()){
- int v = (int)queue.pollFirst();
- for (int i=0;i<n;i++){
- if(g[v][i]==1&&d[i]==-1){
- d[i]=d[v]+1;
- queue.addLast(i);
- kol++;
- }
- }
- result = result + (v+1) + " ";
- }
- res = res + kol + "\n" + result + "\n";
- }
- }
- System.out.println(quantity + "\n" + res);
- }
- public static void main(String[] args) {
- Scanner sc = new Scanner(System.in);
- int n=sc.nextInt();
- int[][] matrix = new int[n][n];
- for(int i=0;i<n;i++) {
- for (int j=0; j<n; j++) {
- matrix[i][j]=sc.nextInt();
- }
- }
- BFS_comp(matrix, n);
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement