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 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 cowY.
Output
Sample Input
5 5
2 1
1 5
2 3
1 4
3 4
Sample Output
3
Hint
题意:有N头奶牛,现在给出M对产奶量关系U>V,问至少还需要知道多少奶牛可以做到全部奶牛产奶关系。
思路:有向图,问至少再加多少边,使得任意两点S、T的可以到达(S到达T或者到达S)。闭包传递后不能到达的需要加边,ans++。
至于为什么闭包传递后不连通就就要ans++呢,假设已知了1>2>3>4,5>6>7,我们知道了4>5不就行了吗,ans=1啊。
但是注意题目说的是"确定",而比较了4和5之后可能会4<5,即任然不可以把知道全部顺序。
所以剩下对于C(n,2)对关系里不确定的关系,都有知道才能“确定”所有的大小关系。
#include<cstdio>
#include<bitset>
#include<cstdlib>
#include<cstring>
#include<iostream>
#include<algorithm>
using namespace std;
const int maxn=;
bitset<maxn>mp[maxn];
int main()
{
int N,M,ans,i,j,k;
while(~scanf("%d%d",&N,&M)){
ans=;memset(mp,,sizeof(mp));
for(i=;i<=M;i++){
scanf("%d%d",&j,&k);
mp[j].set(k);
} for(k=;k<=N;k++)
for(i=;i<=N;i++)
if(mp[i][k])
mp[i]|=mp[k];
for(i=;i<=N;i++)
for(j=i+;j<=N;j++)
if(!mp[i][j]&&!mp[j][i])
ans++;
printf("%d\n",ans);
}
return ;
}
POJ3275:Ranking the Cows(Bitset加速floyd求闭包传递)的更多相关文章
- POJ3275 Ranking the Cows floyd的bitset优化
POJ3275 Ranking the Cows #include <iostream> #include <cstdio> #include <bitset> u ...
- World Finals 1996 Uva 247 (Floyd求闭包)
思路:用Floyd求传递闭包. 附:逗号后的空格没看到,WA了好多次…….还有就是强连通分量也可以做,但是对这个题来说太麻烦,而且不方便输出,. 代码如下: #include<iostream& ...
- 洛谷P2881 [USACO07MAR]排名的牛Ranking the Cows(bitset Floyd)
题意 题目链接 Sol 显然如果题目什么都不说的话需要\(\frac{n * (n - 1)}{2}\)个相对关系 然后求一下传递闭包减掉就行了 #include<bits/stdc++.h&g ...
- 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 ...
- POJ 3275 Ranking the cows ( Floyd求解传递闭包 && Bitset优化 )
题意 : 给出 N 头牛,以及 M 个某些牛之间的大小关系,问你最少还要确定多少对牛的关系才能将所有的牛按照一定顺序排序起来 分析 : 这些给出的关系想一下就知道是满足传递性的 例如 A > B ...
- POJ 3275 Ranking the Cows(传递闭包)【bitset优化Floyd】+【领接表优化Floyd】
<题目链接> 题目大意:FJ想按照奶牛产奶的能力给她们排序.现在已知有N头奶牛$(1 ≤ N ≤ 1,000)$.FJ通过比较,已经知道了M$1 ≤ M ≤ 10,000$对相对关系.每一 ...
- 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
Ranking the cows Description Each of Farmer John's N cows (1 ≤ N ≤ 1,000) produces milk at a differe ...
随机推荐
- SQLServer中计算周
--本周最大值与最小值.平均值 DECLARE @WeekMax float,@WeekMin float,@WeekAvg float,@AddDate varchar(20) DECLARE @W ...
- PageCache 在查询中的作用很大
百度Elasticsearch-产品描述-介绍-百度云 https://cloud.baidu.com/doc/BES/FAQ.html#.51.46.57.73.73.7E.71.4C.6F.AA. ...
- unset($_COOKIE['wcookie_date'])
foreach ($_COOKIE AS $wk => $wv) { unset($_COOKIE[$wk]); } die(); w 删除cookie
- centos7安装tomcat7
1.去官网下载指定的安装包http://tomcat.apache.org/download-70.cgi 2.把下载下来的安装包放在/opt下 3.使用命令解压下载的文件tar -zxvf apac ...
- JSP--JSP语法--指令--include(动态包含/静态包含)--九大隐式对象--四大域对象--JSP内置标签--JavaBean的动作元素--MVC三层架构
一.JSP 原理:JSP其实就是一个servlet. Servlet负责业务逻辑处理,JSP只负责显示.开发中,JSP中不能有一行JAVA代码 二.JSP语法 1. JSP模板元素:JSP中HT ...
- Pytorch(一)
一.Pytorch介绍 Pytorch 是Torch在Python上的衍生物 和Tensorflow相比: Pytorch建立的神经网络是动态的,而Tensorflow建立的神经网络是静态的 Tens ...
- Linux学习笔记—Linux磁盘与文件系统管理(转载)
认识EXT2文件系统 文件的系统特性 Linux的正规文件系统为Ext2 文件数据除了文件实际内容外,还包括其他属性(文件权限.文件属性). 文件系统将这两部分数据分别存放在不同的块,权限和属性放在i ...
- Ionic 3 项目的工程目录结构(转载)
工程目录结构说明如下图
- Django基础一
Django基础一 Web框架本质 框架,即framework,特指为解决一个开放性问题而设计的具有一定约束性的支撑结构,使用框架可以帮你快速开发特定的系统,简单地说,就是你用别人搭建好的舞台来做表演 ...
- 用pytesseract识别验证码报错
运行py文件出现下面报错 pytesseract.pytesseract.TesseractError: (1, 'Error opening data file \\Program Files\\T ...