USACO 2020 December Contest, Silver
Problem 1. Cowntagion
Farmer John and his fellow farmers have been working nonstop to control the spread of the terrible bovine disease COWVID-19 across their farms.
Together, they oversee a collection of NN farms (1≤N≤1051≤N≤105), conveniently numbered 1…N1…N. The farms are connected by a set of N−1N−1 roads such that any farm can be reached from farm 1 by some sequence of roads.
Unfortunately, a cow in farm 1 has just tested positive for COWVID-19. None of the other cows at that farm or at any other farms have the disease yet. However, knowing the contagious nature of the disease, Farmer John anticipates exactly one of the following adverse events on each successive day:
(1) In a single farm, a "superspreader" event causes the number of cows at that farm with COWVID-19 to double; or
(2) A single cow with COWVID-19 moves along a road from one farm to an adjacent farm.
Farmer John is worried about how fast the outbreak might spread. Please help him by determining the minimum possible number of days before it could be the case that at least one cow in every farm has the disease.
INPUT FORMAT (input arrives from the terminal / stdin):
The first line contains the single integer NN. The next N−1N−1 lines each contain two space-separated integers aa and bb describing a road between farms aa and bb. Both aa and bb are in the range 1…N1…N.
OUTPUT FORMAT (print output to the terminal / stdout):
The minimum number of days until the outbreak could reach every farm.
SAMPLE INPUT:
4 1 2 1 3 1 4
SAMPLE OUTPUT:
5
One possible sequence of events corresponding to this example is the following: the number of sick cows in farm 1 doubles and then doubles again, so that after two days, there are 4 sick cows in farm 1. In each of the next 3 days, a sick cow travels from farm 1 to each of farms 2, 3, and 4 respectively. After 5 days, at least 1 sick cow exists at each farm.
SCORING:
- In test cases 1-4, every farm is connected directly to farm 1 (aside from farm 11 itself).
- In test cases 5-7, farms 2…N2…N are each adjacent to at most two roads.
- In test cases 8-15, there are no additional constraints.
Problem 2. Rectangular Pasture
Farmer John's largest pasture can be regarded as a large 2D grid of square "cells" (picture a huge chess board). Currently, there are NN cows occupying some of these cells (1≤N≤25001≤N≤2500).
Farmer John wants to build a fence that will enclose a rectangular region of cells; the rectangle must be oriented so its sides are parallel with the xx and yy axes, and it could be as small as a single cell. Please help him count the number of distinct subsets of cows that he can enclose in such a region. Note that the empty subset should be counted as one of these.
INPUT FORMAT (input arrives from the terminal / stdin):
The first line contains a single integer NN. Each of the next NN lines Each of the next NN lines contains two space-separated integers, indicating the (x,y)(x,y) coordinates of a cow's cell. All xx coordinates are distinct from each-other, and all yy coordinates are distinct from each-other. All xx and yy values lie in the range 0…1090…109.
OUTPUT FORMAT (print output to the terminal / stdout):
The number of subsets of cows that FJ can fence off. It can be shown that this quantity fits within a signed 64-bit integer (e.g., a "long long" in C/C++).
SAMPLE INPUT:
4 0 2 1 0 2 3 3 5
SAMPLE OUTPUT:
13
There are 2424 subsets in total. FJ cannot create a fence enclosing only cows 1, 2, and 4, or only cows 2 and 4, or only cows 1 and 4, so the answer is 24−3=16−3=1324−3=16−3=13.
SCORING:
- Test cases 2-3 satisfy N≤20N≤20.
- Test cases 4-6 satisfy N≤100N≤100.
- Test cases 7-12 satisfy N≤500N≤500.
- Test cases 13-20 satisfy no additional constraints.
Problem 3. Stuck in a Rut
Farmer John has recently expanded the size of his farm, so from the perspective of his cows it is effectively now infinite in size! The cows think of the grazing area of the farm as an infinite 2D grid of square "cells", each filled with delicious grass (think of each cell as a square in an infinite chessboard). Each of Farmer John's NN cows (1≤N≤10001≤N≤1000) starts out in a different cell; some start facing north, and some start facing east.
Every hour, every cow either
- Stops (and then remains stopped from that point on) if the grass in her current cell was already eaten by another cow.
- Eats all the grass in her current cell and moves one cell forward according to the direction she faces.
Over time, each cow therefore leaves a barren "rut" of empty cells behind her.
If two cows move onto the same grassy cell in the same move, they share the cell and continue moving in their respective directions in the next hour.
Farmer John isn't happy when he sees cows that stop grazing, and he wants to know who to blame for his stopped cows. If cow bb stops in a cell that cow aa originally ate, then we say that cow aa stopped cow bb. Moreover, if cow aa stopped cow bb and cow bb stopped cow cc, we say that cow aa also stopped cow cc (that is, the "stopping" relationship is transitive). Each cow is blamed in accordance with the number of cows she stopped. Please compute the amount of blame assigned to each cow -- that is, the number of cows she stopped.
INPUT FORMAT (input arrives from the terminal / stdin):
The first line of input contains NN. Each of the next NN lines describes the starting location of a cow, in terms of a character that is either N (for north-facing) or E (for east-facing) and two nonnegative integers xx and yy (0≤x≤1090≤x≤109, 0≤y≤1090≤y≤109) giving the coordinates of a cell. All xx-coordinates are distinct from each-other, and similarly for the yy-coordinates.
To be as clear as possible regarding directions and coordinates, if a cow is in cell (x,y)(x,y) and moves north, she ends up in cell (x,y+1)(x,y+1). If she instead had moved east, she would end up in cell (x+1,y)(x+1,y).
OUTPUT FORMAT (print output to the terminal / stdout):
Print NN lines of output. Line ii in the output should describe the blame assigned to the iith cow in the input.
SAMPLE INPUT:
6 E 3 5 N 5 3 E 4 6 E 10 4 N 11 1 E 9 2
SAMPLE OUTPUT:
0 0 1 2 1 0
In this example, cow 3 stops cow 2, cow 4 stops cow 5, and cow 5 stops cow 6. By transitivity, cow 4 also stops cow 6.
SCORING:
- In test cases 2-5, all coordinates are at most 20002000.
- In test cases 6-10, there are no additional constraints.