USACO Bronze 2019 December - Milk Factory
Author: Mrinall Umasudhan and Jesse Choe
Appears In
This problem can also be solved using Depth-First Search (a Silver topic). Represent the factory as a directed unweighted graph with edges from to for all . For every node , we start a DFS at node and check whether this results in every other node being visited. We can keep track of whether the current DFS visits all nodes with a boolean array whose entries are initially set to false
. If every node is visited, then we print . Otherwise, if there exists no such that this condition is satisfied, then we print .
Implementation
Java
import java.io.*;import java.util.*;class Graph{private int V;private ArrayList<ArrayList<Integer>> adj;Graph(int V){this.V = V;adj = new ArrayList<ArrayList<Integer>>(V);for(int i = 0; i < V; i++){
C++
#include <bits/stdc++.h>using namespace std;using ll = long long;using str = string;using bb = bool;using vl = vector<ll>;using pl = pair<ll, ll>;using vb = vector<bb>;using vs = vector<str>;
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!