Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import java.io.IOException;
- import java.util.LinkedList;
- import java.util.Scanner;
- public class Main {
- static Scanner in = new Scanner(System.in);
- static int n = in.nextInt();
- static int[][] a = new int[n + 1][n + 1];
- static int[] color = new int[n + 1];
- static int k = 1;
- static final int black = 2;
- static final int grey = 1;
- static final int white = 0;
- static boolean f = true;
- public static void dfs(int v, int p) {
- if (color[v] == grey) {
- f = false;
- } else {
- color[v] = grey;
- for (int i = 1; i < n + 1; i++) {
- if (a[v][i] == 1 && color[i] != black && i != p) {
- dfs(i, v);
- }
- }
- color[v] = black;
- }
- }
- public static void main(String[] args) {
- for (int i = 1; i < n + 1; i++) {
- for (int j = 1; j < n + 1; j++) {
- a[i][j] = in.nextInt();
- }
- }
- for (int i = 1; i < n + 1; i++) {
- color[i] = white;
- }
- dfs(1, -1);
- for (int i = 1; i < n + 1; i++) {
- if (color[i] != black) {
- f = false;
- break;
- }
- }
- if (f) System.out.println("YES");
- else System.out.println("NO");
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement