P4589 [TJOI2018]智力竞赛(二分&最小链覆盖)
Posted Harris-H
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了P4589 [TJOI2018]智力竞赛(二分&最小链覆盖)相关的知识,希望对你有一定的参考价值。
P4589 [TJOI2018]智力竞赛(二分&最小链覆盖)
题意转换就是求最小相交链覆盖。
先floyd预处理。
然后二分答案。
每次根据 m i d mid mid建图,然后跑匈牙利。
#include<algorithm>
#include<iostream>
#include<cstring>
#include<cstdio>
#include<cmath>
#include<map>
#define Redge(u) for (int k = h[u],to; k; k = ed[k].nxt)
#define REP(i,n) for (int i = 1; i <= (n); i++)
#define mp(a,b) make_pair<int,int>(a,b)
#define cls(s) memset(s,0,sizeof(s))
#define cp pair<int,int>
#define LL long long int
using namespace std;
const int maxn = 505,maxm = 100005,INF = 1000000000;
inline int read(){
int out = 0,flag = 1; char c = getchar();
while (c < 48 || c > 57){if (c == '-') flag = -1; c = getchar();}
while (c >= 48 && c <= 57){out = (out << 3) + (out << 1) + c - 48; c = getchar();}
return out * flag;
}
int G[maxn][maxn],g[maxn][maxn],val[maxn],b[maxn],tot,n,m;
int lk[maxn],vis[maxn];
bool find(int u){
REP(i,n) if (g[u][i] && !vis[i]){
vis[i] = true;
if (!lk[i] || find(lk[i])){
lk[i] = u; return true;
}
}
return false;
}
bool check(int v){
int cnt = 0;
REP(i,n) if (val[i] < v) cnt++;
REP(i,n) REP(j,n)
if (val[i] < v && val[j] < v) g[i][j] = G[i][j];
else g[i][j] = 0;
cls(lk);
REP(i,n) if (val[i] < v){
cls(vis); if (find(i)) cnt--;
}
return cnt <= m + 1;
}
int main(){
m = read(); n = read(); int tmp;
REP(i,n){
b[i] = val[i] = read();
tmp = read();
while (tmp--) G[i][read()] = true;
}
REP(k,n) REP(i,n) REP(j,n) G[i][j] |= (G[i][k] & G[k][j]);
sort(b + 1,b + 1 + n); tot=unique(b+1,b+n+1)-b-1;
for (int i = 1; i <= n; i++) val[i] = lower_bound(b + 1,b + 1 + tot,val[i]) - b;
REP(i,n) REP(j,n) g[i][j] = G[i][j];
if (check(tot + 1)){puts("AK"); return 0;}
int l = 1,r = tot,mid;
while (l < r){
mid = l + r + 1 >> 1;
if (check(mid)) l = mid;
else r = mid - 1;
}
printf("%d\\n",b[l]);
return 0;
}
以上是关于P4589 [TJOI2018]智力竞赛(二分&最小链覆盖)的主要内容,如果未能解决你的问题,请参考以下文章