fork download
  1. /* package whatever; // don't place package name! */
  2.  
  3. import java.util.*;
  4. import java.lang.*;
  5. import java.io.*;
  6.  
  7. /* Name of the class has to be "Main" only if the class is public. */
  8. class Ideone
  9. {
  10. public static void main(String[] args) throws Exception {
  11.  
  12. Scanner sc = new Scanner(System.in);
  13.  
  14. int n = sc.nextInt();
  15. int m = sc.nextInt();
  16.  
  17. List<List<Integer>> adj = new ArrayList<>();
  18. List<Integer> val = new ArrayList<>();
  19.  
  20. // adjacency list
  21. for(int i = 0; i <= n; i++) {
  22. adj.add(new ArrayList<>());
  23. }
  24.  
  25. // dummy for 1-based indexing
  26. val.add(0);
  27.  
  28. // reading node values
  29. for(int i = 1; i <= n; i++) {
  30. val.add(sc.nextInt());
  31. }
  32.  
  33. // reading edges
  34. for(int i = 0; i < m; i++) {
  35.  
  36. int u = sc.nextInt();
  37. int v = sc.nextInt();
  38.  
  39. adj.get(u).add(v);
  40. adj.get(v).add(u);
  41. }
  42.  
  43. shoetestPathOfIthNodeFrom(adj,1,val);
  44. }
  45.  
  46. public static void shoetestPathOfIthNodeFrom(List<List<Integer>> adj, int src,List<Integer> val){
  47. Queue<Integer> q = new LinkedList<>();
  48. int lvl[] = new int[adj.size()];
  49. boolean visited[] = new boolean[adj.size()];
  50. int cfive[] = new int[adj.size()];
  51. visited[src] = true;
  52. lvl[src] = 0;
  53. if(val.get(src) == 5){
  54. cfive[src] =cfive[src]+1;
  55. }
  56. while(!q.isEmpty()){
  57. int par = q.poll();
  58.  
  59. for(int ch : adj.get(par)){
  60.  
  61. if(visited[ch]==false){
  62. q.add(ch);
  63. visited[ch] = true;
  64. lvl[ch] = lvl[par]+1;
  65. if(val.get(src) == 5){
  66. cfive[src] =cfive[src]+1;
  67. }
  68. }else{
  69. if()
  70. }
  71. }
  72. }
  73. }
  74. }
Success #stdin #stdout 0.23s 60904KB
stdin
6 6
0
5
5
0
5
0
1 2
1 5
2 3
5 6
3 4
6 4
stdout
Node Values:
1 -> 0
2 -> 5
3 -> 5
4 -> 0
5 -> 5
6 -> 0

Graph:
1 -> [2, 5]
2 -> [1, 3]
3 -> [2, 4]
4 -> [3, 6]
5 -> [1, 6]
6 -> [5, 4]