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)对奶牛的产奶率,但他 ...
随机推荐
- php的一个验证邮箱的正则表达式
/([a-z0-9]*[-_\.]*[a-z0-9]+)*[-_\.]*@([a-z0-9]*[-_]?[a-z0-9]+)+[\.]([a-z0-9]{2,3}|[a-z0-9]*[-_]?[a-z ...
- 面试题42:计算逆波兰表达式(RPN)
这是一个比较简单的题目,借助栈可以轻松实现逆波兰表达式. 题目描述: Evaluate the value of an arithmetic expression in Reverse Polish ...
- 剑指offer65:矩阵中的路径
题目描述: 请设计一个函数,用来判断在一个矩阵中是否存在一条包含某字符串所有字符的路径.路径可以从矩阵中的任意一个格子开始,每一步可以在矩阵中向左,向右,向上,向下移动一个格子.如果一条路径经过了矩阵 ...
- bzoj 5341: [Ctsc2018]暴力写挂
Description Solution 边分治+边分树合并 这个题很多做法都是启发式合并的复杂度的,都有点卡 以前有个套路叫做线段树合并优化启发式合并,消掉一个 \(log\) 这个题思路类似,建出 ...
- Java Bad version
Eclipse的三个地方需要重新设置: 在工程上点右键,选属性,三个地方: Java Build Path Java Compiler Project Facets:这个地方还可以设置tomcat的r ...
- HTML的map-area的使用
使用背景 在把设置图转成页面的时候,时常会遇到这种情况:一张小图片上有好多个可以点击的小图标,按常规的处理方法是把这一个一个的小图切出来,然后每个加个a标签进行跳转,但是这样会非常的浪费时间,而且会增 ...
- Messenger与AIDL的异同
Messenger与AIDL的异同 最近做项目需要使用进程间通信,大家知道应用层的进程间通信无非Broadcast,Activity,Service,Content Provider四大组件.Broa ...
- 为什么要用 C# 来作为您的首选编程语言
因为您可以用,并且也是您的最佳选择!之所以可用,是因为 C# 能够很好地在 Mac.Linux.Android 和 iOS 上运行(对了,还有 Windows):它可以在您最喜爱的编辑器上运行:它在一 ...
- 解决 ImportError: cannot import name pywrap_tensorflow
原文:https://aichamp.wordpress.com/2016/11/13/handeling-importerror-cannot-import-name-pywrap_tensorfl ...
- php返回数组后处理(开户成功后弹窗提示)
1. 在注册的时候,注册成功后经常会弹窗提示自己注册的信息,这类做法需要返回mysql数据库中获取的数组值,返回给前台页面,赋值给弹窗. 2.做法: 返回数组 打印的数组的值 返回数组处理 赋值给弹窗 ...