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.
【题目分析】
从cow看出来这是从usaco上找的题目。大概的意思是说告诉你一些牛的实力的关系,问你有多少牛的排名可以确定。只需要跑一边floyd,如果这头牛比其他牛强的数目+其他牛比这头牛强的数目==n-1就可以确定排名了。思路很巧妙,就是用floyd传递了闭包。
【代码】
#include <cstdio>
#include <cstring>
#include <iostream>
#include <algorithm>
int map[101][101];
using namespace std;
int main()
{
int n,m;
while (cin>>n>>m)
{
memset(map,0,sizeof map);
for (int i=1;i<=m;++i)
{
int a,b;
cin>>a>>b;
map[a][b]=1;
}
for (int k=1;k<=n;++k)
for (int i=1;i<=n;++i)
for (int j=1;j<=n;++j)
map[i][j]|=(map[i][k]&map[k][j]);
int ans=0;
for (int i=1;i<=n;++i)
{
int cnt=0;
for (int j=1;j<=n;++j) {if (map[i][j]) cnt++; if (map[j][i]) cnt++;}
if (cnt==n-1) ans++;
}
cout<<ans<<endl;
}
}
本文介绍了一道关于牛参与编程比赛并根据比赛结果确定牛的技能排名的问题。通过使用Floyd算法来传递实力关系,最终确定能够精确排名的牛的数量。
288

被折叠的 条评论
为什么被折叠?



