USACO Bronze 2019 December - Milk Factory

Author: Mrinall Umasudhan and Jesse Choe

Official Analysis

This problem can also be solved using Depth-First Search (a Silver topic). Represent the factory as a directed unweighted graph with edges from bib_i to aia_i for all ii. For every node i[1,N]i\in [1,N], we start a DFS at node ii 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 ii. Otherwise, if there exists no ii such that this condition is satisfied, then we print 1-1.

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!