Enchanted Forest
Time limit2sMemory limit128 MB
Given initial heights and growth rates on an N by N grid, find the largest edge-connected group sharing one height at some present or future real time.
- Level
Hard8 of 10
- Topics
- Union-find, Sorting, Graph
- Solved
- No attempts yet
Problem
Mirko lives in a big enchanted forest where the trees are very tall and grow quickly. The forest is an matrix and every cell holds exactly one tree.
Mirko likes the trees of the enchanted forest. He watched them for years and measured, for every tree, how many meters it grows in a year. The trees grow continuously. A tree that grows 5 meters in a year grows 2.5 meters in half a year.
Mirko also likes the mushrooms of the enchanted forest. Now and then he eats a suspicious colorful mushroom and starts asking odd questions. Yesterday it happened again, and he wondered how large the biggest connected group of trees of equal height would be if the trees kept growing at their current speed.
He measured the current height of every tree and asked you for the answer.
- Two trees are adjacent if their cells share a common edge.
- Two trees are connected if a sequence of adjacent trees leads from the first one to the second one.
- A group of trees is connected if every pair of trees in the group is connected by such a sequence that uses only trees of the group.
Input
The first line contains the integer ().
The next lines contain integers each. The th integer of the th line is (), the current height in meters of the tree in row and column .
The next lines again contain integers each. The th integer of the th line is (), the number of meters the tree in row and column grows in a year.
The input is large, so use a fast reading method.
Output
Print one line with the number of trees in the largest connected group of trees of equal height.
Note
The moment of the comparison is any real moment from now on, so the best moment can fall inside a year. Trees can reach the same height after 8 months, which is two thirds of a year.