Advertisement
vov44k

Untitled

Jan 28th, 2023
665
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 1.35 KB | None | 0 0
  1. import java.io.IOException;
  2. import java.util.LinkedList;
  3. import java.util.Scanner;
  4.  
  5. public class Main {
  6.     static Scanner in = new Scanner(System.in);
  7.     static int n = in.nextInt();
  8.     static int[][] a = new int[n + 1][n + 1];
  9.     static int[] color = new int[n + 1];
  10.     static int k = 1;
  11.     static final int black = 2;
  12.     static final int grey = 1;
  13.     static final int white = 0;
  14.     static boolean f = true;
  15.  
  16.     public static void dfs(int v, int p) {
  17.         if (color[v] == grey) {
  18.             f = false;
  19.         } else {
  20.             color[v] = grey;
  21.  
  22.             for (int i = 1; i < n + 1; i++) {
  23.                 if (a[v][i] == 1 && color[i] != black && i != p) {
  24.                     dfs(i, v);
  25.                 }
  26.             }
  27.             color[v] = black;
  28.         }
  29.     }
  30.  
  31.  
  32.     public static void main(String[] args) {
  33.         for (int i = 1; i < n + 1; i++) {
  34.             for (int j = 1; j < n + 1; j++) {
  35.                 a[i][j] = in.nextInt();
  36.             }
  37.         }
  38.         for (int i = 1; i < n + 1; i++) {
  39.             color[i] = white;
  40.         }
  41.  
  42.         dfs(1, -1);
  43.  
  44.         for (int i = 1; i < n + 1; i++) {
  45.             if (color[i] != black) {
  46.                 f = false;
  47.                 break;
  48.             }
  49.         }
  50.         if (f) System.out.println("YES");
  51.         else System.out.println("NO");
  52.     }
  53.  
  54. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement