Http://poj.org/problem? Id = 3660
N(1 ≤N≤ 100) cows, conveniently numbered 1 ..N, Are participating in a programming contest. As we all know, some cows Code better than others. Each cow has a certain constant skill rating that is unique among the competitors.
The contest is conducting CTED in several head-to-head rounds, each between two cows. If cowAHas a greater skill level than cowB(1 ≤A≤N; 1 ≤B≤N;A=B), Then cowAWill always beat cowB.
Farmer John is trying to rank the cows by skill level. Given a list the resultsM(1 ≤M≤ 4,500) Two-cow rounds, determine the number of cows whose ranks can be precisely determined from the results. It is guaranteed that the results of the rounds will not be contradic.pdf.
Input
* Line 1: two space-separated integers:NAndM
* Lines 2 ..M+ 1: Each line contains two space-separated integers that describe the competitors and results (the first integer,A, Is the winner) of a single round of competition:AAndB
Output
* Line 1: A single integer representing the number of cows whose ranks can be determined
Sample Input
5 54 34 23 21 22 5
Sample output
2
Question: $ Floyd $ if the winner of the algorithm is added with the sum of $ n-1 $, it is the person who can determine the position.
Code:
# Include <stdio. h> # include <string. h> # include <iostream> # include <math. h> using namespace STD; int n, m; int MP [110] [110]; void Floyd () {for (int K = 1; k <= N; k ++) {for (INT I = 1; I <= N; I ++) {If (MP [I] [k]) for (Int J = 1; j <= N; j ++) {If (MP [k] [J] = 1 & MP [I] [k] = 1) {MP [I] [J] = 1; MP [J] [I] =-1 ;} else if (MP [k] [J] =-1 & MP [I] [k] =-1) {MP [I] [J] =-1; MP [J] [I] = 1;} else continue ;}}} int main () {memset (MP, 0, sizeof (MP )); scanf ("% d", & N, & M); For (INT I = 1; I <= m; I ++) {int A, B; scanf ("% d", & A, & B); MP [a] [B] = 1; MP [B] [a] =-1 ;} floyd (); int ans = 0; For (INT I = 1; I <= N; I ++) {int sum = 0; For (Int J = 1; j <= N; j ++) if (MP [I] [J]) sum ++; If (sum = n-1) ans ++ ;} printf ("% d \ n", ANS); Return 0 ;}
Poj 3360 H-cow contest