题目大意:有N头牛,编号从1~N,每头牛有各自的实力,他们之间比了M场,实力强的总会获胜。现在按实力排名,你能确定有几头牛的排名。
解题思路:要确定一头牛的排名,说明要知道他跟除他外的所有牛的比赛结果。因为N最多100,数据量不大。用floyd传递闭包解。
ac代码:
#include <iostream>
#include <cstring>
using namespace std;
int n, m, w[105][105], sum, t1, t2;
int main()
{while (scanf("%d%d", &n, &m)!=EOF){memset(w, 0, sizeof(w));sum = 0;for (int i=0; i<m; i++){scanf("%d%d", &t1, &t2);w[t1][t2] = 1;w[t2][t1] = -1;}for (int k=1; k<=n; k++)for (int i=1; i<=n; i++)for (int j=1; j<=n; j++){if (w[i][k] == 1 && w[k][j] == 1)w[i][j] = 1, w[j][i] = -1;else if (w[i][k] == -1 && w[k][j] == -1)w[i][j] = -1, w[j][i] = 1;}for (int i=1; i<=n; i++){t1 = 0;for (int j=1; j<=n; j++)if (!w[i][j])t1++;if (t1 == 1)sum++;}printf("%d\n", sum);}
return 0;
}