# BZOJ-2654 tree

2654: tree

V<=50000,E<=100000,所有数据边权为[1,100]中的正整数。

2 2 1

0 1 1 1

0 1 2 0

2

#include<cstdio>
#include<cstdlib>
#include<cstring>
#include<iostream>
#include<algorithm>
#define LL long long
using namespace std;
const int maxn=5e4+5,maxm=1e5+5;
int n,m,ned,fa[maxn],n1,n2,ans;
struct js{
int x,y,s;
}blk[maxm],wit[maxm];
{
int ret=0,f=1;char ch=getchar();
while (ch<'0'||ch>'9') {if (ch=='-') f=-1;ch=getchar();}
while (ch>='0'&&ch<='9') ret=ret*10+ch-'0',ch=getchar();
return ret*f;
}
int get(int x){return (fa[x]==x)?x:fa[x]=get(fa[x]);}
bool cmp(js x,js y){return x.s<y.s;}
int check(int tem)
{
for (int i=1;i<=n;i++) fa[i]=i;
int L1=1,L2=1,cnt=0,tot=n-1;
js nxt;int p=1,sum=0;
for (int i=1;i<=m;i++)
{
if (wit[L2].s+tem<=blk[L1].s) nxt=wit[L2++],p=1;
else nxt=blk[L1++],p=0;
int x=get(nxt.x),y=get(nxt.y);
if (x==y) continue;fa[x]=y;
sum+=nxt.s+tem*p;cnt+=p;
if (!(--tot)) break;
}
if (cnt>=ned) ans=sum-(LL)ned*tem;
//最重要的就是-ned*tem，扣回多出代价
return cnt;
}
int main()
{
for (int i=1;i<=m;i++)
{
if (c) blk[++n1]=(js){x,y,s};
else wit[++n2]=(js){x,y,s};
}
sort(blk+1,blk+n1+1,cmp);
sort(wit+1,wit+n2+1,cmp);//分成两组，可以避免二分check时多次排序
blk[++n1].s=1e9;wit[++n2].s=1e9;
int L=-105,R=105;ans=1e9;
while (L<=R)
{
int mid=(R-L>>1)+L;
}