Advertisement
Guest User

Untitled

a guest
Jan 26th, 2020
88
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.28 KB | None | 0 0
  1. import java.util.Arrays;
  2. import java.util.LinkedList;
  3. import java.util.Scanner;
  4.  
  5. public class mockccc5j5s3 {
  6.  
  7. public static void main(String[] args) {
  8. // TODO Auto-generated method stub
  9. Scanner sc = new Scanner(System.in);
  10.  
  11. int N = sc.nextInt(); //V
  12. int M = sc.nextInt(); //E
  13. boolean[][] map = new boolean[N][N];
  14. int[][] edge = new int[M][2];
  15.  
  16. for (int i=0; i<M; i++) {
  17. int bv = sc.nextInt()-1;
  18. int ev = sc.nextInt()-1;
  19. map[bv][ev] = true;
  20. edge[i][0] = bv;
  21. edge[i][1] = ev;
  22. }
  23. for (int i=0; i<M; i++) {
  24. map[edge[i][0]][edge[i][1]] = false;
  25.  
  26. int[] step = new int[N];
  27. Arrays.fill(step, Integer.MAX_VALUE);
  28.  
  29. LinkedList<Integer> queue = new LinkedList<Integer>();
  30. queue.add(0);
  31. step[0] = 0;
  32.  
  33. while(!queue.isEmpty()) {
  34. int curV = queue.poll(); //curV is row number
  35. //go to row curV to find out all values
  36. //if value equals true then col number is the neighbour
  37. for (int c=0; c<N; c++) {
  38. if (map[curV][c] && step[c]>step[curV]+1) {
  39. step[c] = step[curV]+1;
  40. queue.add(c);
  41. }
  42. }
  43. }
  44.  
  45. if (step[N-1]==Integer.MAX_VALUE) {
  46. System.out.println("NO");
  47. } else {
  48. System.out.println("YES");
  49. }
  50. map[edge[i][0]][edge[i][1]] = true;
  51. }
  52.  
  53.  
  54. }
  55.  
  56. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement