# BZOJ 4883 棋盘上的守卫 解题报告

## BZOJ4883 棋盘上的守卫

Code:

#include <cstdio>
#include <cctype>
#include <cstring>
#include <algorithm>
#define ll long long
using std::max;
using std::min;
const int SIZE=1<<21;
char ibuf[SIZE],*iS,*iT;
#define gc() getchar()
template <class T>
{
x=0;char c=gc();
while(!isdigit(c)) c=gc();
while(isdigit(c)) x=x*10+c-'0',c=gc();
}
const int N=2e5+10;
int n,m,k;
ll ans;
struct node
{
int u,v,w;
node(){}
node(int a,int b,int c){u=a,v=b,w=c;}
bool friend operator <(node a,node b){return a.w<b.w;}
}E[N];
int f[N],huan[N];
int Find(int x){return f[x]=f[x]==x?x:Find(f[x]);}
int main()
{
for(int w,i=1;i<=n;i++)
for(int j=1;j<=m;j++)
{
E[++k]=node(i,j+n,w);
}
for(int i=1;i<=n+m;i++) f[i]=i;
std::sort(E+1,E+1+k);
for(int i=1;i<=k;i++)
{
int u=Find(E[i].u),v=Find(E[i].v);
if(u!=v&&!(huan[u]&huan[v]))
{
huan[u]|=huan[v];
f[v]=u;
ans+=E[i].w;
}
if(u==v&&!huan[u])
{
huan[u]=1;
ans+=E[i].w;
}
}
printf("%lld\n",ans);
return 0;
}

2019.6.26

posted @ 2019-06-26 11:23  露迭月  阅读(...)  评论(...编辑  收藏