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 ≤ N; A ≠ 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.
* 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
* Line 1: A single integer representing the number of cows whose ranks can be determined
5 5 4 3 4 2 3 2 1 2 2 5Sample Output
2
题意:给了n(1<=n<=100)个点,m个关系,例子a b 意思是a永远大于b,然后通过关系排个名,问有多少个点的关系能被确定。
以下 个人废话。(可跳过)
瓜皮的我,一开始把m 4500范围当n范围了,没想Floyd,用了dijkstra,然后还把题意读错了,我以为是那个点的关系能被确认。。。
然后emmm 我也不知道怎么的就输出了个2,自信不造样例测试。提交wa了
以上 个人废话。
言归正传。我们要确认一个点的排名,也就要确认和其他点的关系。并且当且仅当这个点与其他所有点确认关系。
那么自然是判断每个店能不能和其他点有关系。一边for循环遍历所有点。内嵌其他所有点for循环,如果全都有,那么答案个数+1,否则不加。
那么就是个很水很水的,类似最短路问题。Floyd遍历所有点。通过每个点优化一边每两个点的关系。最后遍历所有点得出答案
代码会有注释详解。
#include<stdio.h>
#include<string.h>
#define mx 200
#define inf 0x3f3f3f3f
int n,m;
int e[mx][mx];//定义关系二维数inf代表不可确定,1代表可确定
void floyd()
{
for(int k=1; k<=n; k++)
for(int i=1; i<=n; i++)
for(int j=1; j<=n; j++)
if(e[i][k]&&e[k][j])//如果i->k和k->j的关系可以确定
e[i][j]=1;
return ;
}
int main()
{
int a,b;
int i,j;
while(~scanf("%d%d",&n,&m))
{
memset(e,0,sizeof(e));
for(i=0; i<m; i++)
{
scanf("%d%d",&a,&b);
e[a][b]=1;
}
floyd();
int ans=0;
for(i=1; i<=n; i++)
{
for(j=1; j<=n; j++)//判断点与其他所有点关系是否可以确定
if(i!=j)
if(e[i][j]==0&&e[j][i]==0)//如果存在一个不能确认,那这个点不满足。
break;
if(j>n)
ans++;
}
printf("%d\n",ans);
}
return 0;
}
ps:好久没写博客了,之前一直在刷专题,补题,每两天都有5小时的比赛。太频繁了,弄得我没喘息机会。说实话,不如让我自己刷专题,补知识点,学算法。
2018-05-07 20:09:30