[Explanations] Pearl

Title Description

  N pearl particle shape and size are the same, their weights are not the same. n is an integer, all the pearls numbered from 1 to n. Your task is to find the weight of pearls which is just the middle, i.e. in the weight of all pearls, pearls in the weight of the column (n + 1) / 2 bits. Here are ways to compare a pair of pearl:

  To give you a scale used to compare the weight of pearl, we can re-some of which is more than two pearl, after making a series of comparisons, we can certainly do not have some middle weight pearls away.

  For example, the following is given of a case where five pearls of four comparisons:

  1, Pearl 1 Pearl 2 weight ratio

  2, 4 pearl pearl than 3 weight

  3, pearl pearl a weight ratio of 5

  4, 4 pearl pearl than 2 weight

  From the above results, although we can not pinpoint the pearls which have an intermediate weight, but we can be sure pearl pearl 1 and 4 may not have an intermediate weight, as a weight ratio of 2,4,5 pearl pearl, pearl and 1,2, 3 lighter than pearls 4, so we can remove these two pearls.

  Write a program that the statistics of the total number of pearls certainly not middle weight.

 

Input Format

  The first line contains two space-separated integers N and M, where 1≤N≤99, and N is an odd number, M is the number of comparisons performed pearl;

  The next row comprises M rows each separated by a space of two integers x and y, x represents a weight ratio of pearl pearl y.

 

Output Format

  Only one line contains an integer representing the total number of pearl can not be an intermediate weight.

 

SAMPLE INPUT

5 4

2 1

4 3

5 1

4 2

 

Sample Output

2

 

answer

  We can each be seen as pearl node, the relationship has to be seen as the side, it will form a directed graph. We can find floyd connectivity between nodes, and then seek a built FIG reverse, so that you know, for any node, which points weight (lighter) than it.

#include <iostream>
#define MAX_N 100

using namespace std;

int n, m;
bool h[MAX_N][MAX_N], l[MAX_N][MAX_N];
int ans;

int main()
{
     cin >> n >> m;
    for(register int i = 1; i <= m; i++)
    {
        int tmp1, tmp2;
        cin >> tmp1 >> tmp2;
        h[tmp1][tmp2] = 1;
        l[tmp2][tmp1] = 1;
    }    
    for(register int k = 1; k <= n; k++)
    {
        for(register int i = 1; i <= n; i++)
        {
            for(register int j = 1; j <= n; j++)
            {
                h[i][j] |= h[i][k] & h[k][j];
                l[i][j] |= l[i][k] & l[k][j];
            }
        }
    }
    for(register int i = 1; i <= n; i++)
    {
        int cnt_h = 0, Cnt_l = 0 ;
        for (register int j = 1 ; j <= n; j ++ ) 
        { 
            cnt_h + = h [i] [j]; 
            cnt_l + = l [i] [j]; 
        } 
        If (cnt_h> n / 2 || cnt_l> n / 2 ) years ++ ; 
    } 
    Cout << years;
    return  0 ; 
}
Reference program

 

Guess you like

Origin www.cnblogs.com/kcn999/p/10988884.html