• 洛谷 P2419 [USACO08JAN]牛大赛Cow Contest


    题目背景

    [Usaco2008 Jan]

    题目描述

    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的N(1 <= N <= 100)头奶牛们最近参加了场程序设计竞赛:)。在赛场上,奶牛们按1..N依次编号。每头奶牛的编程能力不尽相同,并且没有哪两头奶牛的水平不相上下,也就是说,奶牛们的编程能力有明确的排名。 整个比赛被分成了若干轮,每一轮是两头指定编号的奶牛的对决。如果编号为A的奶牛的编程能力强于编号为B的奶牛(1 <= A <= N; 1 <= B <= N; A != B) ,那么她们的对决中,编号为A的奶牛总是能胜出。 FJ想知道奶牛们编程能力的具体排名,于是他找来了奶牛们所有 M(1 <= M <= 4,500)轮比赛的结果,希望你能根据这些信息,推断出尽可能多的奶牛的编程能力排名。比赛结果保证不会自相矛盾。

    输入输出格式

    输入格式:

    第1行: 2个用空格隔开的整数:N 和 M

    第2..M+1行: 每行为2个用空格隔开的整数A、B,描述了参加某一轮比赛的奶 牛的编号,以及结果(编号为A,即为每行的第一个数的奶牛为 胜者)

    输出格式:

    第1行: 输出1个整数,表示排名可以确定的奶牛的数目

    输入输出样例

    输入样例#1:
    5 5
    4 3
    4 2
    3 2
    1 2
    2 5
    输出样例#1:
    2
    

    说明

    输出说明:

    编号为2的奶牛输给了编号为1、3、4的奶牛,也就是说她的水平比这3头奶

    牛都差。而编号为5的奶牛又输在了她的手下,也就是说,她的水平比编号为5的

    奶牛强一些。于是,编号为2的奶牛的排名必然为第4,编号为5的奶牛的水平必

    然最差。其他3头奶牛的排名仍无法确定。

    屠龙宝刀点击就送

    做法1 floyd

    若与点i连通的点的个数等于n-1(除了他) 那么他的排名就可以确认

    #include <cstdio>
    #include <vector>
    #define N 105
    using namespace std;
    int ans[N],n,m;
    int Map[N][N];
    int main()
    {
        scanf("%d%d",&n,&m);
        for(int a,b;m--;)
        {
            scanf("%d%d",&a,&b);
            Map[a][b]=1;
        }
        for(int k=1;k<=n;++k)
         for(int i=1;i<=n;++i)
          for(int j=1;j<=n;++j)
           Map[i][j]=Map[i][k]&Map[k][j]|Map[i][j];//==if(Map[i][k]&&Map[k][j]) Map[i][j]=1;
        for(int i=1;i<=n;++i)
         for(int j=1;j<=n;++j)
          if(Map[i][j]) ans[i]++,ans[j]++;
        int Answer=0;
        for(int i=1;i<=n;++i) if(ans[i]==n-1) Answer++;
        printf("%d
    ",Answer);
        return 0;
    }

    做法2 

    并查集+拓扑排序

    如果一个点可以被唯一确定,那么所有点一定属于一个连通块 可以想到并查集

    如果满足不了上面那个 那就要拓扑排序了

    Map数组指胜负关系 path数组指可确定的排名关系

    #include <cstdio>
    #include <vector>
    #include <queue>
    #define N 105
    
    using namespace std;
    int fa[N],rd[N],Map[N][N],path[N][N],n,m;
    int find_(int x) {return x==fa[x]?x:fa[x]=find_(fa[x]);}
    int tppx()
    {
        int ret=0,que[N<<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]];//==if(path[now][que[j]]) path[i][que[j]]=1;
                }
            }
        }
        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
    ");
        else printf("%d
    ",tppx());
        return 0;
    }
    我们都在命运之湖上荡舟划桨,波浪起伏着而我们无法逃脱孤航。但是假使我们迷失了方向,波浪将指引我们穿越另一天的曙光。
  • 相关阅读:
    Spring Session Logout
    VC6.0 error LNK2001: unresolved external symbol _main解决办法
    JDBC数据库编程基本流程
    JDBC连接SQL Server2008
    JDK的下载、安装及环境变量的配置
    奇数阶幻方口诀
    ping: sendto: Network is unreachable
    new/delete和malloc/free的区别
    MFC的消息管理
    VC++之自定义消息
  • 原文地址:https://www.cnblogs.com/ruojisun/p/7419527.html
Copyright © 2020-2023  润新知