洛谷 P2419 [USACO08JAN]牛大赛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

题目描述

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头奶牛的排名仍无法确定。

看到谁打败谁谁打败谁的题目就果断想到拓扑排序。

这道题属于拓扑排序的一道变形题目。

CODE:

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

USACO Cow Contest的更多相关文章

  1. POJ 3660 Cow Contest(传递闭包floyed算法)

    Cow Contest Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 5989   Accepted: 3234 Descr ...

  2. POJ-3660.Cow Contest(有向图的传递闭包)

      Cow Contest Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 17797   Accepted: 9893 De ...

  3. POJ 3660 Cow Contest(Floyd求传递闭包(可达矩阵))

    Cow Contest Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 16341   Accepted: 9146 Desc ...

  4. POJ 3660 Cow Contest (dfs)

    Cow Contest Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 11129   Accepted: 6183 Desc ...

  5. POJ 3660 Cow Contest 传递闭包+Floyd

    原题链接:http://poj.org/problem?id=3660 Cow Contest Time Limit: 1000MS   Memory Limit: 65536K Total Subm ...

  6. POJ3660 Cow Contest —— Floyd 传递闭包

    题目链接:http://poj.org/problem?id=3660 Cow Contest Time Limit: 1000MS   Memory Limit: 65536K Total Subm ...

  7. ACM: POJ 3660 Cow Contest - Floyd算法

    链接 Cow Contest Time Limit:1000MS     Memory Limit:65536KB     64bit IO Format:%lld & %llu Descri ...

  8. POJ 3660 Cow Contest

    题目链接:http://poj.org/problem?id=3660 Cow Contest Time Limit: 1000MS   Memory Limit: 65536K Total Subm ...

  9. POJ 3660 Cow Contest (闭包传递)

    Cow Contest Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 7690   Accepted: 4288 Descr ...

随机推荐

  1. [日常] 用vim的时候发现的不是很小的bug...

    前一天晚上的时候不知道搞啥了...第二天早上起来开 gnome-system-monitor 的时候发现CPU占用好像不太对头 (一直有个核是 \(100\%\)), 转到进程的时候发现使用最高的居然 ...

  2. strcasecmp()函数

    函数介绍: strcasecmp用忽略大小写比较字符串.,通过strcasecmp函数可以指定每个字符串用于比较的字符数,strncasecmp用来比较参数s1和s2字符串前n个字符,比较时会自动忽略 ...

  3. JAVA基础系列:ThreadLocal

    1. 思路 什么是ThreadLocal?ThreadLocal类顾名思义可以理解为线程本地变量.也就是说如果定义了一个ThreadLocal,每个线程往这个ThreadLocal中读写是线程隔离,互 ...

  4. Linux 网络通信 API详解【转载】

    TCP/IP分层模型 OSI协议参考模型,它是基于国际标准化组织(ISO)的建议发展起来的, 它分为7个层次:应用层.表示层.会话层.传输层.网络层.数据链路层及物理层. 这个7层的协议模型虽然规定得 ...

  5. Excel导入遇到的问题An object with the same key already exists in the ObjectStateManager……

    我再导入excel的时候,在本地上是好好的,但是部署之后就不对了. 开始以为是路径可能出错,然后特意跟踪了路径发现没问题, 后面写了很多日志记录发现下面那异常: An object with the ...

  6. HDU-1719 Friend 数学推导

    Friend HDU - 1719 Friend number are defined recursively as follows. (1) numbers 1 and 2 are friend n ...

  7. python yield from (二)

    #pep380 #1. RESULT = yield from EXPR可以简化成下面这样 #一些说明 """ _i:子生成器,同时也是一个迭代器 _y:子生成器生产的值 ...

  8. 阿里云 .NET SDK Roa 和 Rpc 风格签名

    阿里云 .NET SDK Roa 和 Rpc 风格的签名 Demo,适用于自己不想用其提供的SDK,想用自己组装 Roa 和 Rpc 的签名方式. Roa 和 Rpc 的签名方式主要有以下几个不同点: ...

  9. 关于 Paket

    参考地址:https://fsprojects.github.io/Paket/editor-support.html 1. 安装 Paket for Visual Studio,一个类似于 Nuge ...

  10. [Docker] Win10中安装Docker并运行Nginx镜像

    一.安装Docker 进入官网:https://www.docker.com/products/docker-desktop 可能需要先注册登录,很简单的. 点击 Download Desktop f ...