P2881 [USACO07MAR]排名的牛Ranking the Cows

bitset优化传递闭包模板题

这种关系直接用图论来建模就是了,其实就是一个传递闭包。

传递闭包有一个朴素的做法就是floyd。

而这道题的范围是\(n \leq 1000\),\(n^3\)的暴力显然会T。

而使用bitset,听说可以优化到原做法的\(\frac{1}{32}\)甚至更好!

直接给代码其实是自己不懂原理

#include<cstdio>
#include<bitset>
const int maxn = 1005;

std::bitset<maxn> b[maxn];
int n, m;
int main()
{
    scanf("%d%d", &n, &m);
    for(int i = 1; i <= n; i++) b[i][i] = true;
    while(m--)
    {
        int u, v; scanf("%d%d", &u, &v);
        b[u][v] = true;
    }
    for(int k = 1; k <= n; k++)
    {
        for(int i = 1; i <= n; i++)
        {
            if(b[i][k])
            {
                b[i] |= b[k];
            }
        }
    }
    int ans = 0;
    for(int i = 1; i <= n; i++) ans += b[i].count();
    ans -= n;
    ans = n * (n - 1) / 2 - ans;
    printf("%d\n", ans);
    return 0;
}

原文地址:https://www.cnblogs.com/Garen-Wang/p/9794984.html

时间: 2024-10-13 12:34:14

P2881 [USACO07MAR]排名的牛Ranking the Cows的相关文章

bzoj1703[Usaco2007 Mar]Ranking the Cows 奶牛排名*

bzoj1703[Usaco2007 Mar]Ranking the Cows 奶牛排名 题意: n头奶牛,知道n对奶牛之间的产奶量大小,问想知道所有奶牛产奶量大小顺序至少还需知道几对.n≤1000. 题解: 每个大小关系看为一条有向边,对每头奶牛进行dfs,求每头奶牛可以到的奶牛数和可以到它的奶牛数之和,用n-1减后就是需要和它比较的奶牛数.最后输出(n*(n-1)-所有牛的结果相加)/2即可. 代码: 1 #include <cstdio> 2 #include <cstring&g

Bzoj 1703: [Usaco2007 Mar]Ranking the Cows 奶牛排名 传递闭包,bitset

1703: [Usaco2007 Mar]Ranking the Cows 奶牛排名 Time Limit: 5 Sec  Memory Limit: 64 MBSubmit: 323  Solved: 238[Submit][Status][Discuss] Description 农夫约翰有N(1≤N≤1000)头奶牛,每一头奶牛都有一个确定的独一无二的正整数产奶率.约翰想要让这些奶牛按产奶率从高到低排序.    约翰已经比较了M(1≤M≤10000)对奶牛的产奶率,但他发现,他还需要再做一

POJ-3275:Ranking the Cows(Floyd、bitset)

Ranking the Cows Time Limit: 2000MS   Memory Limit: 65536K Total Submissions: 3301   Accepted: 1511 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

POJ3275 Ranking the Cows floyd的bitset优化

POJ3275 Ranking the Cows 1 #include <iostream> 2 #include <cstdio> 3 #include <bitset> 4 using namespace std; 5 const int maxn = 1005; 6 int n, m; 7 bitset<maxn> maps[maxn]; 8 void floyd() { 9 for (int k = 1; k <= n; k++) { 10 f

【BZOJ】1703: [Usaco2007 Mar]Ranking the Cows 奶牛排名

[题意]给定n头牛和m对大小关系,求最坏情况下至少还需要比较几对奶牛的大小(在未确定顺序的奶牛对中随机比较) [算法]floyd求传递闭包 [题解]可达说明大小已知,则不可达点对数量就是最少比较次数. 使用bitset优化传递闭包,复杂度O(n^3 /32). #include<cstdio> #include<bitset> #include<algorithm> using namespace std; const int maxn=1010; int n,m; b

POJ3275 [USACO07MAR]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 r

BZOJ 1703 [Usaco2007 Mar]Ranking the Cows 奶牛排名 bitset优化

题意:链接 方法: bitset传递闭包 解析: 显然答案为无序点对的个数. 但是无序点对的个数怎么求呢? 容斥原理. 所有点对个数减去有序点对的个数即为答案. 怎么维护有序点对个数呢? bitset传递闭包即可. 人生中的第一次rnk1!!!!!!!!!!! 代码: #include <bitset> #include <cstdio> #include <cstring> #include <iostream> #include <algorith

【dfs】BZOJ1703-[Usaco2007 Mar]Ranking the Cows 奶牛排名

[题目大意] 农夫约翰有N(1≤N≤1000)头奶牛,每一头奶牛都有一个确定的独一无二的正整数产奶率.约翰想要让这些奶牛按产奶率从高到低排序,约翰已经比较了M(1≤M≤10000)对奶牛的产奶率,但他发现,他还需要再做一张关于另外C对奶牛的产奶率比较,才能推断出所有奶牛的产奶率排序.请帮他确定C的最小值. [思路] 对于M对关系,从产奶率高的往产奶率低的连一条有向边.对于每个节点i,它能抵达的节点的总数即是能比较得出的比它小的奶牛总数. 由于原本总共有N*(N-1)/2对关系,ans=N*(N-

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