Explanation
Assume that we have a valid solution composed of connected components in our graph.
Let's look at one of these connected components, which could look something like this:
![]()
However, for each of the connected components, the grass types can be inverted and still be a valid solution:
![]()
There are possible ways to arrange each connected component. Thus, if there's connected components, there's ways to arrange the entire graph.
Note how the nodes force their adjacen t ones to have a specific coloring, so if any two nodes don't satisfy this condition, it's impossible. Otherwise, the solution will always be .
Implementation
import java.io.*;import java.util.ArrayList;import java.util.StringTokenizer;public class Revegetate {public static int[] type;public static boolean impossible;public static ArrayList<Integer>[] same;public static ArrayList<Integer>[] diff;
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!