Advertisement
qwerty787788

Tree

Dec 7th, 2014
356
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 2.73 KB | None | 0 0
  1. import java.io.*;
  2. import java.util.*;
  3.  
  4. public class Tree {
  5.     FastScanner in;
  6.     PrintWriter out;
  7.  
  8.     class Dsu {
  9.         int[] p;
  10.  
  11.         Dsu(int n) {
  12.             p = new int[n];
  13.             for (int i = 0; i < n; i++) {
  14.                 p[i] = i;
  15.             }
  16.         }
  17.  
  18.         int get(int v) {
  19.             return p[v] == v ? v : (p[v] = get(p[v]));
  20.         }
  21.  
  22.         void unite(int x, int y) {
  23.             p[get(x)] = get(y);
  24.         }
  25.     }
  26.  
  27.     double sum;
  28.  
  29.     int go(int v, int p, int curH, ArrayList<Integer>[] g) {
  30.         int maxH = curH;
  31.         for (int to : g[v]) {
  32.             if (to != p) {
  33.                 maxH = Math.max(maxH, go(to, v, curH + 1, g));
  34.             }
  35.         }
  36.         sum += curH * 1.0 * (maxH - curH);
  37.         return maxH;
  38.     }
  39.  
  40.     void solve() {
  41.         Random rnd = new Random(123);
  42.         for (int it = 0; it < 50; it++) {
  43.             int n = 30_000;
  44.             Dsu dsu = new Dsu(n);
  45.             ArrayList<Integer>[] g = new ArrayList[n];
  46.             for (int i = 0; i < n; i++) {
  47.                 g[i] = new ArrayList<>();
  48.             }
  49.             for (int iter = 0; iter < n - 1; iter++) {
  50.                 int x = rnd.nextInt(n);
  51.                 int y = rnd.nextInt(n);
  52.                 if (dsu.get(x) != dsu.get(y)) {
  53.                     g[x].add(y);
  54.                     g[y].add(x);
  55.                     dsu.unite(x, y);
  56.                 } else {
  57.                     iter--;
  58.                 }
  59.             }
  60.             sum = 0;
  61.             go(0, 0, 1, g);
  62.             System.err.println("avarage = " + sum / n);
  63.         }
  64.     }
  65.  
  66.     void run() {
  67.         try {
  68.             in = new FastScanner(new File("tree.in"));
  69.             out = new PrintWriter(new File("tree.out"));
  70.  
  71.             solve();
  72.  
  73.             out.close();
  74.         } catch (FileNotFoundException e) {
  75.             e.printStackTrace();
  76.         }
  77.     }
  78.  
  79.     void runIO() {
  80.  
  81.         in = new FastScanner(System.in);
  82.         out = new PrintWriter(System.out);
  83.  
  84.         solve();
  85.  
  86.         out.close();
  87.     }
  88.  
  89.     class FastScanner {
  90.         BufferedReader br;
  91.         StringTokenizer st;
  92.  
  93.         public FastScanner(File f) {
  94.             try {
  95.                 br = new BufferedReader(new FileReader(f));
  96.             } catch (FileNotFoundException e) {
  97.                 e.printStackTrace();
  98.             }
  99.         }
  100.  
  101.         public FastScanner(InputStream f) {
  102.             br = new BufferedReader(new InputStreamReader(f));
  103.         }
  104.  
  105.         String next() {
  106.             while (st == null || !st.hasMoreTokens()) {
  107.                 String s = null;
  108.                 try {
  109.                     s = br.readLine();
  110.                 } catch (IOException e) {
  111.                     e.printStackTrace();
  112.                 }
  113.                 if (s == null)
  114.                     return null;
  115.                 st = new StringTokenizer(s);
  116.             }
  117.             return st.nextToken();
  118.         }
  119.  
  120.         boolean hasMoreTokens() {
  121.             while (st == null || !st.hasMoreTokens()) {
  122.                 String s = null;
  123.                 try {
  124.                     s = br.readLine();
  125.                 } catch (IOException e) {
  126.                     e.printStackTrace();
  127.                 }
  128.                 if (s == null)
  129.                     return false;
  130.                 st = new StringTokenizer(s);
  131.             }
  132.             return true;
  133.         }
  134.  
  135.         int nextInt() {
  136.             return Integer.parseInt(next());
  137.         }
  138.  
  139.         long nextLong() {
  140.             return Long.parseLong(next());
  141.         }
  142.  
  143.         double nextDouble() {
  144.             return Double.parseDouble(next());
  145.         }
  146.     }
  147.  
  148.     public static void main(String[] args) {
  149.         new Tree().runIO();
  150.     }
  151. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement