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

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 5
2 1
1 5
2 3
1 4
3 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?"

#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 );
}
时间: 2024-10-14 15:06:05

bitset优化FLOYD HDU 3275的相关文章

hdu 5036 Explosion bitset优化floyd

http://acm.hdu.edu.cn/showproblem.php?pid=5036 题意就是给定一副有向图,现在需要走遍这n个顶点,一开始出发的顶点是这n个之中的随便一个. 如果走了1,那么1能联通的顶点就可以直接走过去,其他不和1连通的,就需要炸坏.问需要炸弹的期望. 比如一副图是1-->2-->3的.那么期望是11 / 6 假如从1号点开始,1/3概率选中1号点开始,那么需要炸弹数是1(炸开一号),贡献是1/3 假如从2号点开始,1/3概率选中2号点开始,那么需要炸开2号点,然后

hdu 5036 Explosion (bitset优化的传递闭包求解概率)

Explosion Time Limit: 6000/3000 MS (Java/Others)    Memory Limit: 262144/262144 K (Java/Others) Total Submission(s): 142    Accepted Submission(s): 25 Problem Description Everyone knows Matt enjoys playing games very much. Now, he is playing such a g

HDU - 6268: Master of Subgraph (分治+bitset优化背包)

题意:T组样例,给次给出一个N节点的点权树,以及M,问连通块的点权和sum的情况,输出sum=1到M,用0或者1表示. 思路:背包,N^2,由于是无向的连通块,所以可以用分治优化到NlgN. 然后背包可以用bitset优化.注意不要想着背包合并背包,背包只能合并单点. #include<bits/stdc++.h> #define pb push_back #define rep(i,a,b) for(int i=a;i<=b;i++) #define Gv G[u][i] #defin

Codeforces 788C The Great Mixing(背包问题建模+bitset优化或BFS)

[题目链接] http://codeforces.com/problemset/problem/788/C [题目大意] 给出一些浓度的饮料,要求调出n/1000浓度的饮料,问最少需要多少升饮料 [题解] 设浓度为a,现在要求出系数x1,x2,x3……,使得x1*a1+x2*a2+x3*a3+……=n*(x1+x2+x3+……) 得a1*(x1-n)+a2*(x2-n)+a3*(x3-n)+……=0 假设现在有x1-n和x2-n,设其数值为x和y,那么一定有(x)*y+(-y)*x=0, x+y

XJOI NOIP2015模拟赛Day1 T2 ctps bitset优化 或 排序+cdq分治+树状数组+平衡树

题意: 4维空间中有1个点集A,|A|=n,用(a,b,c,d)表示每个点. 共有m个询问,每次询问输入一个点(a,b,c,d),求最大的S,其中S={p|p∈A且ap<=a,bp<=b,cp<=c,dp<=d},输出|S| 输入格式: 第一行n 接下来n行有n个4维点对 第n+2行有一个数m 再接下来m行每行有一个四维点对,表示每个询问 输出格式: 对于每个询问输出一个数 **方法:**bitset优化 或 排序+cdq分治+树状数组+平衡树 解析: 神题,考场不会,暴力骗40,

hdu5313Bipartite Graph(二分图染色+DP(bitset优化))

题意:给n个点m条边,问最多可以添加几条边使图为完全二分图 分析:如果二分图没有限制,看到是两边分别为n/2个点和n-n/2个点的最优但是可   能出现大于此点的情况,比如n=4,m=3,边为1 2,1 3,1 4.此时完全二分图边最多为3,所以要求得二分图左边或者右边可达到的离n/2最近的点数是多少为最优解,于是采用染色分别求出各个联通快的2种颜色的各个点数,然后用推出能达到的点数,这里dp[i]的下一个状态是dp[i+1]+d[i+1][0]和dp[i+1][1](dp[i]为前i个二分图的

HDU5890:Eighty seven(Bitset优化背包)

Mr. Fib is a mathematics teacher of a primary school. In the next lesson, he is planning to teach children how to add numbers up. Before the class, he will prepare NN cards with numbers. The number on the ii-th card is aiai. In class, each turn he wi

01二维背包+bitset优化——hdu5890

口胡一种别的解法: 三重退背包,g1[j]k]表示不选x的选了j件物品,体积为k的方案数,g[0][0] = 1 , g1[j][k]=dp[j][k]-g1[j-1][k-a[x]] 然后按这样再退三层,最后看g3[10][87]的方案数是否非0即可,这样复杂度是O(50*50*50*10*87) 如果直接枚举删掉的数,然后用可行性二维01背包做 复杂度是O(50*50*50)*O(n*10*87) 再加上bitset优化第二维 复杂度/32 ,由于n比较小,所以也差不多 /* 在n个数里找到

hdu 4920 Matrix multiplication bitset优化常数

Matrix multiplication Time Limit: 4000/2000 MS (Java/Others)    Memory Limit: 131072/131072 K (Java/Others) Problem Description Given two matrices A and B of size n×n, find the product of them. bobo hates big integers. So you are only asked to find t