Nyoj 110 Swordsman Duel

Source: Internet
Author: User

Describe

in the era of Louis 13 and Cardinal Richelieu in power, a duel took place. n individuals stand in a circle and draw lots. The man in the smoke fights with the man on his right, and the negative is out of the circle. The final outcome of this duel depends crucially on the order of the duel. Now books in any two duel who can win the message, but "a win B" this relationship is not transitive. For example, A is stronger than B, B is stronger than C, and C is stronger than a. If A and B duel first, C will eventually win, but if B and C duel earlier, then finally a will win. Clearly, the first duel among the three had a direct impact on the final outcome. Suppose that now the n person is surrounded by a circle, numbered 1 ~n sequentially. There was a duel of n-1 field altogether. In the first field, one of them (set I) and the person on his right (i.e., i+1, if i=n, the right person is number 1th). The negative is eliminated from the circle, and the person next to him complements his position. The strong and weak relationship between the known n individuals (i.e., the winning or losing relationship between any two persons). If there is a lottery to allow the K-person to win, we say that the K-man is likely to win, and our task is to determine the number of possible winners based on the strength and weakness of n individuals.

Input
The first line is an integer N (1<=n<=) that represents the number of groups of test data. The second line is an integer n representing the total number of duels. (2<=n<=) The next n row is a matrix of n rows n columns, and if 1 is the first row in the matrix, the first person will win when I am in a duel with the J person. 0 indicates that the first person and the first person will fail when they duel with the J-man. 

Output
For each set of test data, the number of people that the output might win, one row for each set of outputs

Sample input
1 3 0 1 0 0 0 1 1 0 0

Sample output
3

Think of the ring as a chain
Dynamic programming problems, similar to Freud's algorithm
Exercises
The person with the number X can win from everyone, the necessary condition is that he can meet himself,
That is, the ring as a chain, X-point split into two at both ends of the chain, the middle of the people are eliminated, X remains unbeaten.
In this way, in the chain of several consecutive people, only to consider whether the two people can win the reunion, the middle is not considered,
Thus, one-dimensional state representation is reduced.
Set MEET[I,J] record I and J can meet, can meet is true, otherwise false. The state transition equation is
if (exists meet[i][t] && meet[t][j]) && (fight[i][t] | | fight[j][t]=true) && i < T < j)
MEET[I][J] = true;
Else
MEET[I][J] = Falze;

AC Code:

1#include <iostream>2#include <cstdio>3#include <cstring>4 using namespacestd;5 #defineN 5066 intN;7 intMp[n][n];8 intDp[n][n];//Dp[i][j]9 intMain ()Ten { One     intT; Ascanf"%d",&t); -      while(t--){ -scanf"%d",&n); the         for(intI=0; i<n;i++){ -            for(intj=0; j<n;j++){ -scanf"%d",&mp[i][j]); -           } +        } -Memset (DP,0,sizeof(DP)); +         for(intI=0; i<n;i++){ Adp[i][(i+1)%n]=1; at        } -  -         for(intLen =1; len<n;len++){ -            for(intI=0; i<n;i++){ -              intj = (i+len+1)%N; -              if(Dp[i][j])Continue; in               for(intk= (i+1)%n;k!=j;k++,k%=N) { -                   if(Dp[i][k] && dp[k][j] && (Mp[i][k] | |Mp[k][j])) { todp[i][j]=1; +                       Break; -                   } the              } *           } $        }Panax Notoginseng  -        intAns =0; the         for(intI=0; i<n;i++){ +           if(Dp[i][i]) { Aans++; the           } +        } -printf"%d\n", ans); $  $     } -     return 0; -}

Nyoj 110 Swordsman Duel

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.