1025 [USACO 2008 Jan S]Cow Contest 传递闭包 被打败和打败总共的个数
链接:https://ac.nowcoder.com/acm/contest/26077/1025
来源:牛客网
题目描述
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.
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.
输入描述:
* 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 B
输出描述:
* Line 1: A single integer representing the number of cows whose ranks can be determined
分析
这题和上题一样可以用bitset优化传递闭包来做
上一题只需要算出总共有多少对关系已经被确定。
这题要确定 对于某个单个的牛,它能被打败和打败总共多少个,然后看看这个关系是不是n - 1(也就是囊括了所有牛)这样才能确定它的等级。
如果满足条件,能确定等级的牛的个数+1
//-------------------------代码---------------------------- //#define int ll const int N = 200; int n,m; bitset<N>f[N]; bitset<N>p[N]; void solve() { cin>>n>>m; fo(i,1,m) { int a,b;cin>>a>>b; f[b][a] = 1; p[a][b] = 1; } for(int k = 1;k<=n;k++) { for(int i = 1;i<=n;i++) { if(f[i][k]) { f[i] |= f[k]; } if(p[i][k]) { p[i] |= p[k]; } } }int ans = 0; fo(i,1,n) { // cout<<f[i].count()<<' '; if(f[i].count() + p[i].count() >= n - 1) ans ++ ; } // cout<<endl; cout<<ans<<endl; } void main_init() {} signed main(){ AC();clapping();TLE; cout<<fixed<<setprecision(12); main_init(); // while(cin>>n,n) // while(cin>>n>>m,n,m) // int t;cin>>t;while(t -- ) solve(); // {solve(); } return 0; } /*样例区 */ //------------------------------------------------------------