BZOJ 1821 部落划分(二分+并查集)

答案是具有单调性的。 因为最近的两个部落的距离为mid,所以要是有两个野人的距离<mid,则他们一定是一个部落的。

用并查集维护各联通块,如果最后的联通块个数>=k,那么mid还可以再小点。如果<k,mid还可以再大点。

二分搞一搞就行了。

 

# include <cstdio>
# include <cstring>
# include <cstdlib>
# include <iostream>
# include <vector>
# include <queue>
# include <stack>
# include <map>
# include <set>
# include <cmath>
# include <algorithm>
using namespace std;
# define lowbit(x) ((x)&(-x))
# define pi 3.1415926535
# define eps 1e-9
# define MOD 45989
# define INF 1000000000
# define mem(a,b) memset(a,b,sizeof(a))
# define FOR(i,a,n) for(int i=a; i<=n; ++i)
# define FO(i,a,n) for(int i=a; i<n; ++i)
# define bug puts("H");
# define lch p<<1,l,mid
# define rch p<<1|1,mid+1,r
# define mp make_pair
# define pb push_back
typedef pair<int,int> PII;
typedef vector<int> VI;
# pragma comment(linker, "/STACK:1024000000,1024000000")
typedef long long LL;
int Scan() {
    int res=0, flag=0;
    char ch;
    if((ch=getchar())=='-') flag=1;
    else if(ch>='0'&&ch<='9') res=ch-'0';
    while((ch=getchar())>='0'&&ch<='9')  res=res*10+(ch-'0');
    return flag?-res:res;
}
void Out(int a) {
    if(a<0) {putchar('-'); a=-a;}
    if(a>=10) Out(a/10);
    putchar(a%10+'0');
}
const int N=100005;
//Code begin...

int a[N][2], f[N], n, k, fa[N];

int find(int x)
{
    int s, temp;
    for (s=x; fa[s]>=0; s=fa[s]) ;
    while (s!=x) temp=fa[x], fa[x]=s, x=temp;
    return s;
}
void union_set(int x, int y)
{
    int temp=fa[x]+fa[y];
    if (fa[x]>fa[y]) fa[x]=y, fa[y]=temp;
    else fa[y]=x, fa[x]=temp;
}
bool check(double x){
    int res=n, u, v;
    x=x*x;
    mem(fa,-1);
    FOR(i,1,n) FOR(j,i+1,n) {
        if ((a[i][0]-a[j][0])*(a[i][0]-a[j][0])+(a[i][1]-a[j][1])*(a[i][1]-a[j][1])>=x) continue;
        u=find(i), v=find(j);
        if (u!=v) union_set(u,v), --res;
    }
    return res>=k;
}
int main ()
{
    scanf("%d%d",&n,&k);
    FOR(i,1,n) scanf("%d%d",&a[i][0],&a[i][1]);
    double l=0, r=20000, mid;
    FOR(i,1,50) {
        mid=(l+r)/2;
        if (check(mid)) l=mid;
        else r=mid;
    }
    printf("%.2lf\n",mid);
    return 0;
}
View Code

 

posted @ 2017-03-30 18:24  free-loop  阅读(191)  评论(0编辑  收藏  举报