目录

USACO 2017 Dec Gold P2

USACO analysis

Mark Chen | 15 Dec 2020

Problem 2. Barn Painting

Problem Description

Farmer John has $N$ barns connected together. (It is guaranteed that the connected graph does NOT contain a cycle) and three different paints. He has already painted $K$ of $N$ barns. Find out the number of valid ways he can paint the remaining barns.

Proposed Solution

We can solve this problem recursively. Let $f(n, c)$ represent the number of possible valid solutions of painting on a subtree with root $n$ and color $c$ on root, we can represent this function recursively.

\[\begin{aligned} &U = \left\{n' \mid n' \text{ is child of }n\right\}\\ &f(n, c) = \prod_{u\in U}\left({\sum_{c'\in C}{f(u, c')}}\right) \end{aligned}\]

When we meet a node that is already been painted, we let $f(node, c) = 0$ if $c$ is not the color that is painted.

Time Complexity Analysis

For each node, we will calculate $f(n, c)$ for at most three times (with three different $c$). Therefore, the time complexity will be $O(3N)$.


评论区