博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
poj_3275 Ranking the cows
阅读量:4969 次
发布时间:2019-06-12

本文共 3544 字,大约阅读时间需要 11 分钟。

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 52 11 52 31 43 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会比正确值大

 

转载于:https://www.cnblogs.com/zi-qilin/p/9454960.html

你可能感兴趣的文章
Sd - JavaBase问题
查看>>
利用CSS让dl dt dd呈现多行多列效果
查看>>
【译著】第9章 SportsStore:管理 — 《精通ASP.NET MVC 3框架》
查看>>
判断文件类型
查看>>
python-GIL、死锁递归锁及线程补充
查看>>
.Net服务组件(ServicedComponent)简介及其使用
查看>>
Binding介绍
查看>>
JQuery实现可编辑的表格
查看>>
bzoj4903 [Ctsc2017]吉夫特
查看>>
java线程学习之wait方法
查看>>
ORM之Dapper操作Sql Server和MySql数据库
查看>>
2012/8/3SVN小入门
查看>>
HDU - 6370 Werewolf 2018 Multi-University Training Contest 6 (DFS找环)
查看>>
js随机数 从头开始系列
查看>>
19.网络策略控制
查看>>
《Programing in Lua》第一部分:CH1-CH10
查看>>
用 async/await 来处理异步
查看>>
手机网页制作需要注意的一点东西
查看>>
Winfon 页签切换及窗体控件自适应
查看>>
Open the Local Activity through the Browser
查看>>