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