bzoj 3714 [PA2014]Kuglarz——思路+最小生成树

题目:https://www.lydsy.com/JudgeOnline/problem.php?id=3714

如果用s[ i ]表示前 i 个的奇偶性,那么c(i_j)表示s[ i-1 ]^s[ j ]。知道其中一个就能知道另一个。

已知s[ 0 ]=0。所以把 c 看成连边,从0能走到每个点就行。即求一个最小生成树。

然后跑得很慢地A了。也不知为何这么慢。

#include<iostream>
#include<cstdio>
#include<cstring>
#include<algorithm>
#define ll long long
using namespace std;
const int N=2005;
int n,xnt,fa[N],cnt;
ll ans;
struct Ed{
    int x,y,w;
    Ed(int x=0,int y=0,int w=0):x(x),y(y),w(w) {}
    bool operator< (const Ed &b) const
        {return w<b.w;}
}ed[N*N>>1];
int rdn()
{
    int ret=0;bool fx=1;char ch=getchar();
    while(ch>'9'||ch<'0'){if(ch=='-')fx=0;ch=getchar();}
    while(ch>='0'&&ch<='9') ret=(ret<<3)+(ret<<1)+ch-'0',ch=getchar();
    return fx?ret:-ret;
}
int find(int a){return fa[a]==a?a:fa[a]=find(fa[a]);}
int main()
{
    n=rdn();
    for(int i=1;i<=n;i++)
        for(int j=i,z;j<=n;j++)
        {
            z=rdn();
            ed[++xnt]=Ed(i-1,j,z);
        }
    sort(ed+1,ed+xnt+1);
    for(int i=1;i<=n;i++) fa[i]=i;
    for(int i=1,u,v;i<=xnt;i++)
    {
        u=find(ed[i].x); v=find(ed[i].y);
        if(u!=v)
        {
            fa[u]=v;ans+=ed[i].w;
            cnt++;
            if(cnt==n)break;
        }
    }
    printf("%lld\n",ans);
    return 0;
}

 

posted on 2018-09-25 16:58  Narh  阅读(117)  评论(0编辑  收藏  举报

导航