Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import java.util.Arrays;
- import java.util.LinkedList;
- import java.util.Scanner;
- public class mockccc5j5s3 {
- public static void main(String[] args) {
- // TODO Auto-generated method stub
- Scanner sc = new Scanner(System.in);
- int N = sc.nextInt(); //V
- int M = sc.nextInt(); //E
- boolean[][] map = new boolean[N][N];
- int[][] edge = new int[M][2];
- for (int i=0; i<M; i++) {
- int bv = sc.nextInt()-1;
- int ev = sc.nextInt()-1;
- map[bv][ev] = true;
- edge[i][0] = bv;
- edge[i][1] = ev;
- }
- for (int i=0; i<M; i++) {
- map[edge[i][0]][edge[i][1]] = false;
- int[] step = new int[N];
- Arrays.fill(step, Integer.MAX_VALUE);
- LinkedList<Integer> queue = new LinkedList<Integer>();
- queue.add(0);
- step[0] = 0;
- while(!queue.isEmpty()) {
- int curV = queue.poll(); //curV is row number
- //go to row curV to find out all values
- //if value equals true then col number is the neighbour
- for (int c=0; c<N; c++) {
- if (map[curV][c] && step[c]>step[curV]+1) {
- step[c] = step[curV]+1;
- queue.add(c);
- }
- }
- }
- if (step[N-1]==Integer.MAX_VALUE) {
- System.out.println("NO");
- } else {
- System.out.println("YES");
- }
- map[edge[i][0]][edge[i][1]] = true;
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement