P4047 [JSOI2010] tribe divided disjoint-set

Ideas: disjoint-set + Spanning Tree

Submission: 2 (although the sample but never had the feeling was right $ QwQ $ (judge side lost a))

answer:

All points between the connected edges, then $ $ Sort again, from small to large plus side, even until the first $ n-k + 1 $ sides (corresponding to the case is broken $ k $ shortest side communication block), recording weight value is the answer.

#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

Guess you like

Origin www.cnblogs.com/Jackpei/p/11223871.html