#P9287. Virus Spread and Stability
Virus Spread and Stability
Virus Spread and Stability
You are given n cells and n viruses. Initially, virus i infects cell i for 1 ≤ i ≤ n. Each cell j has a certain infection vulnerability score for every virus. We denote by \(a_{j,v}\) the vulnerability score of cell j to virus v.
The infection process is governed by the following rule: when a cell \(A\) (which currently carries virus \(v\)) attacks another cell \(B\) (which currently carries virus \(u\)), the attack is successful and cell \(B\) becomes infected by virus \(v\) if the following condition holds:
[ a_{B,v} > a_{B,u} ]
The cells may attack in an arbitrary order. The game ends if and only if no cell can be infected by any virus using the above rule.
We say that virus \(i\) is feasible if there exists some attack order during which virus \(i\) ends up infecting at least one cell at the end of the process. Similarly, virus \(i\) is called stable if for every possible attack order virus \(i\) ends up with at least one cell in the final configuration.
An important observation is about the final configuration: since no further infection can occur, for every cell \(j\) that ends with virus \(v\), it must hold that \(a_{j,v}\) is at least as large as every other \(a_{j,x}\). In other words, the final occupant virus for a cell must achieve the maximum vulnerability score in that cell’s row. In particular, virus \(i\) will never lose its own cell if \(a_{i,i} = \max_{1 \le x \le n} a_{i,x}\).
Thus, one can prove that:
- Virus \(i\) is stable if and only if \(a_{i,i} = \max_{1 \le x \le n} a_{i,x}\).
- Virus \(i\) is feasible if either \(a_{i,i} = \max_{1 \le x \le n} a_{i,x}\) or there exists some cell \(j\) (possibly \(j \ne i\)) such that \(a_{j,i} = \max_{1 \le x \le n} a_{j,x}\) and additionally \(a_{j,i} > a_{j,j}\). The condition \(a_{j,i} > a_{j,j}\) guarantees that cell \(j\) can be taken over by virus \(i\) when it is attacked.
Your task is to compute the number of feasible viruses and the number of stable viruses.
inputFormat
The first line of input contains an integer n representing the number of cells/viruses. The following n lines each contain n space-separated integers. The j-th integer on the i-th line represents the vulnerability score \(a_{i,j}\) of cell i to virus j.
outputFormat
Output two integers separated by a space: the number of feasible viruses and the number of stable viruses.
sample
2
1 2
3 2
2 0