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.

输入描述:

* 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
示例1

输入

复制
5 5
4 3
4 2
3 2
1 2
2 5

输出

复制
2

说明

Cow 2 loses to cows 1, 3, and 4. Thus, cow 2 is no better than any of the cows 1, 3, and 4. Cow 5 loses to cow 2, so cow 2 is better than cow 5.  Thus, cow 2 must be fourth, and cow 5 must be fifth. The ranks of the other cows cannot 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;
}

/*样例区


*/

//------------------------------------------------------------

 

posted @ 2022-08-19 18:26  er007  阅读(25)  评论(0编辑  收藏  举报