Probable winners

5

1 votes
2D dynamic programming, Dynamic Programming, Algorithms
Problem

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 (N1) 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×N. Element Cij is 1 if Playeri defeats Playerj in the pre-tournament clash and Element Cij is 0 if Playeri gets defeated in the pre-tournament clash. Cij 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

1T2

1N2000

0Cij1

 

Time Limit: 3
Memory Limit: 256
Source Limit:
Explanation

Using the matrix we can see that winner of matches between:

  • Player 1 and Player 2 was Player 1
  • Player 1 and Player 3 was Player 3
  • Player 2 and Player 3 was Player 2

Player will stand as P1,P2,P3
There are two cases:

Case 1: First match is between P1 and P2, winner will be P1, so now sequence of standing will be P1,P3 and now a match between them, we know P3 will be winner.
Case 2: First match is between P2 and P3, winner will be P2, so now sequence of standing will be P1,P2 and now a match between them, we know P1 will be winner.

Answer is 2 as P1 and P3 can win (Non-zero chance of winning), there is no way that P2 can win.

Editor Image

?