Hints

Hint 1

Hint 2

Solution

Official Analysis (C++)

Solution

Video Solution

By Arpan Banerjee

Video Solution Code

Bonus (Silver level)

Suppose that instead of cow ii passing to the nearest cow, cow ii passes to cow pip_i where the array a1,a2,,aNa_1,a_2,\dots,a_N is given as input (1aiN1\le a_i\le N). In other words, the passing graph can be any functional graph.

Solve the problem in O(N)O(N) time.

Join the USACO Forum!

Stuck on a problem, or don't understand a module? Join the USACO Forum and get help from other competitive programmers!