#5241. Problem 1. Tree Boxes

0

Problem 1. Tree Boxes

Problem 1. Tree Boxes

USACO 2019 US Open Contest, Platinum

Farmer John is planning to build NN (1N1051 \leq N \leq 10^5) farms that will be connected by N1N-1 roads, forming a tree. Typically, whenever one of his farms is having an issue he is not told the specific farm that is having an issue. Instead, he is told that one of the farms along the path from some farm AA to another farm BB is having an issue. This is often confusing for Farmer John, as he usually drives offroad tractors and isn't familiar with the road system.

Farmer John considers the location of a farm to be a 2D point. He would prefer to be told that there is a problem in one of the farms in a specified axis-aligned rectangular box. This way Farmer John can decide for himself how to navigate between the farms. Bessie told him that this is a little too ambitious, so he will be satisfied if he is notified with at most two axis-aligned rectangular boxes whose intersection (of farms) is empty and whose union is exactly the farms along the path from AA to BB. You must help Farmer John determine where he should build his farms such that this condition is satisfied.

This is an interactive problem, you will not be using standard (or file) I/O.

Solutions that use standard (or file) I/O will be disqualified.

However, you ARE ALLOWED to use global and static variables. You must implement the following functions to help Farmer John:

  • void addRoad(int A, int B): processes a road between farms AA and BB (0A,BN10 \le A, B \le N - 1).
  • void buildFarms(): determines where Farmer John should build all his farms.
  • void notifyFJ(int A, int B): notifies Farmer John with either one or two boxes that satisfy the aforementioned conditions.

Your implementation of the above functions will be able to call the functions given below. You may assume that notifyFJ\texttt{notifyFJ} will be called QQ times (1Q1051 \leq Q \leq 10^5).

  • int getN(): gets the value of NN.
  • int getQ(): gets the value of QQ.
  • void setFarmLocation(int ID, int X, int Y): determines that Farmer John should build farm IDID (0IDN10 \le ID \le N-1) at location (X,Y)(X,Y) where (1X,YN)(1 \le X, Y \le N ). Should only be called from buildFarms\texttt{buildFarms}.
  • void addBox(int X1, int Y1, int X2, int Y2): adds a box to notify Farmer John where (1X1X2N)(1 \le X1 \le X2 \le N ) and (1Y1Y2N)(1 \le Y1 \le Y2 \le N ). Should only be called from notifyFJ\texttt{notifyFJ}.

The interactive protocol works as follows. First, addRoad\texttt{addRoad} will be called N1N-1 times, to inform your program of the road system. Then, buildFarms\texttt{buildFarms} will be called and you must determine where Farmer John should build each farm and call setFarmLocation\texttt{setFarmLocation} for every farm accordingly. Finally, there will be QQ calls to notifyFJ\texttt{notifyFJ} where you must make either one or two calls to addBox\texttt{addBox} to notify Farmer John.

It is guaranteed there is always a valid way to notify Farmer John using either one or two boxes. The memory limit for this problem is set to 512MB, above the usual 256MB limit.

For a C++ solution, use this template:


#include "grader.h"

void addRoad(int a, int b){
	// Fill in code here
}

void buildFarms(){
	// Fill in code here
}

void notifyFJ(int a, int b){
	// Fill in code here
}

For a Java solution, use this template:


import java.io.IOException;
// If you find it necessary, you may import other standard libraries here.
public class boxes extends Grader {

  	// Copy this exactly:
        
@Override
  	public static void main(String args[]) throws IOException { new boxes().run(); }

        
@Override
  	public void addRoad(int a, int b) {
      // Fill in code here
  	}
        
@Override
  	public void buildFarms(){
      // Fill in code here
	  }
  	
@Override
  	public void notifyFJ(int a, int b){
      // Fill in code here
  	}
}

Sample Interaction Grader calls addRoad(0,1)\texttt{addRoad(0,1)}

Grader calls addRoad(1,2)\texttt{addRoad(1,2)}

Grader calls buildFarms()\texttt{buildFarms()}

Solution calls setFarmLocation(0,1,1)\texttt{setFarmLocation(0,1,1)}

Solution calls setFarmLocation(1,1,2)\texttt{setFarmLocation(1,1,2)}

Solution calls setFarmLocation(2,2,2)\texttt{setFarmLocation(2,2,2)}

Solution ends buildFarms()\texttt{buildFarms()}

Grader calls notifyFJ(0,0)\texttt{notifyFJ(0,0)}

Solution calls addBox(1,1,1,1)\texttt{addBox(1,1,1,1)}

Solution ends notifyFJ(0,0)\texttt{notifyFJ(0,0)}

Grader calls notifyFJ(0,2)\texttt{notifyFJ(0,2)}

Solution calls addBox(1,1,1,2)\texttt{addBox(1,1,1,2)}

Solution calls addBox(2,2,2,2)\texttt{addBox(2,2,2,2)}

Solution ends notifyFJ(0,2)\texttt{notifyFJ(0,2)}

Grader terminates, and solution passes test-case

(Note: if you do not pass the first test case, the grader will indicate this as usual. However, note that the short sample interaction above does not correspond to the first test case or any other).

Problem credits: Spencer Compton