bitset优化FLOYD HDU 3275
Posted joeylee97
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了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 = 1; i <= n; i++) { for (int j = 1; 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 = 0; for (int i = 1; i <= n; i++) { for (int j = i+1; j <= n; j++) { if (!g[i][j]&&!g[j][i]) ans++; } } printf("%d\n", ans ); }
以上是关于bitset优化FLOYD HDU 3275的主要内容,如果未能解决你的问题,请参考以下文章
POJ3275:Ranking the Cows(Bitset加速floyd求闭包传递)
hdu 5036 Explosion bitset优化floyd
[bzoj2208][Jsoi2010]连通数_bitset_传递闭包floyd
Codeforces 576D. Flights for Regular Customers(倍增+floyd+bitset优化)