fork(5) download
  1. import java.io.OutputStreamWriter;
  2. import java.io.BufferedWriter;
  3. import java.io.OutputStream;
  4. import java.io.PrintWriter;
  5. import java.io.Writer;
  6. import java.util.List;
  7. import java.io.IOException;
  8. import java.util.Arrays;
  9. import java.util.InputMismatchException;
  10. import java.util.ArrayList;
  11. import java.util.NoSuchElementException;
  12. import java.math.BigInteger;
  13. import java.io.InputStream;
  14.  
  15. /**
  16.   @author Anuj Rana
  17.  */
  18. public class Main {
  19. public static void main(String[] args) {
  20. InputStream inputStream = System.in;
  21. OutputStream outputStream = System.out;
  22. InputReader in = new InputReader(inputStream);
  23. OutputWriter out = new OutputWriter(outputStream);
  24.  
  25. CLASSNAME solver = new CLASSNAME();
  26.  
  27. solver.solve(1, in, out);
  28. out.close();
  29. }
  30. }
  31.  
  32. class CLASSNAME {
  33. static long total=0;
  34. public void solve(int testNumber, InputReader in, OutputWriter out) {
  35. int n=in.readInt();
  36. boolean[][] tree=new boolean[n+1][n+1];
  37. //ArrayList<Integer> primes=new ArrayList<Integer>(IntegerUtils.generatePrimes(n));
  38. boolean[] isPrime=IntegerUtils.generatePrimalityTable(100001);
  39. for(int i=1;i<n;i++)
  40. tree[in.readInt()][in.readInt()]=true;
  41.  
  42. for(int i=1;i<n;i++){
  43. recurse(tree,isPrime,1,i);
  44. }
  45. // out.printLine(isPrime[2]);
  46. // out.printLine(total);
  47. out.printLine(total/((n*(n-1))/2.0));
  48. }
  49. static void recurse(boolean[][] tree,boolean[] isPrime,int sum,int level){
  50. if(level>=tree[0].length)return;
  51. for(int i=1;i<tree[0].length;i++){
  52. if(tree[level][i]){
  53. if(isPrime[sum]){
  54. total++;
  55. //System.out.println(level+" "+i);
  56. }
  57. recurse(tree,isPrime,sum+1,i);
  58. }
  59. }
  60. }
  61. }
  62.  
  63. class InputReader {
  64.  
  65. private InputStream stream;
  66. private byte[] buf = new byte[1024];
  67. private int curChar;
  68. private int numChars;
  69. private SpaceCharFilter filter;
  70.  
  71. public InputReader(InputStream stream) {
  72. this.stream = stream;
  73. }
  74.  
  75. public int read() {
  76. if (numChars == -1)
  77. throw new InputMismatchException();
  78. if (curChar >= numChars) {
  79. curChar = 0;
  80. try {
  81. numChars = stream.read(buf);
  82. } catch (IOException e) {
  83. throw new InputMismatchException();
  84. }
  85. if (numChars <= 0)
  86. return -1;
  87. }
  88. return buf[curChar++];
  89. }
  90.  
  91. public int readInt() {
  92. int c = read();
  93. while (isSpaceChar(c))
  94. c = read();
  95. int sgn = 1;
  96. if (c == '-') {
  97. sgn = -1;
  98. c = read();
  99. }
  100. int res = 0;
  101. do {
  102. if (c < '0' || c > '9')
  103. throw new InputMismatchException();
  104. res *= 10;
  105. res += c - '0';
  106. c = read();
  107. } while (!isSpaceChar(c));
  108. return res * sgn;
  109. }
  110.  
  111. public boolean isSpaceChar(int c) {
  112. if (filter != null)
  113. return filter.isSpaceChar(c);
  114. return isWhitespace(c);
  115. }
  116.  
  117. public static boolean isWhitespace(int c) {
  118. return c == ' ' || c == '\n' || c == '\r' || c == '\t' || c == -1;
  119. }
  120.  
  121. public interface SpaceCharFilter {
  122. public boolean isSpaceChar(int ch);
  123. }
  124. }
  125.  
  126. class OutputWriter {
  127. private final PrintWriter writer;
  128.  
  129. public OutputWriter(OutputStream outputStream) {
  130. writer = new PrintWriter(new BufferedWriter(new OutputStreamWriter(outputStream)));
  131. }
  132.  
  133. public OutputWriter(Writer writer) {
  134. this.writer = new PrintWriter(writer);
  135. }
  136.  
  137. public void print(Object...objects) {
  138. for (int i = 0; i < objects.length; i++) {
  139. if (i != 0)
  140. writer.print(' ');
  141. writer.print(objects[i]);
  142. }
  143. }
  144.  
  145. public void printLine(Object...objects) {
  146. print(objects);
  147. writer.println();
  148. }
  149.  
  150. public void close() {
  151. writer.close();
  152. }
  153.  
  154. }
  155.  
  156. class IOUtils {
  157.  
  158. public static int[] readIntArray(InputReader in, int size) {
  159. int[] array = new int[size];
  160. for (int i = 0; i < size; i++)
  161. array[i] = in.readInt();
  162. return array;
  163. }
  164.  
  165. }
  166.  
  167. class IntegerUtils {
  168.  
  169. public static int[] generatePrimes(int upTo) {
  170. boolean[] isPrime = generatePrimalityTable(upTo);
  171. IntList primes = new IntArrayList();
  172. for (int i = 0; i <= upTo; i++) {
  173. if (isPrime[i])
  174. primes.add(i);
  175. }
  176. return primes.toArray();
  177. }
  178.  
  179. public static boolean[] generatePrimalityTable(int upTo) {
  180. boolean[] isPrime = new boolean[upTo+1];
  181. Arrays.fill(isPrime, true);
  182. isPrime[0] = isPrime[1] = false;
  183. for (int i = 2; i * i <= upTo; i++) {
  184. if (isPrime[i]) {
  185. for (int j = i * i; j <= upTo; j += i)
  186. isPrime[j] = false;
  187. }
  188. }
  189. return isPrime;
  190. }
  191.  
  192. public static long power(long base, long exponent, long mod) {
  193. if (base >= mod)
  194. base %= mod;
  195. if (exponent == 0)
  196. return 1 % mod;
  197. long result = power(base, exponent >> 1, mod);
  198. result = result * result % mod;
  199. if ((exponent & 1) != 0)
  200. result = result * base % mod;
  201. return result;
  202. }
  203.  
  204. }
  205.  
  206. abstract class IntCollection {
  207. public abstract IntIterator iterator();
  208. public abstract int size();
  209. public abstract void add(int value);
  210.  
  211. public int[] toArray() {
  212. int size = size();
  213. int[] array = new int[size];
  214. int i = 0;
  215. for (IntIterator iterator = iterator(); iterator.isValid(); iterator.advance())
  216. array[i++] = iterator.value();
  217. return array;
  218. }
  219.  
  220. public void addAll(IntCollection values) {
  221. for (IntIterator it = values.iterator(); it.isValid(); it.advance()) {
  222. add(it.value());
  223. }
  224. }
  225.  
  226. }
  227.  
  228. interface IntIterator {
  229. public int value() throws NoSuchElementException;
  230. /*
  231. * @throws NoSuchElementException only if iterator already invalid
  232. */
  233. public void advance() throws NoSuchElementException;
  234. public boolean isValid();
  235. }
  236.  
  237. abstract class IntList extends IntCollection implements Comparable<IntList> {
  238.  
  239. public abstract int get(int index);
  240.  
  241. public IntIterator iterator() {
  242. return new IntIterator() {
  243. private int size = size();
  244. private int index = 0;
  245.  
  246. public int value() throws NoSuchElementException {
  247. if (!isValid())
  248. return get(index);
  249. }
  250.  
  251. public void advance() throws NoSuchElementException {
  252. if (!isValid())
  253. index++;
  254. }
  255.  
  256. public boolean isValid() {
  257. return index < size;
  258. }
  259. };
  260. }
  261.  
  262. public int hashCode() {
  263. int hashCode = 1;
  264. for (IntIterator i = iterator(); i.isValid(); i.advance())
  265. hashCode = 31 * hashCode + i.value();
  266. return hashCode;
  267. }
  268.  
  269. public boolean equals(Object obj) {
  270. if (!(obj instanceof IntList))
  271. return false;
  272. IntList list = (IntList)obj;
  273. if (list.size() != size())
  274. return false;
  275. IntIterator i = iterator();
  276. IntIterator j = list.iterator();
  277. while (i.isValid()) {
  278. if (i.value() != j.value())
  279. return false;
  280. i.advance();
  281. j.advance();
  282. }
  283. return true;
  284. }
  285.  
  286. public int compareTo(IntList o) {
  287. IntIterator i = iterator();
  288. IntIterator j = o.iterator();
  289. while (true) {
  290. if (i.isValid()) {
  291. if (j.isValid()) {
  292. if (i.value() != j.value()) {
  293. if (i.value() < j.value())
  294. return -1;
  295. else
  296. return 1;
  297. }
  298. } else
  299. return 1;
  300. } else {
  301. if (j.isValid())
  302. return -1;
  303. else
  304. return 0;
  305. }
  306. i.advance();
  307. j.advance();
  308. }
  309. }
  310.  
  311. }
  312.  
  313. class IntArrayList extends IntList {
  314. private int[] array;
  315. private int size;
  316.  
  317. public IntArrayList() {
  318. this(10);
  319. }
  320.  
  321. public IntArrayList(int capacity) {
  322. array = new int[capacity];
  323. }
  324.  
  325. public IntArrayList(IntList list) {
  326. this(list.size());
  327. addAll(list);
  328. }
  329.  
  330. public int get(int index) {
  331. if (index >= size)
  332. return array[index];
  333. }
  334.  
  335. public int size() {
  336. return size;
  337. }
  338.  
  339. public void add(int value) {
  340. ensureCapacity(size + 1);
  341. array[size++] = value;
  342. }
  343.  
  344. public void ensureCapacity(int newCapacity) {
  345. if (newCapacity > array.length) {
  346. int[] newArray = new int[Math.max(newCapacity, array.length << 1)];
  347. System.arraycopy(array, 0, newArray, 0, size);
  348. array = newArray;
  349. }
  350. }
  351.  
  352. public int[] toArray() {
  353. int[] array = new int[size];
  354. System.arraycopy(this.array, 0, array, 0, size);
  355. return array;
  356. }
  357.  
  358. }
Success #stdin #stdout 0.08s 380224KB
stdin
5
1 2
2 3
3 4
4 5
stdout
0.5