SHARE
TWEET

TreeAndVertex

a guest Mar 29th, 2016 84 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. import java.util.*;
  2.  
  3. public class TreeAndVertex {
  4.   static final int N = 100;
  5.  
  6.   boolean order(int l, int m, int r) {
  7.     return l <= m && m <= r;
  8.   }
  9.  
  10.   public String checkData(int[] tree) {
  11.     int n = tree.length + 1;
  12.  
  13.     if (!order(2, n, 100))
  14.       return "tree must contain between 1 and 99 elements, inclusive.";
  15.  
  16.     for (int i = 0; i < n - 1; ++i)
  17.       if (!order(0, tree[i], i))
  18.         return "tree[i] must be between 0 and i, inclusive.";
  19.  
  20.     return "";
  21.   }
  22.  
  23.   public int get(int[] tree) {
  24.     int n = tree.length + 1;
  25.     Integer[] deg = new Integer[n];
  26.  
  27.     for (int i = 0; i < n; ++i)
  28.       deg[i] = new Integer(0);
  29.    
  30.     for (int i = 0; i < n - 1; ++i) {
  31.       deg[tree[i]]++;
  32.       deg[i + 1]++;
  33.     }
  34.  
  35.     return Collections.max(Arrays.asList(deg));
  36.   }
  37.  
  38.   public static void main(String[] args) {
  39.     TreeAndVertex solver = new TreeAndVertex();
  40.     int[] tree = {0, 1, 2};
  41.     System.out.println(solver.checkData(tree));
  42.     System.out.println(solver.get(tree));
  43.   }
  44. };
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
 
Top