Cow Contest Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 13189 Accepted: 7337 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 Source
USACO 2008 January Silver
大致题意: 有n头牛, 以及m组胜负关系, 问能通过这m组关系判断出多少头牛的排名
思路: 拿到这道题时, 我想的是对n进行一个拓补排序, 可排序后就不知到该怎么做来进一步判断排名, 然后又想通过判断一个点度数来判断, 发现当1个点与其他n-1个点的关系确定时, 就能确定这个点的具体排名, 但不知具体怎么实现, 于是去网上搜题解发现有个叫Floyd传递闭包的东西, 这题就是简单例题.
知识点 : Floyd传递闭包:
这题是利用Floyd的传递闭包, 来确定一个点与其他所有点是否有关
代码如下:
#include <iostream>
#include <cstring>
#include <string>
#include <iterator>
#include <cmath>
#include <algorithm>
#include <sstream>
#include <map>
#include <stack>
#include <set>
#include <queue>
#include <iomanip>
using namespace std;
stringstream ss;
typedef pair<int, int> PII;
typedef long long ll;
map<string,string> mp;
const int N = 110;
const int INF = 0x3f3f3f3f;
int f,n,m,k;
bool st[N];
int g[N][N],d[N],cnt[N];
void floyd()
{
for(int k = 1; k<=n; k++)
{
for(int i = 1; i<=n; i++)
{
for(int j = 1; j<=n; j++)
{
if(g[i][k] && g[k][j]) g[i][j] = 1;
}
}
}
}
int main()
{
cin>>n>>m;
while(m -- )
{
int a,b;
cin>>a>>b;
g[a][b] = 1;
}
floyd();
int res = 0;
for(int i = 1; i<=n; i++)
{
int cnt = 0;
for(int j = 1; j<=n; j++)
{
if(i != j)
{
if(g[i][j] || g[j][i]) cnt++;
}
}
if(cnt == n-1) res++;
}
cout<<res;
}
|