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 ≤ 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.
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
题意:给你几头牛,然后告诉你几组数据,每组数据两个数代表两头牛,第一个数代表的牛比第二个数代表的牛更牛逼,然后最后问你有多少头牛和其它全部的牛关系已经确定(比如告诉你1>2,3>2,只知道2是最小的,但1和3的不能确定,此时答案为1,但是如果告诉你1>2,2>3,那么就可以知道1>2>3,此时1,2,3之间的关系都能确定答案就是3)
思路:用floyd,如果1>2的话,就让mapp[1][2]=1,mapp[2][1]=-1,表示1比2牛逼,如果mapp[1][2]=1,mapp[2][3]=1,就表示1>2,2>3,此时就能知道1>3,即mapp[1][3]=1,然后全部遍历一下就行了
#include<stdio.h>
#include<stack>
#include<iostream>
#include<algorithm>
#include<queue>
using namespace std;
int mapp[105][105],inf=99999999,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++)
{
if((mapp[i][k]==mapp[k][j])&&mapp[i][k]!=inf)
{
mapp[i][j]=mapp[i][k];
}
}
}
}
int time=0;
for(int i=1;i<=n;i++)
{
int sum=0;
for(int j=1;j<=n;j++)
{
if(mapp[i][j]!=inf)
sum++;
if(sum==n-1)
{
time++;
break; //break不要忘记
}
}
}
printf("%d\n",time);
return ;
}
int main()
{
scanf("%d%d",&n,&m);
int p,q;
for(int a=1;a<=n;a++)
for(int b=1;b<=n;b++)
mapp[a][b]=inf;
for(int a=1;a<=m;a++)
{
scanf("%d%d",&p,&q);
mapp[p][q]=1,mapp[q][p]=-1;
}
floyd();
return 0;
}