POJ 3660 Cow Contest(Floyd求传递闭包(可达矩阵))

Cow Contest
Time Limit: 1000MS   Memory Limit: 65536K
Total Submissions: 16341   Accepted: 9146

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 ≤ AN; 1 ≤ BN; AB), 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

Source

 
分析;给你n头牛,m组关系,每组关系形式为A B,代表A比B厉害
问你根据这些关系可以推出有几头牛的名次是确定的
如果一头牛和其他的所有牛的关系都确定了的话,那么该牛的名次也是确定的
关系的确定分两种情况:
比如A和C
1.直接确定 题目直接告诉你了A比C厉害
2.间接确定 题目告诉你A比B厉害,B又比C厉害,那么我们可以推出A比C厉害
所有我们必须考虑间接确定的情况,其实这种间接确定就是一个传递闭包
要求传递闭包的话我们必须确定可达矩阵
可达矩阵:G[i][j]=1的话,代表i和j之间是可以到达的(直接或者间接到达)
可达矩阵可以使用floyd算法确定,虽然Floyd是求最短路的,但是也是可以求传递闭包(可达矩阵的)
 
#include<stdio.h>
#include<iostream>
#include<math.h>
#include<string.h>
#include<set>
#include<map>
#include<list>
#include<queue>
#include<algorithm>
using namespace std;
typedef long long LL;
int mon1[]= {,,,,,,,,,,,,};
int mon2[]= {,,,,,,,,,,,,};
int dir[][]= {{,},{,-},{,},{-,}}; int getval()
{
int ret();
char c;
while((c=getchar())==' '||c=='\n'||c=='\r');
ret=c-'';
while((c=getchar())!=' '&&c!='\n'&&c!='\r')
ret=ret*+c-'';
return ret;
} #define max_v 105
int G[max_v][max_v];
int n,m; void floyd()//求可达矩阵
{
for(int k=;k<=n;k++)
{
for(int i=;i<=n;i++)
{
for(int j=;j<=n;j++)
{
if(i==j)
continue;
if(G[i][k]==&&G[k][j]==)
G[i][j]=;
}
}
}
}
int main()
{
int x,y;
while(~scanf("%d %d",&n,&m))
{
memset(G,,sizeof(G));
for(int i=;i<=m;i++)
{
scanf("%d %d",&x,&y);
G[x][y]=;
}
floyd();
int sum=;
int cnt=;
for(int i=;i<=n;i++)
{
cnt=;
for(int j=;j<=n;j++)
{
//printf("%d ",G[i][j]);
if(G[i][j]||G[j][i])
cnt++;
}
// printf("\n");
if(cnt==n-)//存在某牛和其他n-1头牛的关系直接或间接确定,那么该牛名次确定
sum++;
}
printf("%d\n",sum);
}
return ;
}
/*
分析;给你n头牛,m组关系,每组关系形式为A B,代表A比B厉害
问你根据这些关系可以推出有几头牛的名次是确定的 如果一头牛和其他的所有牛的关系都确定了的话,那么该牛的名次也是确定的
关系的确定分两种情况:
比如A和C
1.直接确定 题目直接告诉你了A比C厉害
2.间接确定 题目告诉你A比B厉害,B又比C厉害,那么我们可以推出A比C厉害 所有我们必须考虑间接确定的情况,其实这种间接确定就是一个传递闭包
要求传递闭包的话我们必须确定可达矩阵
可达矩阵:G[i][j]=1的话,代表i和j之间是可以到达的(直接或者间接到达) 可达矩阵可以使用floyd算法确定,虽然Floyd是求最短路的,但是也是可以求传递闭包(可达矩阵的) */
上一篇:[十二省联考2019]骗分过样例 luoguP5285 loj#3050


下一篇:2017"百度之星"程序设计大赛 - 资格赛【1001 Floyd求最小环 1002 歪解(并查集),1003 完全背包 1004 01背包 1005 打表找规律+卡特兰数】