1703: [Usaco2007 Mar]Ranking the Cows 奶牛排名
Time Limit: 5 Sec Memory Limit: 64 MB
Description
农夫约翰有N(1≤N≤1000)头奶牛,每一头奶牛都有一个确定的独一无二的正整数产奶率.约翰想要让这些奶牛按产奶率从高到低排序. 约翰已经比较了M(1≤M≤10000)对奶牛的产奶率,但他发现,他还需要再做一张关于另外C对奶牛的产奶率比较,才能推断出所有奶牛的产奶率排序.请帮他确定C的最小值.
Input
第1行包含两个用空格分开的整数N和M.接下来M行,每行有两个用空格分开的整数X和Y(1≤X,y≤1000),表示奶牛X的产奶率高于奶牛Y.
Output
C的最小值.
Sample Input
5 5
2 1
1 5
2 3
1 4
3 4
INPUT DETAILS:
FJ is comparing 5 cows and has already determined that cow 2 > cow
1, cow 1 > cow 5, cow 2 > cow 3, cow 1 > cow 4, and cow 3 > cow 4
(where the ‘>’ notation means “produces milk more quickly”).
Sample Output
3
HINT
从输入样例中可以发现,约翰已经知道的排名有奶牛2>奶牛1>奶牛5和奶牛2>奶牛3>奶牛4,奶牛2排名第一.但是他还需要知道奶牛1的名次是否高于奶牛3来确定排名第2的奶牛,假设奶牛1的名次高于奶牛3.接着,他需要知道奶牛4和奶牛5的名次,假设奶牛5的名次高于奶牛4.在此之后,他还需要知道奶牛5的名次是否高于奶牛3.所以,他至少仍需要知道3个关于奶牛的排名.
Source
Gold
思路:
bitset传递闭包
答案为无序点对的个数,也就是总数减去有序点对的个数。
n^3的floyd要T?
bitset传递闭包优化。
#include <cstdio>
#include <cstring>
#include <algorithm>
#include <bitset>
#define N 1010
using namespace std;
int n, m;
bitset<N> a[N];
int main(){
scanf("%d%d", &n, &m);
for(int i=1; i<=m; i++){
int x, y; scanf("%d%d",&x,&y);
a[x][y] = 1;
}
for(int i=1; i<=n; i++)
for(int j=1; j<=n; j++)
if ( a[j][i] ) a[j] |= a[i];
int ans = 0;
for(int i=1; i<=n; i++)
for(int j=i+1; j<=n; j++)
if (!a[i][j] && !a[j][i]) ans++;
printf("%d\n", ans);
}