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

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 cowY.

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?"

题意:有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=1010;
bitset<maxn>mp[maxn];
int main()
{
    int N,M,ans,i,j,k;
    while(~scanf("%d%d",&N,&M)){
        ans=0;memset(mp,0,sizeof(mp));
        for(i=1;i<=M;i++){
            scanf("%d%d",&j,&k);
            mp[j].set(k);
        }

        for(k=1;k<=N;k++)
         for(i=1;i<=N;i++)
          if(mp[i][k])
           mp[i]|=mp[k];
        for(i=1;i<=N;i++)
         for(j=i+1;j<=N;j++)
          if(!mp[i][j]&&!mp[j][i])
           ans++;
        printf("%d\n",ans);
    }
    return 0;
}

原文地址:https://www.cnblogs.com/hua-dong/p/8538980.html

时间: 2024-10-11 21:34:42

POJ3275:Ranking the Cows(Bitset加速floyd求闭包传递)的相关文章

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

World Finals 1996 Uva (Floyd求闭包)

思路:用Floyd求传递闭包. 附:逗号后的空格没看到,WA了好多次…….还有就是强连通分量也可以做,但是对这个题来说太麻烦,而且不方便输出,. 代码如下: #include<iostream> #include<cstdio> #include<cstring> #include<string> #include<map> using namespace std; int n,m; map<string,int> ma; map&l

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

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)对奶牛的产奶率,但他发现,他还需要再做一

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 奶牛排名

[题意]给定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

HDU - 1599 find the mincost route(Floyd求最小环)

find the mincost route Time Limit: 2000MS Memory Limit: 32768KB 64bit IO Format: %I64d & %I64u Submit Status Description 杭州有N个景区,景区之间有一些双向的路来连接,现在8600想找一条旅游路线,这个路线从A点出发并且最后回到A点,假设经过的路线为V1,V2,....VK,V1,那么必须满足K>2,就是说至除了出发点以外至少要经过2个其他不同的景区,而且不能重复经过同一个

【BZOJ 1027】 (凸包+floyd求最小环)

[题意] 某公司加工一种由铁.铝.锡组成的合金.他们的工作很简单.首先进口一些铁铝锡合金原材料,不同种类的原材料中铁铝锡的比重不同.然后,将每种原材料取出一定量,经过融解.混合,得到新的合金.新的合金的铁铝锡比重为用户所需要的比重. 现在,用户给出了n种他们需要的合金,以及每种合金中铁铝锡的比重.公司希望能够订购最少种类的原材料,并且使用这些原材料可以加工出用户需要的所有种类的合金. [分析] 只要考虑前两个物质的比例,因为加起来等于1. 如果你看过zoj3154,就会知道这题的模型,用二元组表

多源最短路径Floyd、Floyd求最小环【模板】

Floyd算法:用来找出每对点之间的最短距离.图可以是无向图,也可以是有向图,边权可为正,也可以为负,唯一要求是不能有负环. 1.初始化:将Map[][]中的数据复制到Dist[][]中作为每对顶点之间的最短路径的初值,Pre[i][j] = i 表示 i 到 j 路径中 j 的前一节点. 2. k 从 1 到 N 循环 N 次,每次循环中,枚举图中不同的两点 i,j,如果Dist[i][j] > Dist[i][k] + Dist[k][j],则更新Dist[i][j] = Dist[i][k