poj_3275 Ranking the cows
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
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
Sample Input
5 5
2 1
1 5
2 3
1 4
3 4
Sample Output
3
Hint
#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 ] 中第 j 位为 1 表示第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的更多相关文章
- Bzoj 1703: [Usaco2007 Mar]Ranking the Cows 奶牛排名 传递闭包,bitset
1703: [Usaco2007 Mar]Ranking the Cows 奶牛排名 Time Limit: 5 Sec Memory Limit: 64 MBSubmit: 323 Solved ...
- POJ-3275:Ranking the Cows(Floyd、bitset)
Ranking the Cows Time Limit: 2000MS Memory Limit: 65536K Total Submissions: 3301 Accepted: 1511 ...
- poj 3275 "Ranking the Cows"(DFS or Floyd+bitset<>)
传送门 题意: 农场主 FJ 有 n 头奶牛,现在给你 m 对关系(x,y)表示奶牛x的产奶速率高于奶牛y: FJ 想按照奶牛的产奶速率由高到低排列这些奶牛,但是这 m 对关系可能不能精确确定这 n ...
- POJ3275 Ranking the Cows floyd的bitset优化
POJ3275 Ranking the Cows #include <iostream> #include <cstdio> #include <bitset> u ...
- bzoj:1703: [Usaco2007 Mar]Ranking the Cows 奶牛排名
Description 农夫约翰有N(1≤N≤1000)头奶牛,每一头奶牛都有一个确定的独一无二的正整数产奶率.约翰想要让这些奶牛按产奶率从高到低排序. 约翰已经比较了M(1≤M≤100 ...
- 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 ...
- POJ 3275 Ranking the Cows(传递闭包)【bitset优化Floyd】+【领接表优化Floyd】
<题目链接> 题目大意:FJ想按照奶牛产奶的能力给她们排序.现在已知有N头奶牛$(1 ≤ N ≤ 1,000)$.FJ通过比较,已经知道了M$1 ≤ M ≤ 10,000$对相对关系.每一 ...
- 洛谷P2881 [USACO07MAR]排名的牛Ranking the Cows(bitset Floyd)
题意 题目链接 Sol 显然如果题目什么都不说的话需要\(\frac{n * (n - 1)}{2}\)个相对关系 然后求一下传递闭包减掉就行了 #include<bits/stdc++.h&g ...
- 【dfs】BZOJ1703-[Usaco2007 Mar]Ranking the Cows 奶牛排名
[题目大意] 农夫约翰有N(1≤N≤1000)头奶牛,每一头奶牛都有一个确定的独一无二的正整数产奶率.约翰想要让这些奶牛按产奶率从高到低排序,约翰已经比较了M(1≤M≤10000)对奶牛的产奶率,但他 ...
随机推荐
- Cocos2d-x游戏导出android工程,提取cocos的so文件
Cocos2d-x游戏导出android工程,提取cocos的so文件 原本cocos游戏的android工程编译时,需要将cocos的库文件进行编译,这些文件大部分是cpp文件, 使用ndk ...
- windows下mysql配置,my.ini配置文件
基本配置,这个配置可以直接复制到mysql根目录下了my.ini文件中, [client] port=3306 [mysql] no-beep # default-character-set= [my ...
- Spanner:谷歌新一代全球部署的列式数据库
Spanner 是一个可扩展的.全球分布式的数据库,提供分布式ACID. 架构 universe:一个部署的实例成为universe,目前谷歌有3个,分别为开发/测试/线上 Zone:一个数据中心,相 ...
- 反应器(Reactor)模式
Java NIO非堵塞技术实际是采取反应器模式,或者说是观察者(observer)模式为我们监察I/O端口,如果有内容进来,会自动通知我们,这样,我们就不必开启多个线程死等,从外界看,实现了流畅的I/ ...
- DataGridView删除多行选中数据
思路是找到最先选择和最后选择到的行 ,弄一个for循环,根据这些行的索引值在执行数据的删除. 我这里用了EF. DialogResult result = MessageBox ...
- Eclipse发布的Dynamical web项目在Tomacat文件夹下显示
Eclipse设置了Tomacat后,项目信息会在你的workspace上,在Tomacat文件夹上是没有的.但是通过设置是可以在Tomacat文件夹上存在的. 配置好服务器后,先关闭服务器,然后在E ...
- IDEA 中将已有项目放到 GitHub 上去
前言:公司用的都是 idea+svn 来管理代码,所以家里尝试一下 idea+github 来管理.我的本地仓库是配置过ssh key,所以此处没有再次配置ssh key,如需配置可以参考 https ...
- UVAlive6807 Túnel de Rata (最小生成树)
题意 题目链接 Sol 神仙题Orz 我们考虑选的边的补集,可以很惊奇的发现,这个补集中的边恰好是原图中的一颗生成树: 并且答案就是所有边权的和减去这个边集中的边的权值: 于是我们只需要求最大生成树就 ...
- CefSharp F12打开DevTools查看console
winform嵌入chrome浏览器,修改项目属性 生成 平台为x86 1.nuget安装cefsharp 2.实例化浏览器 private void Form1_Load(object sender ...
- jquery3.0移除了.load()方法
参考链接:新版jquery去掉load事件了吗? 今天也遇到了这个问题,查了一下文档,确实从3.0开始移除了load.unload.error事件方法.不过依然可以用on方法绑定这些事件. Break ...