fork download
  1. //imports for BufferedReader
  2. import java.io.BufferedReader;
  3. import java.io.InputStreamReader;
  4.  
  5. //import for Scanner and other utility classes
  6. import java.util.*;
  7.  
  8. class TestClass {
  9.  
  10. static class Cell{
  11.  
  12. int v;
  13. int w;
  14.  
  15. Cell(int vertex,int weight){
  16. v=vertex;
  17. w=weight;
  18. }
  19. }
  20.  
  21. static class CompareCell implements Comparator<Cell>{
  22.  
  23. public int compare(Cell c1,Cell c2){
  24.  
  25. if(c2.w==c1.w){
  26. return c2.v-c1.v;
  27. }
  28. return c2.w-c1.w;
  29. }
  30. }
  31. public static void main(String args[] ) throws Exception {
  32.  
  33. // Write your code here
  34.  
  35. String nmk[]=br.readLine().split(" ");
  36. int n=Integer.parseInt(nmk[0]);
  37. int m=Integer.parseInt(nmk[1]);
  38. int k=Integer.parseInt(nmk[2]);
  39.  
  40. String val[]=br.readLine().split(" ");
  41.  
  42. int values[]=new int[val.length+1];
  43.  
  44. HashMap<Integer,List<Cell>> graph=new HashMap<>();
  45.  
  46. for(int i=1;i<=n;i++){
  47. graph.put(i,new ArrayList<>());
  48. }
  49.  
  50. for(int i=1;i<values.length;i++){
  51. values[i]=Integer.parseInt(val[i-1]);
  52. }
  53.  
  54. while(m--!=0){
  55.  
  56. String uv[]=br.readLine().split(" ");
  57.  
  58. int u=Integer.parseInt(uv[0]);
  59. int v=Integer.parseInt(uv[1]);
  60.  
  61. Cell uc=new Cell(u,values[u]);
  62. Cell vc=new Cell(v,values[v]);
  63.  
  64. graph.get(u).add(vc);
  65. graph.get(v).add(uc);
  66. }
  67.  
  68. for(int i=1;i<=graph.size();i++){
  69. List<Cell> list=graph.get(i);
  70. Collections.sort(list,new CompareCell());
  71.  
  72. if(list.size()<k){
  73. System.out.print("-1");
  74. }else{
  75. int t=1;
  76. for(Cell c:list){
  77. if(t==k){
  78. System.out.print(c.v+" ");
  79. }
  80. t++;
  81. }
  82. }
  83. System.out.println();
  84. }
  85. }
  86. }
Runtime error #stdin #stdout #stderr 0.06s 32896KB
stdin
Standard input is empty
stdout
Standard output is empty
stderr
Exception in thread "main" java.lang.NullPointerException
	at TestClass.main(Main.java:36)