Official Analysis (C++, Java, Python)
Explanation
Let be the hay cow likes.
If there are adjacent hays such that , we can run a focus group on or . This will result in cow and infecting cow or with the type of hay they like. We can keep running focus groups like this to eventually infect every cow with hay .
Situations where will also create adjacent hays of the same type (by running a focus group on ) and therefore this also makes part of the answer.
Implementation
Time Complexity:
C++
#include <bits/stdc++.h>using namespace std;int main() {int test_num;cin >> test_num;for (int t = 0; t < test_num; t++) {int n;cin >> n;vector<int> hay(n);
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!