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

第一想法用拓扑排序玩传递闭包,毕竟是稀疏图

 1 #include<cstdio>
 2 #include<cstdlib>
 3 #include<stack>
 4 #include<bitset>
 5 #include<vector>
 6 using namespace std;
 7 const int N=1024;
 8 stack<int> S;
 9 bitset<N> a[N];
10 vector<int> G[N];
11 int n,m;
12 int d[N];
13 int read(){
14     char c;int x=0,f=1;
15     for(c=getchar();!isdigit(c);c=getchar())if(c==‘-‘)f=-1;
16     for(;isdigit(c);c=getchar())x=(x<<3)+(x<<1)+(c^48);
17     return x*f;
18 }
19 void TopSort(){
20     for(int i=0;i<n;++i)
21         if(!d[i])S.push(i);
22     while(!S.empty()){
23         int u=S.top();S.pop();
24         for(int i=0,i_end=G[u].size();i<i_end;++i){
25             int v=G[u][i];
26             a[v]|=a[u];
27             --d[v];
28             if(!d[v])S.push(v);
29         }
30     }
31 }
32 int main(){
33     scanf("%d%d",&n,&m);
34     while(m--){
35         int u=read()-1,v=read()-1;
36         G[u].push_back(v);
37         ++d[v];
38     }
39     for(int i=0;i<n;++i)a[i][i]=1;
40     TopSort();
41     for(int i=0;i<n;++i)
42         m+=a[i].count();
43     printf("%d\n",n*(n-1)/2-m-1+n);
44     return 0;
45 }

就当是对ZJOI2017Day2的交代吧

时间: 2024-10-07 14:09:21

POJ3275 [USACO07MAR]Ranking the Cows的相关文章

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 奶牛排名 传递闭包,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

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

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>

USACO 2007 “March Gold” Ranking the Cows

题目链接:https://www.luogu.org/problemnew/show/P2881 题目链接:https://vjudge.net/problem/POJ-3275 题目大意 给定标号为 1~N 这 N 个数,在给定 M 组大小关系,求还需要知道多少组大小关系才可以给这组数排序? 分析1(Floyd + bitset) 总共需要知道 n * (n - 1) / 2 条边,因此只要求一下现在已经有了多少条边,再减一下即可.由于大小关系有传递性,因此计数之前需要求传递闭包. 直接上 f

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-