题意:有N头牛,M个关系,每个关系A B表示编号为A的牛比编号为B的牛强,问若想将N头牛按能力排名,有多少头牛的名次是确定的。
分析:
1、a[u][v]=1表示牛u比牛v强,flod扫一遍,可以将所有牛的大小关系都存入a。
2、对于每一头牛,cntfront表示比它强的牛的个数,cntrear表示比它弱的牛的个数,若两者相加等于N-1,那该牛的名次自然可以确定。
#pragma comment(linker, "/STACK:102400000, 102400000") #include<cstdio> #include<cstring> #include<cstdlib> #include<cctype> #include<cmath> #include<iostream> #include<sstream> #include<iterator> #include<algorithm> #include<string> #include<vector> #include<set> #include<map> #include<stack> #include<deque> #include<queue> #include<list> #define Min(a, b) ((a < b) ? a : b) #define Max(a, b) ((a < b) ? b : a) const double eps = 1e-8; inline int dcmp(double a, double b){ if(fabs(a - b) < eps) return 0; return a > b ? 1 : -1; } typedef long long LL; typedef unsigned long long ULL; const int INT_INF = 0x3f3f3f3f; const int INT_M_INF = 0x7f7f7f7f; const LL LL_INF = 0x3f3f3f3f3f3f3f3f; const LL LL_M_INF = 0x7f7f7f7f7f7f7f7f; const int dr[] = {0, 0, -1, 1, -1, -1, 1, 1}; const int dc[] = {-1, 1, 0, 0, -1, 1, -1, 1}; const int MOD = 1e9 + 7; const double pi = acos(-1.0); const int MAXN = 100 + 10; const int MAXT = 10000 + 10; using namespace std; int a[MAXN][MAXN]; int ans; int N, M; int solve(){ for(int i = 1; i <= N; ++i){ int cntfront = 0, cntrear = 0; for(int j = 1; j <= N; ++j){ if(a[j][i]) ++cntfront; if(a[i][j]) ++cntrear; } if(cntfront + cntrear == N - 1) ++ans; } return ans; } int main(){ scanf("%d%d", &N, &M); for(int i = 0; i < M; ++i){ int u, v; scanf("%d%d", &u, &v); a[u][v] = 1; } for(int k = 1; k <= N; ++k){ for(int i = 1; i <= N; ++i){ for(int j = 1; j <= N; ++j){ if(a[i][k] && a[k][j]) a[i][j] = 1; } } } printf("%d\n", solve()); return 0; }