USACO Silver 2016 Open - Closing the Farm

Authors: Jesse Choe, Maggie Liu

Official Analysis

In this problem, we are asked to determine if all remaining barns are connected as the barns close one at a time. To check this, we can run a depth-first search (DFS) starting from the barn that will close last. To simulate the closure of each barn, we can store a boolean array and set each barn in the iteration to be "closed". Then, we can run the DFS and check how many nodes have been visited. If all the unclosed nodes have been visited, then we print "YES"; otherwise, we print "NO".

C++

#include <bits/stdc++.h>
using namespace std;
using ll = long long;
using vl = vector<ll>;
using vb = vector<bool>;
using pl = pair<ll, ll>;
using vpl = vector<pl>;
using vc = vector<char>;
using vs = vector<string>;

Alternate Implementation

#include <iostream>
#include <cstdio>
#include <vector>
using namespace std;
void dfs(int barn);
vector<bool> visited(3000, false);
vector<bool> open(3000, true);
vector<int> graph[3000];
int main()

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!