Advertisement
Guest User

Untitled

a guest
Jul 27th, 2016
50
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.04 KB | None | 0 0
  1. import java.util.*;
  2. import java.io.*;
  3. import java.math.*;
  4. import java.util.concurrent.ConcurrentLinkedQueue;
  5.  
  6. /**
  7. * Auto-generated code below aims at helping you parse
  8. * the standard input according to the problem statement.
  9. **/
  10. class Player {
  11.  
  12. public static void main(String args[]) {
  13. Scanner in = new Scanner(System.in);
  14. int N = in.nextInt(); // the total number of nodes in the level, including the gateways
  15. int L = in.nextInt(); // the number of links
  16. int E = in.nextInt(); // the number of exit gateways
  17.  
  18. boolean[][] edges = new boolean[N][N];
  19. boolean[] exits = new boolean[N];
  20. for (int i = 0; i < L; i++) {
  21. int N1 = in.nextInt(); // N1 and N2 defines a link between these nodes
  22. int N2 = in.nextInt();
  23. edges[N1][N2] = true;
  24. edges[N2][N1] = true;
  25. }
  26. for (int i = 0; i < E; i++) {
  27. int EI = in.nextInt(); // the index of a gateway node
  28. exits[EI] = true;
  29. }
  30.  
  31. // game loop
  32. while (true) {
  33. int SI = in.nextInt(); // The index of the node on which the Skynet agent is positioned this turn
  34.  
  35. ArrayList<ArrayList<Integer>> shortestPaths = getShortestPaths(N, SI, edges, exits);
  36. int[][] edgeCount = countEdges(N, shortestPaths);
  37. int maximum = -1;
  38. String edgeToCut = null;
  39. for(int N1 = 0; N1 < N; N1++) {
  40. for(int N2 = 0; N2 < N; N2++) {
  41. if(edgeCount[N1][N2] > maximum) {
  42. maximum = edgeCount[N1][N2];
  43. edgeToCut = N1+" "+N2;
  44. }
  45. }
  46. }
  47.  
  48. // Example: 0 1 are the indices of the nodes you wish to sever the link between
  49. System.out.println(edgeToCut);
  50. }
  51. }
  52.  
  53. private static ArrayList<ArrayList<Integer>> getShortestPaths(int N, int posIdx, boolean[][] edges, boolean[] exits) {
  54. //initialize data-structures for breadth-first-search
  55. ConcurrentLinkedQueue<Integer> visitingQueue = new ConcurrentLinkedQueue<Integer>();
  56. ArrayList<ArrayList<Integer>> paths = new ArrayList<ArrayList<Integer>>(N);
  57. for(int i = 0; i < N; i++) {
  58. paths.add(new ArrayList<Integer>());
  59. }
  60. ArrayList<ArrayList<Integer>> shortestPaths = new ArrayList<ArrayList<Integer>>();
  61. boolean[] visited = new boolean[N];
  62. int shortestLength = Integer.MAX_VALUE;
  63. //add first element
  64. paths.set(posIdx, new ArrayList<Integer>());
  65. visitingQueue.offer(posIdx);
  66. visited[posIdx] = true;
  67. //go through the queue in breadth-first-search
  68. while(!visitingQueue.isEmpty()) {
  69. int current = visitingQueue.poll();
  70. if(exits[current]) {
  71. shortestLength = Math.min(shortestLength, paths.get(current).size() + 1);
  72. }
  73. for(int i = 0; i < N; i++) {
  74. if(edges[i][current] && !visited[i]) {
  75. paths.set(i, new ArrayList<Integer>(paths.get(current)));
  76. paths.get(i).add(current);
  77. visitingQueue.offer(i);
  78. visited[i] = true;
  79. }
  80. }
  81. }
  82. for(int i = 0; i < N; i++) {
  83. if(exits[i] && paths.get(i).size() + 1 == shortestLength) {
  84. ArrayList<Integer> path = paths.get(i);
  85. path.add(i);
  86. shortestPaths.add(path);
  87. }
  88. }
  89. return shortestPaths;
  90. }
  91.  
  92. private static int[][] countEdges(int N, ArrayList<ArrayList<Integer>> paths) {
  93. int[][] counts = new int[N][N];
  94. for(int i = 0; i < paths.size(); i++) {
  95. for(int j = 1; j < paths.get(i).size(); j++) {
  96. int N1 = paths.get(i).get(j-1);
  97. int N2 = paths.get(i).get(j);
  98. counts[N1][N2]++;
  99. counts[N2][N1]++;
  100. }
  101. }
  102. return counts;
  103. }
  104. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement