Probable winners
Practice
5 (1 votes)
2d dynamic programming
Dynamic programming
Algorithms
Problem
79% Success 324 Attempts 50 Points 3s Time Limit 256MB Memory 1024 KB Max Code

There are \(N\) players competing in a tournament. Before the start of the tournament, there will be pre-tournament clashes in which all the players must play against each other, the winner in each of these games was noted, and this used to decide the winner of the original tournament.

Now, the tournament has started and all the players will be standing in a line sequentially such as \(1, 2, 3,\ ...N\). To decide the winner, the following operation will be performed for \((N-1)\) times:

  • Select any two adjacent players with equal probability. The player who won the pre-tournament game between these two will survive and the other player will be out of the tournament and will be removed from the line.

The player who remains at the end will be declared the winner. Your task is to find the number of players who have a non-zero probability of winning the tournament.

You will be given a two-dimensional matrix \(C\) of size \(N\times N\). Element \(C_{ij}\) is 1 if Playeri defeats Playerj in the pre-tournament clash and Element \(C_{ij}\) is 0 if Playeri gets defeated in the pre-tournament clash. \(C_{ij}\) will be 0 when \(i=j\).

Input format

  • The first line contains an integer \(T\) denoting the number of test cases.
  • The first line of each of the test case contains an integer \(N\) denoting the number of players.
  • Each of the next \(N\) lines contains \(N\) integers denoting matrix \(C\).

Output format

For each test case, print a single line denoting the number of players having a non-zero probability of winning.

Constraints

\(1 \leq T \leq 2\)

\(1 \leq N \leq 2000\)

\(0 \leq Cij \leq 1\)

 

Please login to use the editor

You need to be logged in to access the code editor

Loading...

Please wait while we load the editor

Loading...
Results
Custom Input
Run your code to see the output
Submissions
Please login to view your submissions
Similar Problems
Points:50
5 votes
Tags:
Dynamic ProgrammingAlgorithms2D dynamic programming
Points:50
4 votes
Tags:
2D dynamic programmingMathamaticsAlgorithmsData StructuresDynamic Programming
Points:50
2 votes
Tags:
2D dynamic programmingAlgorithmsDynamic Programming