# Bzoj2654 tree

## 通过参数二分得到最优解

Posted by yjjr's blog on March 4, 2019

# 题目

## Sample Input

2 2 1
0 1 1 1
0 1 2 0


## Sample Output

2


# code

#include<iostream>
#include<cstdio>
#include<cstdlib>
#include<cstring>
#include<cmath>
#include<algorithm>
#define rep(i,a,b) for(int i=a;i<=b;i++)
#define dep(i,a,b) for(int i=a;i>=b;i--)
#define mem(x,num) memset(x,num,sizeof x)
#define reg(x) for(int i=last[x];i;i=e[i].next)
#define ll long long
using namespace std;
ll f=1,x=0;char ch=getchar();
while(ch<'0'||ch>'9'){if(ch=='-')f=-1;ch=getchar();}
while(ch>='0'&&ch<='9'){x=x*10+ch-'0';ch=getchar();}
return x*f;
}
const int maxn=1e5+6;
int n,m,cnt,tot,Need,ans,u[maxn],v[maxn],w[maxn],c[maxn],fa[maxn];
struct edge{int u,v,w,c;}e[maxn<<1];
inline bool cmp(edge x,edge y){return x.w==y.w?x.c<y.c:x.w<y.w;}
inline int find(int x){return x==fa[x]?x:fa[x]=find(fa[x]);}
inline bool Check(int x){
tot=cnt=0;
rep(i,0,n)fa[i]=i;mem(e,0);
rep(i,1,m){
e[i].u=u[i],e[i].v=v[i],e[i].w=w[i],e[i].c=c[i];
if(!c[i])e[i].w+=x;
}
sort(e+1,e+1+m,cmp);
rep(i,1,m){
int r1=find(e[i].u),r2=find(e[i].v);
if(r1!=r2){
fa[r1]=r2;
tot+=e[i].w;
if(!e[i].c)cnt++;
}
}
return cnt>=Need;
}
int main(){