BZOJ 1703. [Usaco2007 Mar] Ranking the Cows Cows ranking

Portal

Determine the ranking at a glance think $ floyd $ transitive closure, and because it is relatively large $ n $ to $ bitset $ optimization

It requires a minimum number of times? Then think of half, then the autistic

Online practices are the result of a default comparison is the optimal solution for you? Whatever. It should be a title wrong ...

#include<iostream>
#include<cstdio>
#include<algorithm>
#include<cstring>
#include<cmath>
#include<bitset>
using namespace std;
typedef long long ll;
inline int read()
{
    int x=0,f=1; char ch=getchar();
    while(ch<'0'||ch>'9') { if(ch=='-') f=-1; ch=getchar(); }
    while(ch>='0'&&ch<='9') { x=(x<<1)+(x<<3)+(ch^48); ch=getchar(); }
    return x*f;
}
const int N=1007;
int n,m,ans;
bitset <N> mp[N];
int main()
{
    n=read(),m=read(); int a,b;
    for(int i=1;i<=m;i++)
        a=read(),b=read(),mp[a][b]=1;
    for(int j=1;j<=n;j++)
        for(int i=1;i<=n;i++)
            if(mp[i][j]) mp[i]|=mp[j];
    ans=n*(n-1)/2;
    for(int i=1;i<=n;i++)
        for(int j=1;j<=n;j++) ans-=mp[i][j];
    printf("%d\n",ans);
    return 0;
}

 

Guess you like

Origin www.cnblogs.com/LLTYYC/p/11417459.html