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