Ranking the cows

Description

Each of Farmer John's N cows (1 ≤ N ≤ 1,000) produces milk at a different positive rate, and FJ would like to order his cows according to these rates from the fastest milk producer to the slowest.

FJ has already compared the milk output rate for M (1 ≤ M ≤ 10,000) pairs of cows. He wants to make a list of C additional pairs of cows such that, if he now compares those C pairs, he will definitely be able to deduce the correct ordering of all N cows. Please help him determine the minimum value of C for which such a list is possible.

Input

Line 1: Two space-separated integers: N and M 
Lines 2..M+1: Two space-separated integers, respectively: X and Y. Both X and Y are in the range 1...N and describe a comparison where cow X was ranked higher than cow Y.

Output

Line 1: A single integer that is the minimum value of C.

Sample Input

5 5
2 1
1 5
2 3
1 4
3 4

Sample Output

3

Hint

From the information in the 5 test results, Farmer John knows that since cow 2 > cow 1 > cow 5 and cow 2 > cow 3 > cow 4, cow 2 has the highest rank. However, he needs to know whether cow 1 > cow 3 to determine the cow with the second highest rank. Also, he will need one more question to determine the ordering between cow 4 and cow 5. After that, he will need to know if cow 5 > cow 3 if cow 1 has higher rank than cow 3. He will have to ask three questions in order to be sure he has the rankings: "Is cow 1 > cow 3? Is cow 4 > cow 5? Is cow 5 > cow 3?"
 
题意:有N头牛,给出m个这N头牛产奶量的大小关系,问至少还需要多少个关系就可以绝对确定这N头牛产奶量的大小顺序;
 
解析:因需要绝对确定产奶量的顺序,可知共需知道C(n,2)个牛与牛之间产奶量的关系
              故只需求出题中所给条件可求出的所有ans个关系,答案即可由C(n,2)-ans求出
              问题来了
              ans如何求出呢?
              由于题中奶牛产奶量的大小关系是可以传递的,显而易见,传递闭包
              于是乎这个题就可以用
              (1)Floyd+传递闭包:(邻接矩阵一定是不行的,O(n^3)绝对TLE,所以这里用的是链式前向星)

#include<cstdio>
#include<cstring>
#include<string>
#include<cmath>
#include<algorithm>
#include<iostream>
using namespace std;
const int maxn=1000+100;
const int maxm=1000000+1000;
int n,m,ans;
int nc[2],head[2][maxn];//head[0]表示a大于b,head[1]表示小于
bool vis[maxn][maxn];
struct jjj{int to,nxt;}line[2][maxm];
inline void add(int a,int b,int c)
{
line[c][++nc[c]].to=b;
line[c][nc[c]].nxt=head[c][a];
head[c][a]=nc[c];
}
int main()
{
memset(head,-1,sizeof(head));
scanf("%d%d",&n,&m);
for(int i=1;i<=m;i++)
{
int a,b;
scanf("%d%d",&a,&b);
if(vis[a][b])continue;
vis[a][b]=1;
add(a,b,0);
add(b,a,1);
ans++;
}
for(int k=1,x,y;k<=n;k++)
{
for(int i=head[1][k];i!=-1;i=line[1][i].nxt)
{
x=line[1][i].to;
for(int j=head[0][k];j!=-1;j=line[0][j].nxt)
{
y=line[0][j].to;
if(vis[x][y]) continue;
vis[x][y]=1;
ans++;
add(x,y,0);
add(y,x,1);
}
}
}
printf("%d",n*(n-1)/2-ans);
return 0;
}

(2)bitset+传递闭包

#include<cstdio>
#include<cstring>
#include<string>
#include<cmath>
#include<algorithm>
#include<iostream>
#include<bitset>
using namespace std;
int n,m,ans;
bitset<1005>a[1005];
int main()
{
scanf("%d%d",&n,&m);
for(int i=1;i<=m;i++)
{
int c,b;
scanf("%d%d",&c,&b);
a[c][b]=1;
}
for(int i=1;i<=n;i++)
for(int j=1;j<=n;j++)
if(a[j][i]) a[j]|=a[i];            //j和i的顺序不能变,不然会WA的很惨!!!
for(int i=1;i<=n;i++)
for(int j=1;j<=n;j++)
if(a[i][j]) ans++;
printf("%d",(n-1)*n/2-ans);
return 0;
}

这里要注意   if(a[j][i]) a[j]|=a[i]; i和j的顺序不能变

如果反了:

for ( int  i = 1 ; i < = n ; i + + )
for ( int  j = 1 ; j < = n ; j + + )
if ( a [ i ] [ j ] )  a [ i ] | = a [ j ] ;(错解)

a [ i ] 中第 位为 表示第i头牛比j高,每一次的按位或 ( | ) 会把a[ j ]中的1全部按位给到a[ i ],即传递给a[ i ]

我们会发现,当i为外循环时,a[ 1 ]只会被a[ j ] 维护一个循环,而此时的a[2]~a[n]还没有被维护,也就是说a[ 2 ]~a[ n ]中储存的数据不完整,即并不是全部比a[ j ]少的牛都储存到了a[ j ]中,之后同理,a[2]~a[n-1]都会有这个问题

