vov44k

t111541

Feb 1st, 2023 (edited)
659
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 1.09 KB | None | 0 0
  1. import java.util.Scanner;
  2.  
  3. public class t111541 {
  4.  
  5.     public static int[][] graph;
  6.     public static int[] used;
  7.     public static boolean tree = true;
  8.  
  9.     public static void dfs(int x) {
  10.         if (used[x] == 2) {
  11.             tree = false;
  12.         } else {
  13.             used[x] = 1;
  14.             for (int i = 0; i < graph.length; i++) {
  15.                 if (graph[x][i] == 1 && used[i] != 1)
  16.                     dfs(i);
  17.             }
  18.             used[x] = 2;
  19.         }
  20.     }
  21.  
  22.     public static void main(String[] args) {
  23.         Scanner in = new Scanner(System.in);
  24.         int n = in.nextInt();
  25.  
  26.         graph = new int[n][n];
  27.         for (int i = 0; i < n; i++) {
  28.             for (int j = 0; j < n; j++) {
  29.                 graph[i][j] = in.nextInt();
  30.             }
  31.         }
  32.         used = new int[n];
  33.         dfs(0);
  34.         for (int i = 0; i < n; i ++)
  35.             if (used[i] == 0) {
  36.                 tree = false;
  37.                 break;
  38.             }
  39.         if (tree)
  40.             System.out.println("YES");
  41.         else
  42.             System.out.println("NO");
  43.     }
  44. }
Advertisement
Add Comment
Please, Sign In to add comment