Approach
Depth-first search
For CCC 2010 S5 - Nutrient Tree, the implementation follows one branch at a time, making it suitable for components, trees, backtracking, or dependency exploration.
- Define the state carried into one recursive or stack frame.
- Mark or choose the current state before exploring children.
- Combine child results or undo the choice when the branch finishes.
Code notes
- 188 lines of Java from the credited upstream file ccc10s5.java.
- The implementation visibly relies on sequence storage.
- 8 loop blocks detected.
Complexity
Count unique states for graph traversal; for backtracking, count the branching factor and maximum depth.
Check the problem constraints before deciding whether this complexity will pass.
Use this to learn the idea, then write your own version.
12345678910111213141516171819202122232425262728293031323334353637 38 39import java.awt.*;40import hsa.*;41 42public class S52010VLv243{44 public static void main (String[] args)45 {46 TextInputFile c;47 c = new TextInputFile ("s5.8.in");48 TreeNode root = createTreeNode (c.readLine ()); 49 int growth = c.readInt (); 50 51 52 53 54 optimize (root, growth);55 56 System.out.println (root.maxNutrients [growth]);57 }58 59 60 public static TreeNode createTreeNode (String s)61 {62 s = s.trim ();63 64 if (!s.startsWith ("("))65 return new TreeNode (Integer.parseInt (s));66 else67 {68 69 s = s.substring (1, s.length () - 1).trim ();70 71 72 73 74 int i;75 if (s.startsWith ("("))76 {77 78 int count = 1;79 i = 1;80 while (count > 0)81 {82 if (s.charAt (i) == '(')83 count++;84 else if (s.charAt (i) == ')')85 count--;86 i++;87 }88 }89 else90 i = s.indexOf (" ");91 92 93 return new TreeNode (createTreeNode (s.substring (0, i)), createTreeNode (s.substring (i + 1)));94 }95 }96 97 98 public static void optimize (TreeNode node, int growth)99 {100 101 if (node.left == null)102 {103 TreeNode leaf = node;104 leaf.maxNutrients = new int [growth + 1];105 for (int i = 0 ; i <= growth ; i++)106 leaf.maxNutrients [i] = leaf.value + i;107 }108 else109 {110 TreeNode n = node;111 int max, tmp;112 113 114 optimize (n.left, growth);115 116 117 int[] optL = new int [growth + 1];118 for (int i = 0 ; i <= growth ; i++)119 {120 max = 0;121 for (int j = 0 ; j <= i ; j++)122 {123 tmp = Math.min ((1 + j) * (1 + j), n.left.maxNutrients [i - j]);124 if (tmp > max)125 max = tmp;126 }127 optL [i] = max;128 }129 130 131 optimize (n.right, growth);132 133 134 int[] optR = new int [growth + 1];135 for (int i = 0 ; i <= growth ; i++)136 {137 max = 0;138 for (int j = 0 ; j <= i ; j++)139 {140 tmp = Math.min ((1 + j) * (1 + j), n.right.maxNutrients [i - j]);141 if (tmp > max)142 max = tmp;143 }144 optR [i] = max;145 }146 147 148 n.maxNutrients = new int [growth + 1];149 for (int i = 0 ; i <= growth ; i++)150 {151 max = 0;152 for (int j = 0 ; j <= i ; j++)153 {154 tmp = optL [j] + optR [i - j];155 if (tmp > max)156 max = tmp;157 }158 n.maxNutrients [i] = max;159 }160 }161 }162}163 164 165class TreeNode166{167 public int[] maxNutrients;168 public int value;169 public TreeNode left, right;170 171 172 public TreeNode (TreeNode l, TreeNode r)173 {174 value = 0;175 left = l;176 right = r;177 }178 179 180 181 public TreeNode (int v)182 {183 value = v;184 left = null;185 right = null;186 }187}188