#P6405. [COCI 2014/2015 #2] ŠUMA
[COCI 2014/2015 #2] ŠUMA
Description
A forest can be modeled as an matrix, where each element describes the height of a tree.
Mirko measured how many meters each tree grows in one year. Note: if a tree grows meters in one year, then it grows meters in half a year.
Mirko wants to know: given the current height and growth rate of every tree in the forest, if these trees continue to grow at their current rates, what is the maximum possible number of trees in a connected group whose heights are all equal.
Two trees, or a group of trees, are considered connected as follows:
- If two trees share a common edge in the matrix, then they are adjacent.
- If there is a sequence of adjacent trees from the first tree to the second tree, then the two trees are connected.
- If every pair of trees in the group is connected, then the group of trees is connected.
Input Format
The first line contains a single integer .
The next lines each contain integers. The number in row and column is , representing the initial height of the tree at position , in meters.
The next lines each contain integers. The number in row and column is , representing the growth rate of the tree at position , in meters.
Because the input is large, please use a faster input method.
Output Format
Output a single line containing one integer, which is the answer.
3
1 2 3
3 2 2
5 2 1
3 2 1
1 2 1
1 2 3
7
2
3 1
3 3
2 5
2 5
3
Hint
Explanation of Sample 2
After months, the heights of the trees at will become meters. These trees form a connected group, so the output should be 3.
Constraints
- For of the testdata, .
- For of the testdata, .
For all valid and , .
Note
This problem is translated from COCI2014-2015 CONTEST #2 T5 ŠUMA.
Translated by ChatGPT 5
京公网安备 11011102002149号