Luogu2798 爆弹虐场 (二分,Kruskal)
二分答案,判定连通性
#include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm>
#include <cmath>
#define R(a,b,c) for(register int a = (b); a <= (c); ++ a)
#define nR(a,b,c) for(register int a = (b); a >= (c); -- a)
#define Max(a,b) ((a) > (b) ? (a) : (b))
#define Min(a,b) ((a) < (b) ? (a) : (b))
#define Fill(a,b) memset(a, b, sizeof(a))
#define Abs(a) ((a) < 0 ? -(a) : (a))
#define Swap(a,b) a^=b^=a^=b
#define ll long long
#define ON_DEBUG
#ifdef ON_DEBUG
#define D_e_Line printf("\n\n----------\n\n")
#define D_e(x) cout << #x << " = " << x << endl
#define Pause() system("pause")
#define FileOpen() freopen("in.txt","r",stdin);
#else
#define D_e_Line ;
#define D_e(x) ;
#define Pause() ;
#define FileOpen() ;
#endif
struct ios{
template<typename ATP>ios& operator >> (ATP &x){
x = 0; int f = 1; char c;
for(c = getchar(); c < '0' || c > '9'; c = getchar()) if(c == '-') f = -1;
while(c >= '0' && c <= '9') x = x * 10 + (c ^ '0'), c = getchar();
x*= f;
return *this;
}
}io;
using namespace std;
const int N = 20007;
int n, k, m;
struct Node{
int x, y, w1, w2;
}a[N];
inline bool cmp_w1(const Node &a, const Node &b){
return a.w1 < b.w1;
}
inline bool cmp_w2(const Node &a, const Node &b){
return a.w2 < b.w2;
}
int fa[N];
inline int Find(int x){
return x == fa[x] ? x : fa[x] = Find(fa[x]);
}
inline bool Check(int tim){
int totTeen = 0, totLeason = 0;
R(i,1,n) fa[i] = i;
R(i,1,m){
int p = Find(a[i].x), q = Find(a[i].y);
if(p != q && tim >= a[i].w2){
if(a[i].w1 <= tim) ++totTeen;
fa[p] = q;
++totLeason;
}
}
return ((totLeason >= n - 1) && (totTeen >= k));
}
int main(){
io >> n >> k >> m;
R(i,1,m){
io >> a[i].x >> a[i].y >> a[i].w1 >> a[i].w2;
}
sort(a + 1, a + m + 1, cmp_w1);
int l = 1, r = 1e6;
while(l <= r){
int mid = (l + r) >> 1;
if(Check(mid))
r = mid - 1;
else
l = mid + 1;
}
printf("%d", l);
return 0;
}