5
January Silver #1: Delivery Route N (1 <= N <= 100) farms located at different (integer) positions in the x-y plane Farmer John wants to visit each farm in sequential order and then return to farm 1 (1, 2, 3, …, N, 1) Takes one minute to make a step either north, south, east, or west You can only cross each farm once Determine the minimum amount of time it’ll take to complete entire delivery route (or -1 if no feasible delivery route is possible)

6
Delivery Route “Minimum time” suggests Dijkstra’s algorithm o Constructing the graph is the hard part Create 5 nodes for each farm, one for the farm itself and the 4 adjacent coordinates Add an edge between every pair of nodes such that it’s possible to go from (x1, y1) to (x2, y2) using a right-angle path w/o intersecting other farms o “Right-angle path” = path that changes direction at most once, moves only up/down/left/right o Runtime of O(N 3 ) works Use this graph w/ Dijkstra’s algorithm o Shortest path between farms is achievable using only these nodes and right-angle paths

7
January Gold #3: Bovine Alliance The cows in each of N farms (1 <= N <= 100,000) were instructed to build a trail to exactly one other farm (N trails total) o However, only M of these trails have been built Farms are arguing over which farms have built trails Calculate the number of ways the M trails could have been built, modulo 1,000,000,007.

8
Bovine Alliance Imagine it as a set of vertices and edges, with edges pointing to the vertex that built the edge o Count number of assignments such that each edge points to some vertex, and each vertex has at most one edge pointing to it Solve the problem for each connected component, multiply the number of possiblities for each together If component has n vertices, there are two different cases for the number of edges: o n-1: component is a tree, number of assignments is n You can leave any of the n cows, and there is one solution for each such possibility o n: component is a cycle, number of assignments is 2 The cycle can point in two directions Use Depth-First Search to find and categorize each connected component o Count node and edges as you DFS

9
December Gold/Silver #1: Cow Photography Farmer John wants to take a photograph of up to 20,000 cows standing in a specific order Every time he’s about to take a picture a group of zero or more cows moves to a set of new positions This happens for 5 photographs o Each cow actively moves in at most one photograph Given the contents of each photograph, reconstruct the original intended order of the cows o This sounds familiar…

10
Cow Photography The solution basically involves sorting the cows Consider two cows from the set, A and B Neither A nor B moved in at least 3 of the 5 photographs Compare the number of times A comes before B o 3 or more: A is before B in original ordering o Else: B is before A Use this as comparator and sort the list

11
PotW No Problem of the Week this week o Practice on your own! :D Submissions for the tiebreaker “Cow Trails” problem over the December break have been graded and ranked Will be announced next meeting, along with prizes for the top scorers of first semester!