- Translate each rule into one explicit state update.
- Maintain the invariant after every processed item.
- Return the accumulated state once all relevant input has been handled.
Code notes
- 201 lines of Java from the credited upstream file ccc09s3.java.
- The implementation visibly relies on sequence storage.
- 14 loop blocks detected.
Complexity
Count the number and nesting of passes over the input, then include the maintained containers in the memory estimate.
Check the problem constraints before deciding whether this complexity will pass.
Use this to learn the idea, then write your own version.
1234567891011121314 15 16import java.awt.*;17import hsa.*;18 19public class CCC2009S3DegreesofSeparation20{21 22 public static void main (String[] args)23 {24 Console c;25 int[] [] g;26 27 c = new Console ();28 TextInputFile f = new TextInputFile ("s3.4.in");29 30 31 32 g = new int [50] [50];33 for (int i = 0 ; i < 50 ; i++)34 for (int j = 0 ; j < 50 ; j++)35 g [i] [j] = 0;36 g [1] [6] = 1;37 g [6] [1] = 1;38 g [2] [6] = 1;39 g [6] [2] = 1;40 g [3] [6] = 1;41 g [6] [3] = 1;42 g [4] [6] = 1;43 g [6] [4] = 1;44 g [5] [6] = 1;45 g [6] [5] = 1;46 g [7] [6] = 1;47 g [6] [7] = 1;48 g [3] [4] = 1;49 g [4] [3] = 1;50 g [4] [5] = 1;51 g [5] [4] = 1;52 g [3] [5] = 1;53 g [5] [3] = 1;54 g [3] [15] = 1;55 g [15] [3] = 1;56 g [13] [15] = 1;57 g [15] [13] = 1;58 g [14] [13] = 1;59 g [13] [14] = 1;60 g [12] [13] = 1;61 g [13] [12] = 1;62 g [7] [8] = 1;63 g [8] [7] = 1;64 g [8] [9] = 1;65 g [9] [8] = 1;66 g [9] [10] = 1;67 g [10] [9] = 1;68 g [9] [12] = 1;69 g [12] [9] = 1;70 g [10] [11] = 1;71 g [11] [10] = 1;72 g [11] [12] = 1;73 g [12] [11] = 1;74 g [16] [17] = 1;75 g [17] [16] = 1;76 g [16] [18] = 1;77 g [18] [16] = 1;78 g [18] [17] = 1;79 g [17] [18] = 1;80 81 82 83 char command;84 int x, y;85 command = f.readChar ();86 while (command != 'q')87 {88 if (command == 'i')89 {90 x = f.readInt ();91 y = f.readInt ();92 g [x] [y] = 1;93 g [y] [x] = 1;94 }95 else if (command == 'd')96 {97 x = f.readInt ();98 y = f.readInt ();99 g [x] [y] = 0;100 g [y] [x] = 0;101 }102 else if (command == 'n')103 {104 x = f.readInt ();105 int count = 0;106 for (int i = 0 ; i < 50 ; i++)107 if (g [x] [i] == 1)108 count++;109 c.println (count);110 }111 else if (command == 'f')112 {113 x = f.readInt ();114 int count = 0;115 count = friendofFriends (g, x);116 c.println (count);117 }118 else if (command == 's')119 {120 x = f.readInt ();121 y = f.readInt ();122 int count = 0;123 count = shortestPath (g, x, y);124 if (count == 999)125 c.println ("Not connected");126 else127 c.println (count);128 }129 command = f.readChar ();130 }131 }132 133 134 135 136 137 138 139 140 public static int friendofFriends (int[] [] g, int x)141 {142 int[] [] q = new int [50] [50];143 int count = 0;144 for (int i = 0 ; i < 50 ; i++)145 for (int j = 0 ; j < 50 ; j++)146 q [i] [j] = g [i] [j];147 148 for (int i = 0 ; i < 50 ; i++)149 if (q [x] [i] == 1)150 for (int j = 0 ; j < 50 ; j++)151 if (q [i] [j] == 1 && j != x && q [x] [j] == 0)152 q [x] [j] = 2;153 154 for (int i = 0 ; i < 50 ; i++)155 if (q [x] [i] == 2)156 count++;157 return count;158 }159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 public static int shortestPath (int[] [] g, int x, int y)174 {175 int[] [] q = new int [50] [50];176 int count = 0;177 for (int i = 0 ; i < 50 ; i++)178 for (int j = 0 ; j < 50 ; j++)179 if (g [i] [j] == 1)180 q [i] [j] = g [i] [j];181 else182 q [i] [j] = 999;183 184 for (int i = 0 ; i < 50 ; i++)185 for (int j = 0 ; j < 50 ; j++)186 if (q [i] [j] > 0)187 for (int k = 0 ; k < 50 ; k++)188 if ((q [j] [k] > 0) && (q [i] [j] + q [j] [k] < q [i] [k]))189 {190 q [i] [k] = q [i] [j] + q [j] [k];191 q [k] [i] = q [i] [j] + q [j] [k];192 }193 194 return q [x] [y];195 }196 197 198 199}200 201