题意:
N个选手,如果A比B强,B比C强,则A必比C强
告知若干个强弱关系,问有多少人的排名可以确定思路:
如果一个点u, 有x个点能到达此点,从u点出发能到达y个点,若x+y=N-1,则u点的排名是确定的。用floyd算出每两个点之间的距离,最后统计,若dist[a][b] 无穷大且dist[b][a]无穷大,则a和b的排名都不能确定。最后用点个数减去不能确定点的个数即可。
实现:
1 #include2 #include 3 #include 4 using namespace std; 5 6 const int INF = 0x3f3f3f3f; 7 int n, m, a[105][105], x, y; 8 int main() 9 {10 cin >> n >> m;11 for (int i = 1; i <= n; i++)12 {13 for (int j = 1; j <= n; j++)14 {15 a[i][j] = INF;16 }17 a[i][i] = 0;18 }19 for (int i = 0; i < m; i++)20 {21 cin >> x >> y;22 a[x][y] = 1;23 }24 for (int k = 1; k <= n; k++)25 {26 for (int i = 1; i <= n; i++)27 {28 for (int j = 1; j <= n; j++)29 {30 if (a[i][k] + a[k][j] < a[i][j])31 {32 a[i][j] = a[i][k] + a[k][j];33 }34 }35 }36 }37 set s;38 for (int i = 1; i <= n; i++)39 {40 for (int j = i + 1; j <= n; j++)41 {42 if (a[i][j] == INF && a[j][i] == INF)43 {44 s.insert(i);45 s.insert(j);46 }47 }48 }49 cout << n - s.size() << endl;50 return 0;51 }