• P4047 [JSOI2010]部落划分 并查集


    思路:并查集+生成树

    提交:2次(虽然样例都没过但感觉是对的$QwQ$(判边少了一条))

    题解:

    把所有点之间连边,然后$sort$一遍,从小往大加边,直到连第$n-k+1$条边(相当于是破话$k$个连通块的最短边),记录权值即为答案。

    #include<cstdio>
    #include<iostream>
    #include<cstring>
    #include<algorithm>
    #include<cmath>
    #define ull unsigned long long
    #define ll long long
    #define R register int
    using namespace std;
    #define pause (for(R i=1;i<=10000000000;++i))
    #define In freopen("NOIPAK++.in","r",stdin)
    #define Out freopen("out.out","w",stdout)
    namespace Fread {
    static char B[1<<15],*S=B,*D=B;
    #ifndef JACK
    #define getchar() (S==D&&(D=(S=B)+fread(B,1,1<<15,stdin),S==D)?EOF:*S++)
    #endif
    inline int g() {
        R ret=0,fix=1; register char ch; while(!isdigit(ch=getchar())) fix=ch=='-'?-1:fix;
        if(ch==EOF) return EOF; do ret=ret*10+(ch^48); while(isdigit(ch=getchar())); return ret*fix;
    } inline bool isempty(const char& ch) {return (ch<=36||ch>=127);}
    inline void gs(char* s) {
        register char ch; while(isempty(ch=getchar()));
        do *s++=ch; while(!isempty(ch=getchar()));
    }
    } using Fread::g; using Fread::gs;
    
    namespace Luitaryi {
    const int N=1010,M=N*N;
    int n,k,cnt,tot,fa[N];
    double ans;
    struct node {int x,y;}p[N];
    #define x(i) p[i].x
    #define y(i) p[i].y
    struct edge { int u,v; double w; edge() {}
        edge(int uu,int vv,double ww) {u=uu,v=vv,w=ww;}
        inline bool operator <(const edge& that) {return w<that.w;}
    }e[M];
    inline int getf(int x) {return x==fa[x]?x:fa[x]=getf(fa[x]);}
    inline void main() {
        n=g(),k=g();
        for(R i=1;i<=n;++i) x(i)=g(),y(i)=g();
        for(R i=1;i<=n;++i) for(R j=i+1;j<=n;++j) 
            e[++cnt]=edge(i,j,sqrt((x(i)-x(j))*(x(i)-x(j))+(y(i)-y(j))*(y(i)-y(j))));
        sort(e+1,e+cnt+1); for(R i=1;i<=n;++i) fa[i]=i;
        for(R i=1;i<=cnt;++i) { R u=e[i].u,v=e[i].v; register double w=e[i].w;
            R uf=getf(u),vf=getf(v);
            if(uf==vf) continue;
            else {
                ans=w; ++tot;
                fa[uf]=vf;
                if(tot==n-k+1) break;
            } 
        } printf("%.2lf",ans);
    }
    }
    signed main() {
        Luitaryi::main();
    }

    2019.07.22

  • 相关阅读:
    UIScrollView
    xcode debug
    ios 开发小技巧
    xcode调试
    Objective-C的反射机制
    git 命令
    iOS block的一些理解
    iOS 开发常用宏
    iOS 常用函数
    转:支付宝系统架构(内部架构图)
  • 原文地址:https://www.cnblogs.com/Jackpei/p/11223871.html
Copyright © 2020-2023  润新知