所以算出的ans会比正确值小,C(n,2)-ans会比正确值大

 

poj_3275 Ranking the cows的更多相关文章

  1. Bzoj 1703: [Usaco2007 Mar]Ranking the Cows 奶牛排名 传递闭包,bitset

    1703: [Usaco2007 Mar]Ranking the Cows 奶牛排名 Time Limit: 5 Sec  Memory Limit: 64 MBSubmit: 323  Solved ...

  2. POJ-3275:Ranking the Cows(Floyd、bitset)

    Ranking the Cows Time Limit: 2000MS   Memory Limit: 65536K Total Submissions: 3301   Accepted: 1511 ...

  3. poj 3275 "Ranking the Cows"(DFS or Floyd+bitset<>)

    传送门 题意: 农场主 FJ 有 n 头奶牛,现在给你 m 对关系(x,y)表示奶牛x的产奶速率高于奶牛y: FJ 想按照奶牛的产奶速率由高到低排列这些奶牛,但是这 m 对关系可能不能精确确定这 n ...

  4. POJ3275 Ranking the Cows floyd的bitset优化

    POJ3275 Ranking the Cows #include <iostream> #include <cstdio> #include <bitset> u ...

  5. bzoj:1703: [Usaco2007 Mar]Ranking the Cows 奶牛排名

    Description     农夫约翰有N(1≤N≤1000)头奶牛,每一头奶牛都有一个确定的独一无二的正整数产奶率.约翰想要让这些奶牛按产奶率从高到低排序.    约翰已经比较了M(1≤M≤100 ...

  6. POJ3275:Ranking the Cows(Bitset加速floyd求闭包传递)

    Each of Farmer John's N cows (1 ≤ N ≤ 1,000) produces milk at a different positive rate, and FJ woul ...

  7. POJ 3275 Ranking the Cows(传递闭包)【bitset优化Floyd】+【领接表优化Floyd】

    <题目链接> 题目大意:FJ想按照奶牛产奶的能力给她们排序.现在已知有N头奶牛$(1 ≤ N ≤ 1,000)$.FJ通过比较,已经知道了M$1 ≤ M ≤ 10,000$对相对关系.每一 ...

  8. 洛谷P2881 [USACO07MAR]排名的牛Ranking the Cows(bitset Floyd)

    题意 题目链接 Sol 显然如果题目什么都不说的话需要\(\frac{n * (n - 1)}{2}\)个相对关系 然后求一下传递闭包减掉就行了 #include<bits/stdc++.h&g ...

  9. 【dfs】BZOJ1703-[Usaco2007 Mar]Ranking the Cows 奶牛排名

    [题目大意] 农夫约翰有N(1≤N≤1000)头奶牛,每一头奶牛都有一个确定的独一无二的正整数产奶率.约翰想要让这些奶牛按产奶率从高到低排序,约翰已经比较了M(1≤M≤10000)对奶牛的产奶率,但他 ...

随机推荐

  1. 【数组】4Sum

    题目: Given an array S of n integers, are there elements a, b, c, and d in S such that a + b + c + d = ...

  2. redis 安装 与错误解决办法

    redis 安装与安装中遇到的错误 redis 安装 wget http://download.redis.io/releases/redis-4.0.11.tar.gz .tar.gz cd red ...

  3. centos7 Mariadb5.5升级到Mariadb10.2

    一次升级过程,在此记录下. 原因:新的项目需要新的数据库版本支持. 升级主要步骤: 备份原数据库 --->卸载mariadb --->添加mariadb国内yum源 --->安装ma ...

  4. Ethereum 源码分析之框架

    accounts 实现了一个高等级的以太坊账户管理     bmt          二进制的默克尔树的实现     build           主要是编译和构建的一些脚本和配置     cmd  ...

  5. Inno Setup设置在安装Finished页面,点击finish后打开网页

    在安装的最后一个页面FinishPage中点击Finished然后打开一个网页 这个功能貌似很简单,不就是在点击finish按钮给它绑定事件,问题立马解决. 在普通的桌面应用程序开发中的确是这样做的, ...

  6. 使用Quartz.net来执行定时任务

    Quartz.net使用方法:http://www.cnblogs.com/lizichao1991/p/5707604.html 最近,项目中需要执行一个计划任务,组长就让我了解一下Quartz.n ...

  7. jQuery.on() 函数

    1.绑定所有的<p>元素// 为所有P元素分别绑定click事件处理函数handler$("p").on("click", handler); 2. ...

  8. 编写高质量JavaScript代码的68个有效方法

    简介: <Effective JavaScript:编写高质量JavaScript代码的68个有效方法>共分为7章,分别涵盖JavaScript的不同主题.第1章主要讲述最基本的主题,如版 ...

  9. [转]RDL Report in Visual Studio New page per Record

    本文转自:https://social.msdn.microsoft.com/Forums/sqlserver/en-US/f58cd5cf-4296-40f0-b3c8-7e4e15d73762/r ...

  10. 使用C#利用cmd来调用java jar包获取其中的数据

    其实也很简单,就是在C#中构建一个Process,启动jar包,并且给jar包传递参数 因为我并没有怎么学过JAVA,所以只写了个很小的Demo,就是根据传入的参数获取对应的数据 以下是JAVA De ...