USACO Cow Contest

Luo Gu P2419 [USACO08JAN] cattle contest Cow Contest

https://www.luogu.org/problemnew/show/P2419

JDOJ 2554: USACO 2008 Jan Silver 1.Cow Contest

https://neooj.com:8082/oldoj/problem.php?id=2554

 

 

Title Description

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.

FJ of N (1 <= N <= 100) cows have recently participated in the field Programming Contest :). In the game, the cows are numbered according to 1..N. Each cow programming skills are not the same, and no two cows which level par, that is to say, cows programming capabilities have a clear ranking. The entire game is divided into several rounds, each round showdown two specified number of dairy cows. If the number of cows A of programming ability in the number of cows B, (1 <= A <= N; 1 <= B <= N;! A = B), then their duel numbered cow A, always win. FJ would like to know the specific ranking cows programming ability, so he hired the cows were all the result of M (1 <= M <= 4,500) round, I hope this information you can infer as much as possible in accordance with the programming cows the ability to rank. Results of the competition is guaranteed not contradictory.

Input and output formats

Input formats:

 

Line 1: 2 with a space-separated integers: N and M

The first 2..M + 1 line: Each row two space-separated integers A, B, describe the cow to participate in a round of the number and the results (numbered A, is the first of each line the number of cows is the winner)

 

Output formats:

 

The number of output an integer representing the ranking cows can be determined: Line 1

 

Sample input and output

Input Sample # 1:  Copy
5 5
4 3
4 2
3 2
1 2
2 5
Output Sample # 1:  Copy
2

Explanation

Output Description:

Number of cows lost to No. 2, 3, 4 cows, that her level than these three milk

Cattle are poor. The number of dairy cows 5 and lost in her hands, that is to say, her level than No. 5

Cows stronger. Thus, the ranking number of cows necessarily 4th 2, 5 numbered horizontal cows must

However, the worst. Ranked other three cows is still uncertain.

 

See who beat so and so who beat title decisively think topological sorting.

This question belongs to the topological sort of a distortion problems.

CODE:

#include<cstdio>
#include<vector>
#include<queue>
using namespace std;
int fa[110],rd[110],map[110][110],path[110][110],n,m;
int find(int x) 
{
    return x==fa[x]?x:fa[x]=find(fa[x]);
}
int topsort()
{
    int ret=0,que[110<<4],l=0,r=0;
    for(int i=1;i<=n;++i) 
        if(!rd[i]) 
            que[++r]=i;
    for(int now;l<r;)
    {
        now=que[++l];
        if(l==r)
        {
            int i=1;
            for(i=1;i<=r;++i) 
                if(path[now][que[i]]==0) 
                    break;
            if(i==r+1)
                ret++;
        }
        for(int i=1;i<=n;++i)
            if(map[now][i])
            {
                if(rd[i])
                {
                    rd[i]--;
                    if(!rd[i])
                        que[++r]=i;
                    for(int j=1;j<=r;++j)
                        path[i][que[j]]|=path[now][que[j]];
                }
            }
    }
    return ret;
}
int main()
{
    scanf("%d%d",&n,&m);
    for(int x,y,i=1;i<=m;++i)
    {
        scanf("%d%d",&x,&y);
        fa[find(y)]=find(x);
        rd[y]++;
        map[x][y]=1;
    }
    int k=0;
    for(int i=1;i<=n;++i) 
    {
        if(fa[i]==i) 
            k++;
        path[i][i]=1;
    } 
    if(k>1) 
        printf("0\n");
    else 
        printf("%d\n",topsort());
    return 0;
}

 

 

Guess you like

Origin www.cnblogs.com/fusiwei/p/11206610.html