fork(2) download
  1. import java.io.BufferedReader;
  2. import java.io.BufferedWriter;
  3. import java.io.IOException;
  4. import java.io.InputStreamReader;
  5. import java.io.OutputStreamWriter;
  6. import java.io.PrintWriter;
  7. import java.util.ArrayList;
  8. import java.util.HashMap;
  9. import java.util.LinkedList;
  10. import java.util.List;
  11. import java.util.Queue;
  12.  
  13. /**
  14.  * @author smundhra
  15.  *
  16.  */
  17.  
  18. /*
  19.  
  20.  */
  21. public class Main {
  22.  
  23. /**
  24. * @param args
  25. * @throws IOException
  26. * @throws NumberFormatException
  27. */
  28. public static void main(String[] args) throws NumberFormatException,
  29.  
  30. // BufferedInputStream in = new BufferedInputStream(new FileInputStream(
  31. // "C:\\CodeChef\\smundhra\\src\\lifeUniverseAndEverything\\Input"));
  32. // System.setIn(in);
  33.  
  34.  
  35. int t = Integer.parseInt(bf.readLine());
  36. // long t1 = System.currentTimeMillis();
  37. while (t > 0) {
  38. Graph g = new Graph(bf);
  39. for (Node n : g.hm.keySet())
  40. g.getAvgDistance(n);
  41. pw.printf(g.mostPopfriend.getN() + " " + "%f" + "\n", g
  42. .getMinDist());
  43.  
  44. t--;
  45.  
  46. }
  47. pw.flush();
  48. bf.close();
  49. // long diff = System.currentTimeMillis() - t1;
  50. // pw.print(diff);
  51. pw.close();
  52.  
  53. }
  54.  
  55. }
  56.  
  57. class Node {
  58.  
  59. int n;
  60. int distance;
  61.  
  62. @Override
  63. public int hashCode() {
  64. final int prime = 31;
  65. int result = 1;
  66. result = prime * result + n;
  67. return result;
  68. }
  69.  
  70. @Override
  71. public boolean equals(Object obj) {
  72. if (this == obj)
  73. return true;
  74. if (obj == null)
  75. return false;
  76. if (getClass() != obj.getClass())
  77. return false;
  78. Node other = (Node) obj;
  79. if (n != other.n)
  80. return false;
  81. return true;
  82. }
  83.  
  84. public int getDistance() {
  85. return distance;
  86. }
  87.  
  88. public void setDistance(int distance) {
  89. this.distance = distance;
  90. }
  91.  
  92. public int getN() {
  93. return n;
  94. }
  95.  
  96. public void setN(int n) {
  97. this.n = n;
  98. }
  99.  
  100. public Node(int n) {
  101. this.n = n;
  102. }
  103.  
  104. private boolean isVisited;
  105.  
  106. public boolean isVisited() {
  107. return isVisited;
  108. }
  109.  
  110. public void setVisited(boolean isVisited) {
  111. this.isVisited = isVisited;
  112. }
  113.  
  114. }
  115.  
  116. class Graph {
  117. int nodeCount;
  118. public HashMap<Node, ArrayList<Node>> hm;
  119. Integer[] s;
  120. Node mostPopfriend;
  121. double minDist = Double.MAX_VALUE;
  122. List<Node> nodeList = new ArrayList<Node>();
  123.  
  124. this.nodeCount = Integer.parseInt(bf.readLine());
  125.  
  126. hm = new HashMap<Node, ArrayList<Node>>();
  127. int i;
  128.  
  129. for (i = 1; i <= nodeCount; i++) {
  130. Node node = new Node(i);
  131. nodeList.add(node);
  132. hm.put(node, new ArrayList<Node>());
  133. }
  134. for (i = 1; i <= nodeCount; i++) {
  135. String a[] = bf.readLine().split(" ");
  136. List<Node> l = hm.get(new Node(i));
  137.  
  138. for (String s : a) {
  139. l.add(nodeList.get(Integer.parseInt(s) - 1));
  140. }
  141. }
  142. }
  143.  
  144. public void getAvgDistance(Node head) {
  145. for (Node node : hm.keySet()) {
  146. node.setVisited(false);
  147. node.setDistance(0);
  148. }
  149.  
  150. double totalDist = 0D;
  151.  
  152. Queue<Node> q = new LinkedList<Node>();
  153. q.add(head);
  154. head.setVisited(true);
  155. while (!q.isEmpty()) {
  156. Node node = q.poll();
  157.  
  158. for (Node n1 : hm.get(node)) {
  159. if (n1 == null)
  160. break;
  161. if (!n1.isVisited()) {
  162. n1.setDistance(node.getDistance() + 1);
  163. n1.setVisited(true);
  164. q.add(n1);
  165. totalDist += n1.getDistance();
  166. }
  167. }
  168. }
  169. double avgDistance = totalDist / (double) nodeCount;
  170. if (avgDistance <= this.minDist) {
  171. this.minDist = avgDistance;
  172. this.mostPopfriend = head;
  173. } else if (avgDistance == this.minDist) {
  174. if (this.mostPopfriend == null)
  175. this.mostPopfriend = head;
  176. if (this.mostPopfriend.getN() > head.getN())
  177. this.mostPopfriend = head;
  178. }
  179.  
  180. }
  181.  
  182. public double getMinDist() {
  183. return minDist;
  184. }
  185. }
Success #stdin #stdout 0.06s 213312KB
stdin
1
2
2
1
stdout
2 0.500000