给你n头牛和m种关系,每种关系可得a>b,问有多少头牛可以确定关系。其实就是一个简单的floyd,a>b, b>c, 则a > c;代码如下:
#include<iostream>
#include<cstring>
#include<cmath>
#include<algorithm>using namespace std;int map[105][105], INF = 0x3f3f3f3f;int main(){int n, m;cin >> n >> m;memset( map, INF, sizeof( map ) );int x, y;for( int i = 0; i < m; i++ ){cin >> x >> y;map[x][y] = 1;map[y][x] = -1;}for( int j = 1; j <= n; j++ )for( int i = 1; i <= n; i++ )for( int k = 1; k <= n; k++ ){if( map[i][j] == map[j][k] && ( map[i][j] == 1 || map[i][j] == -1 ) )map[i][k] = map[i][j];}int ans = 0;for( int i = 1; i <= n; i++ ){int sum = 0;for( int j = 1; j <= n; j++ ){if( map[i][j] != INF )sum++;}if( sum == n - 1 )ans++;}cout << ans << endl;return 0;
}