bitset优化FLOYD HDU 3275
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<iostream>
#include<cstdio>
#include<queue>
#include<algorithm>
#include<cstring>
#include<string>
#include<bitset>
#include<stack>
using namespace std;
typedef long long LL;
#define MAXN 1009
#define N 100
#define INF 0x3f3f3f3f
/*
传递闭包关系
如果排序好的数字 它们之间已经知道的关系数目肯定是C(n,2)
ans 就是 C(n,2) - 现在已经知道的关系数目
现在已经知道的关系可以DFS 也可以Floyd
*/
bitset<MAXN> g[MAXN];
int n, m;
void Floyd()
{
for (int i = ; i <= n; i++)
{
for (int j = ; j <= n; j++)
if (g[j][i])
g[j] |= g[i];
}
}
int main()
{
scanf("%d%d", &n, &m);
int f, t;
while (m--)
{
scanf("%d%d", &f, &t);
g[f][t] = true;
}
Floyd();
int ans = ;
for (int i = ; i <= n; i++)
{
for (int j = i+; j <= n; j++)
{
if (!g[i][j]&&!g[j][i]) ans++;
}
}
printf("%d\n", ans );
}
bitset优化FLOYD HDU 3275的更多相关文章
- POJ 3275 Ranking the Cows(传递闭包)【bitset优化Floyd】+【领接表优化Floyd】
<题目链接> 题目大意:FJ想按照奶牛产奶的能力给她们排序.现在已知有N头奶牛$(1 ≤ N ≤ 1,000)$.FJ通过比较,已经知道了M$1 ≤ M ≤ 10,000$对相对关系.每一 ...
- hdu 5036 Explosion bitset优化floyd
http://acm.hdu.edu.cn/showproblem.php?pid=5036 题意就是给定一副有向图,现在需要走遍这n个顶点,一开始出发的顶点是这n个之中的随便一个. 如果走了1,那么 ...
- POJ 3275 Ranking the cows ( Floyd求解传递闭包 && Bitset优化 )
题意 : 给出 N 头牛,以及 M 个某些牛之间的大小关系,问你最少还要确定多少对牛的关系才能将所有的牛按照一定顺序排序起来 分析 : 这些给出的关系想一下就知道是满足传递性的 例如 A > B ...
- hdu 5745 La Vie en rose DP + bitset优化
http://acm.hdu.edu.cn/showproblem.php?pid=5745 这题好劲爆啊.dp容易想,但是要bitset优化,就想不到了. 先放一个tle的dp.复杂度O(n * m ...
- HDU 5808 Price List Strike Back bitset优化的背包。。水过去了
http://acm.hdu.edu.cn/showproblem.php?pid=5808 用bitset<120>dp,表示dp[0] = true,表示0出现过,dp[100] = ...
- BZOJ2208 [Jsoi2010]连通数[缩点/Floyd传递闭包+bitset优化]
显然并不能直接dfs,因为$m$会非常大,复杂度就是$O(mn)$: 这题有三种做法,都用到了bitset的优化.第二种算是一个意外的收获,之前没想到竟然还有这种神仙操作.. 方法一:缩点+DAG上b ...
- hdu 5506 GT and set dfs+bitset优化
GT and set Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/65536 K (Java/Others) Probl ...
- HDU - 6268: Master of Subgraph (分治+bitset优化背包)
题意:T组样例,给次给出一个N节点的点权树,以及M,问连通块的点权和sum的情况,输出sum=1到M,用0或者1表示. 思路:背包,N^2,由于是无向的连通块,所以可以用分治优化到NlgN. 然后背包 ...
- hdu_5036_Explosion(bitset优化传递闭包)
题目链接:hdu_5036_Explosion 题意: 一个人要打开或者用炸弹砸开所有的门,每个门里面有一些钥匙,一个钥匙对应一个门,有了一个门的钥匙就能打开相应的门,告诉每个门里面有哪些门的钥匙,问 ...
随机推荐
- spring cloud config搭建说明例子(四)-补充配置文件
服务端 ConfigServer pom.xml <dependency> <groupId>org.springframework.cloud</groupId> ...
- 洛谷P5398 [Ynoi2018]GOSICK(二次离线莫队)
题面 传送门 题解 维包一生推 首先请确保您会二次离线莫队 那么我们现在的问题就是怎么转移了,对于\(i\)和前缀\([1,r]\)的贡献,我们拆成\(b_i\)和\(c_i\)两部分,其中\(b_i ...
- 基于itchat实现微信群消息同步机器人
原始网址:http://www.jianshu.com/p/7aeadca0c9bd# 最近 全栈数据工程师养成攻略 的微信群已经将近500人,开了二群之后为了打通不同微信群之间的消息,花了点时间做了 ...
- [BZOJ:3162]:独钓寒江雪
题解: 求本质不同的独立集的个数 首先独立集的个数是很好做的 \(f[u][0/1]\)表示节点\(u\)不选/选的方案数 然后dp就是 \(f[u][0] = f[u][0] * (f[v][0] ...
- Visual C++ Windows 桌面应用程序样例(摘抄)
//================================== //Windows应用程序框架结构(例子) //参考:<Visual C++宝典>陈国建等编著 //======= ...
- iOS popViewControllerAnimated后刷新原先的表格
当主页面列表push子页面,子页面修改后pop回主页面后应该刷新主页面列表数据,不修改子页面信息就不刷新主页面列表,这里介绍个取巧的方法:利用[NSNotificationCenter default ...
- ubuntu下删除和新建用户(并有su权限)
http://blog.csdn.net/speedme/article/details/8206144ubuntu下删除和新建用户(并有su权限) 如何创建ubuntu新用户?输入:sudo add ...
- html5前端杂记
首先是css的一些知识 毕竟自己懂得不多,但是一看资料.感觉似曾相识 <style> .red-text { color: red; } </style>//这里是css样式的 ...
- Android Studio 1.5启动出现“SDK Manager: failed to install”问题的解决
问题描述 Android Studio 1.5是当前最新Android手机应用开发平台,下载bundle版安装后,启动Studio后出现“SDK Manager: failed to install” ...
- CAD设置系统变量(com接口VB语言)
主要用到函数说明: MxDrawXCustomFunction::Mx_SetSysVar 设置系统变量.详细说明如下: 参数 说明 CString sVarName 系统变量名 Value 需要设置 ...