题目描述
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 BOutput
* Line 1: A single integer representing the number of cows whose ranks can be determined
题目大意:n头牛有 n 个互不相等的等级,给你 n 头牛之间他们等级高低的关系,问你可以确定多少头牛的等级。
解题思路:一头牛可以确定等级当且仅当这一头牛与其他 n - 1 头牛的关系都确定,我们可以用一幅图来描述这些牛之间的关系,即如果 a 比 b 等级高,即 mp[ a ] [ b ] = 1,反之则为 mp [ a ][ b ] = -1。再通过Floyed 进行有条件的松弛,来得到每头牛之间可以得知的所有关系,最后遍历每一头牛与其他牛之间的关系,如果都存在关系就可以确定这头牛的等级。
注意建图时要添加双向边。
代码:
#include <iostream>
#include <cstdio>
#include <fstream>
#include <algorithm>
#include <cmath>
#include <deque>
#include <vector>
#include <queue>
#include <string>
#include <cstring>
#include <map>
#include <stack>
#include <set>
#define ll long long
#define ull unsigned long long
#define MOD 998244353
#define INF 0x3f3f3f3f
#define mem(a,x) memset(a,x,sizeof(a))
#define ios ios::sync_with_stdio(false);cin.tie(0);cout.tie(0);
using namespace std;
int mp[105][105];
int n,m;
void Floyed()
{
for(int k=1;k<=n;k++){
for(int i=1;i<=n;i++){
for(int j=1;j<=n;j++){
if(mp[i][k]==1&&mp[j][k]==-1){
mp[i][j]=1;
mp[j][i]=-1;
}else if(mp[j][k]==1&&mp[i][k]==-1){
mp[i][j]=-1;
mp[j][i]=1;
}
}
}
}
}
int main()
{
cin>>n>>m;
mem(mp,INF);
for(int i=1;i<=n;i++)mp[i][i]=0;
for(int i=1;i<=m;i++){
int a,b;
scanf("%d %d",&a,&b);
mp[a][b]=1;
mp[b][a]=-1;
}
Floyed();
/*/ for(int i=1;i<=n;i++){
for(int j=1;j<=n;j++){
cout<<mp[i][j]<<" ";
}
cout<<endl;
} /*/
int sum=0;
for(int i=1;i<=n;i++){
int k=1;
for(int j=1;j<=n;j++){
if(mp[i][j]==INF){
k=0;
break;
}
}
if(k){
sum++;
}
}
cout<<sum;
return 0;
}