链接:https://ac.nowcoder.com/acm/contest/1069/K
来源:牛客网
时间限制:C/C++ 1秒,其他语言2秒
空间限制:C/C++ 32768K,其他语言65536K
64bit IO Format: %lld
题目描述
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
示例1
输入
复制
5 5
4 3
4 2
3 2
1 2
2 5
输出
复制
2
说明
Cow 2 loses to cows 1, 3, and 4. Thus, cow 2 is no better than any of the cows 1, 3, and 4. Cow 5 loses to cow 2, so cow 2 is better than cow 5. Thus, cow 2 must be fourth, and cow 5 must be fifth. The ranks of the other cows cannot be determ
/*
题意大概是:
给出两个人的比赛成绩优劣,
求最终有多少人的名次(排名)可以确定。
构造图:
通过构造成绩低的指向成绩高的单向边来建图
我们发现,只要确定每个点有多少个其他的点可达
就可以确定哪些点的名次是可以确定的。
为什么呢?
我们知道根据我们建的图,
名次越低越少的点可达它,
假设有n个点且所有的点名次都确定,
根据名次由低到高,能到达每个点的个数(除了它本身)一定是唯一确定的:
0,1, 2, 3,4,。。n-1。
所以我们可以根据Floyd求出任意两点的距离,
根据距离判断每个点有多少个可达点。
然后我们对每个点可达点个数排一个序,
根据类似上面举例的序列(0,1, 2, 3,4,。。n-1。)来记录
可以确定名次的人的个数
*/
#include <bits/stdc++.h>
using namespace std;
const int N = 105;
int a[N][N];//i--j的距离
int cnt[N];//到达i号点的个数(除了本身)
int b[N];//辅助排序的数组
int mp[N];//可达点个数的次数,每个能确定名次的点可达点数目的唯一性
int main()
{
int n,m;
cin>>n>>m;
for(int i = 1; i <= n; i++)
{
cnt[i] = 0;
mp[i] = 0;
for(int j = 1; j <= n; j++)
{
if(i==j) a[i][j] = 0;
else a[i][j] = -100000;
}
}
int x,y;
for(int i = 1; i<= m; i++)
{
cin>>x>>y;
a[y][x] = 1;
}
for(int k = 1; k <= n; k++)
{
for(int i = 1; i <= n; i++)
{
for(int j = 1; j <= n; j++)
{
a[i][j] = max(a[i][j],a[i][k]+a[k][j]);
}
}
}
for(int i = 1; i <= n; i++)
{
for(int j = 1; j <= n; j++)
{
if(a[i][j] > 0) cnt[j]++;
}
}
int k = 0;
for(int i = 1; i<= n; i++)
{
b[k++] = cnt[i];
mp[cnt[i]]++;
}
sort(b,b+k);
int ans = 0;
for(int i = 0; i < k; i++)
{
if(b[i]==i&&mp[b[i]]==1)
ans++;
}
cout<<ans<<endl;
return 0;
}