USACO Silver 2019 February - The Great Revegetation

Authors: Sofia Yang, Ryan Chou, Jay Fu


Official Analysis (C++)

Assume that we have a valid solution composed of KK connected components in our graph.

Let's look at one of these connected components, which could look something like this:

possible connected component

However, for each of the KK connected components, the grass types can be inverted and still be a valid solution:

inverted connected component

There are 22 possible ways to arrange each connected component. Thus, if there's kk connected components, there's 2k2^k ways to arrange the entire graph.

Note how the nodes force their adjacent 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 2k2^k.

C++

#include <bits/stdc++.h>
using namespace std;
int main() {
freopen("revegetate.in", "r", stdin);
freopen("revegetate.out", "w", stdout);
int n, m;
cin >> n >> m;

Java

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!