Cow Contest Floyd

来源:互联网 发布:编写吉他谱的软件 编辑:程序博客网 时间:2024/04/27 19:34

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 cowA has a greater skill level than cow B (1 ≤ AN; 1 ≤BN; AB), then cow A will always beat cowB.

Farmer John is trying to rank the cows by skill level. Given a list the results ofM (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 54 34 23 21 22 5

Sample Output

2


N头牛M场比赛结果,求能够确定排名的牛的数量,还真没想到和最短路径有啥联系。。。

Floyd求传递闭包,,,一开始我并不知道有这种操作。要想确定牛的排名,就得知道它与剩下的n-1头牛的胜负关系,所以这里我们不妨将胜看成入度,输看成出度,一头牛与其他牛的胜负关系即度数和为n-1,那么它的排名就确定了

#include <iostream>#include <algorithm>#include <cstdio>#include <cstring>using namespace std;const int N = 110;int g[N][N];int n, m;void Floyd(){    for(int k = 1; k <= n; k++)        for(int i = 1; i <= n; i++)            for(int j = 1; j <= n; j++)                g[i][j] = g[i][j] || (g[i][k] && g[k][j]);  //是否存在胜负关系,这种写法不是我自己想的。}int main(){        int u, v;    memset(g, 0, sizeof(g));    scanf("%d%d", &n, &m);    while(m--)    {        scanf("%d%d", &u, &v);        g[u][v] = 1;    }    Floyd();    int deg, cnt = 0;    for(int i = 1; i <= n; i++)    {        deg = 0;        for(int j = 1; j <= n; j++)        {            if(g[i][j] || g[j][i])  //入度和出度即为胜负关系                deg++;        }        if(deg == n-1)  //与剩下的n-1头牛的胜负关系确定            cnt++;    }    printf("%d", cnt);    return 0;}