• bzoj3943[Usaco2015 Feb]SuperBull*


    bzoj3943[Usaco2015 Feb]SuperBull

    题意:

    n头牛进行锦标赛,每场比赛的好看程度是两头牛的编号异或和,并总有一方被淘汰。求安排比赛(可以决定比赛胜负)可以得到的最大总好看程度是多少。n≤2000

    题解:

    先求出牛两两之间的异或和,然后发现可以把比赛看做连边,且共有n-1场比赛,所以求最大生成树就行了。神犇们用的都是Prim,蒟蒻不会,用Kruscal结果时间排倒数。

    代码:

     1 #include <cstdio>
     2 #include <cstring>
     3 #include <algorithm>
     4 #define maxn 2010
     5 #define inc(i,j,k) for(int i=j;i<=k;i++)
     6 using namespace std;
     7 
     8 inline int read(){
     9     char ch=getchar(); int f=1,x=0;
    10     while(ch<'0'||ch>'9'){if(ch=='-')f=-1; ch=getchar();}
    11     while(ch>='0'&&ch<='9')x=x*10+ch-'0',ch=getchar();
    12     return f*x;
    13 }
    14 int a[maxn],fa[maxn],n,cnt; long long ans;
    15 struct e{int f,t,w;}; e es[maxn*maxn]; int ess;
    16 bool cmp(e a,e b){return a.w>b.w;}
    17 int find(int x){
    18     return x==fa[x]?x:fa[x]=find(fa[x]);
    19 }
    20 int main(){
    21     n=read(); inc(i,1,n)a[i]=read(),fa[i]=i;
    22     inc(i,1,n)inc(j,i+1,n)es[++ess]=(e){i,j,a[i]^a[j]};
    23     sort(es+1,es+1+ess,cmp);
    24     inc(i,1,ess){
    25         int x=find(es[i].f),y=find(es[i].t);
    26         if(x!=y)fa[x]=y,ans+=es[i].w,cnt++; if(cnt==n-1)break;
    27     }
    28     printf("%lld",ans); return 0;
    29 }

    20160825

  • 相关阅读:
    愚人节的礼物
    Image Transformation
    Rails
    Google Map
    Code Formatter
    ACboy needs your help again!
    Geek's Collection(幂运算)
    Train Problem I
    Beautiful Meadow
    Card Trick(模拟)
  • 原文地址:https://www.cnblogs.com/YuanZiming/p/5808458.html
Copyright © 2020-2023  润新知