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 task112634 {
- public static void BFS(int s) {
- LinkedList<Integer> q = new LinkedList<Integer>();
- d[s] = 0;
- q.addLast(s);
- while (!q.isEmpty()) {
- int vertex = q.pollFirst();
- for (int j = 0; j < n; j++) {
- if (G[vertex][j] == 1 && d[j] == -1) {
- d[j] = d[vertex] + 1;
- q.addLast(j);
- }
- }
- }
- }
- static int n;
- static int[] d;
- static int[][] G;
- public static void main(String[] args) throws IOException {
- Scanner in = new Scanner(System.in);
- n = in.nextInt();
- int x = 0;
- int q=0;
- int[] a = new int[n];
- d = new int[n];
- for (int i = 0; i < n; i++) {
- d[i] = -1;
- }
- G = new int[n][n];
- for (int i = 0; i < n; i++) {
- for (int j = 0; j < n; j++) {
- G[i][j] = in.nextInt();
- }
- }
- BFS(0);
- for (int i = 0; i < a.length; i++) {
- if(d[i]==-1) {
- System.out.println("NO");
- q=0;
- break;
- }else {
- q=1;
- }
- }
- if(q==1)System.out.println("YES");
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement