poj-3660 Cow Contest (floyed transitive closure algorithms)

http://poj.org/problem?id=3660

 

Description

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 conducted in several head-to-head rounds, each between two cows. If cow A has a greater skill level than cow B (1 ≤ A ≤ N; 1 ≤ B ≤ NA ≠ B), then cow A will always beat cow B.

Farmer John is trying to rank the cows by skill level. Given a list the results of M (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 contradictory.

Input

* Line 1: Two space-separated integers: N and M
* 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: A and B

Output

* Line 1: A single integer representing the number of cows whose ranks can be determined
 

Sample Input

5 5
4 3
4 2
3 2
1 2
2 5

Sample Output

 2

Explanation

Cow 2 loses to cows 1, 3, and 4. Thus, cow 2 is no better than any of the cows 1, 3, and 4. Cow 5 loses to cow 2, so cow 2 is better than cow 5. Thus, cow 2 must be fourth, and cow 5 must be fifth. The ranks of the other cows cannot be determined.

 

The meaning of problems: there are N cows, evaluation of N levels vary, given the level of the first part of the relationship between levels of cattle, asked how many head of cattle at most levels can be determined

Problem-solving ideas:
A cow level, only when it is determined to determine the other N-1, and when the relationship with the cow.
So we can be seen as a hierarchical relationship of FIG cattle, and appropriate relaxation operation (seek at floyed transitive closure), to obtain the relationship between any two points, and then can be checked for each cow.
 
 1 #include <stdio.h>
 2 #include <string.h>
 3 #include <iostream>
 4 #include <string>
 5 #include <math.h>
 6 #include <algorithm>
 7 #include <queue>
 8 const int INF=0x3f3f3f3f;
 9 using namespace std;
10 int D[101][101];
11 
12 int main()
13 {
14     int n,m;
15     Scanf ( " % D% D " , & n-, & m);
 16      // input information 
. 17      for ( int I = . 1 ; I <= m; I ++ )
 18 is      {
 . 19          int A, B;
 20 is          Scanf ( " % D% D " , A &, & B);
 21 is          D [A] [B] = . 1 ; // A level greater than or B 
22 is          D [B] [A] = - . 1 ; // B level is less than A 
23 is      }
 24      
25      // appropriate relaxation operation (seek at floyed transitive closure) 
26 is      for(int k=1;k<=n;k++)
27     {
28         for(int i=1;i<=n;i++)
29         {
30             for(int g=1;g<=n;g++)
31             {
32                 if(D[i][k]==1&&D[k][g]==1)
33                     D[i][g]=1;
34                 else if(D[i][k]==-1&&D[k][g]==-1)
35                     D [I] [G] = - . 1 ;
 36                      
37 [              }
 38 is          }
 39      }
 40      
41 is      // statistical answer 
42 is      int ANS = 0 ;
 43 is      for ( int I = . 1 ; I <= n-; I ++ )
 44 is      {
 45          int G ;
 46 is          for (G = . 1 ; G <= n-; G ++ )
 47          {
 48              IF (!! I = G && D [I] [G]) // except when i = g, D [i] [g] 0 g i to the relationship not determined 
49                  BREAK ;
50          }
 51 is          IF (n-== G + . 1 ) // description of the rest of bovine cattle has determined relationship 
52 is              ANS ++ ;
 53 is      }
 54 is      the printf ( " % D \ n- " , ANS);
 55      return  0 ;
 56 is }

 

Guess you like

Origin www.cnblogs.com/jiamian/p/11261751.html