Advertisement
vov44k

112634

Jan 26th, 2023
701
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 1.05 KB | None | 0 0
  1. import java.io.IOException;
  2. import java.util.LinkedList;
  3. import java.util.Scanner;
  4.  
  5. public class task112634 {
  6.     public static void BFS(int s) {
  7.         LinkedList<Integer> q = new LinkedList<Integer>();
  8.         d[s] = 0;
  9.         q.addLast(s);
  10.         while (!q.isEmpty()) {
  11.             int vertex =  q.pollFirst();
  12.             for (int j = 0; j < n; j++) {
  13.                 if (G[vertex][j] == 1 && d[j] == -1) {
  14.                     d[j] = d[vertex] + 1;
  15.                     q.addLast(j);
  16.                 }
  17.             }
  18.         }
  19.     }
  20.  
  21.     static int n;
  22.     static int[] d;
  23.     static int[][] G;
  24.     public static void main(String[] args) throws IOException {
  25.         Scanner in = new Scanner(System.in);
  26.         n = in.nextInt();
  27.         int x = 0;
  28.         int q=0;
  29.         int[] a = new int[n];
  30.         d = new int[n];
  31.         for (int i = 0; i < n; i++) {
  32.             d[i] = -1;
  33.         }
  34.         G = new int[n][n];
  35.         for (int i = 0; i < n; i++) {
  36.             for (int j = 0; j < n; j++) {
  37.                 G[i][j] = in.nextInt();
  38.             }
  39.         }
  40.         BFS(0);
  41.         for (int i = 0; i < a.length; i++) {
  42.             if(d[i]==-1) {
  43.                 System.out.println("NO");
  44.                 q=0;
  45.                 break;
  46.             }else {
  47.                 q=1;
  48.             }
  49.         }
  50.         if(q==1)System.out.println("YES");
  51.     }
  52.  
  53. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement