[TJOI2018]智力竞赛
作者:互联网
题目大意
给出一个 \(n\) 个点的带权有向图 \(G\),选出 \(m+1\) 条链,问能否全部点覆盖,如果不能,问 不能 覆盖的点权最小值最大是多少。
(这里的 \(n,m\) 和原题是反着的)
题目分析
其实就是求 \(\verb!DAG!\) 的最小可重复路径点覆盖。我们可以先将这个图传递闭包,求出两个点是否能相互抵达,能则按顺序连边。最后就转换为了求 \(\verb!DAG!\) 的最小路径覆盖问题。
然后我们又知道最小路径覆盖数等于点数减去最大匹配数,所以匈牙利算法求解即可。
可题目要我们最大化不能全部点覆盖的点权的最小值,最小值最大,当“最小”“最大”连在一起的时候,大部分情况不是二分就是 Kruskal 重构树。这里很显然是二分答案。
我们二分一个值 \(now\),判断 当图的点权最大值为 \(now\) 时是否能够全部点覆盖,方法与上面类似(特别注意点的总数不是 \(n\)),如果最小路径覆盖数不超过 \(m+1\) 就满足条件。
求的是“不能”覆盖的点权的最小值,也就是“能”覆盖的点权的最大值(满足这个最大值最大)再 加一。
时限会被卡,匈牙利算法打上时间戳、floyd 简单剪枝就能过了。
代码
// Problem: P4589 [TJOI2018]智力竞赛
// Contest: Luogu
// URL: https://www.luogu.com.cn/problem/P4589
// Memory Limit: 250 MB
// Time Limit: 1000 ms
// Date:2022-06-18 08:23
//
// Powered by CP Editor (https://cpeditor.org)
#include <iostream>
#include <cstdio>
#include <climits>//need "INT_MAX","INT_MIN"
#include <cstring>//need "memset"
#include <numeric>
#include <algorithm>
#include <cmath>
#define enter putchar(10)
#define debug(c,que) std::cerr << #c << " = " << c << que
#define cek(c) puts(c)
#define blow(arr,st,ed,w) for(register int i = (st);i <= (ed); ++ i) std::cout << arr[i] << w;
#define speed_up() std::ios::sync_with_stdio(false),std::cin.tie(0),std::cout.tie(0)
#define mst(a,k) memset(a,k,sizeof(a))
#define stop return(0)
const int mod = 1e9 + 7;
inline int MOD(int x) {
if(x < 0) x += mod;
return x % mod;
}
namespace Newstd {
char buf[1 << 21],*p1 = buf,*p2 = buf;
inline int getc() {
return p1 == p2 && (p2 = (p1 = buf) + fread(buf,1,1 << 21,stdin),p1 == p2) ? EOF : *p1 ++;
}
inline int read() {
int ret = 0,f = 0;char ch = getc();
while (!isdigit(ch)) {
if(ch == '-') f = 1;
ch = getc();
}
while (isdigit(ch)) {
ret = (ret << 3) + (ret << 1) + ch - 48;
ch = getc();
}
return f ? -ret : ret;
}
inline double double_read() {
long long ret = 0,w = 1,aft = 0,dot = 0,num = 0;
char ch = getchar();
while (!isdigit(ch)) {
if (ch == '-') w = -1;
ch = getchar();
}
while (isdigit(ch) || ch == '.') {
if (ch == '.') {
dot = 1;
} else if (dot == 0) {
ret = (ret << 3) + (ret << 1) + ch - 48;
} else {
aft = (aft << 3) + (aft << 1) + ch - '0';
num ++;
}
ch = getchar();
}
return (pow(0.1,num) * aft + ret) * w;
}
inline void write(int x) {
if(x < 0) {
putchar('-');
x = -x;
}
if(x > 9) write(x / 10);
putchar(x % 10 + '0');
}
}
using namespace Newstd;
const int INF = 0x3f3f3f3f;
const int N = 505;
struct Graph {
int v,nxt;
} gra[N * N];
int head[N],val[N],vis[N],mat[N];
bool G[N][N],E[N][N];
int n,m,idx,nowidx;
inline void add(int u,int v) {
gra[++ idx] = (Graph){v,head[u]},head[u] = idx;
}
inline bool dfs(int now) {
for (register int i = head[now];i;i = gra[i].nxt) {
int v = gra[i].v;
if (vis[v] != nowidx) {
vis[v] = nowidx;
if (!mat[v] || dfs(mat[v])) {
mat[v] = now;
return true;
}
}
}
return false;
}
inline void floyd(bool F[][N]) {
for (register int k = 1;k <= n; ++ k) {
for (register int i = 1;i <= n; ++ i) {
if (!F[i][k]) continue;
for (register int j = 1;j <= n; ++ j) {
if (F[i][k] && F[k][j]) {
F[i][j] = true;
}
}
}
}
}
//图的点权最大值为 now 时是否能够全部点覆盖
inline bool check(int now) {
idx = nowidx = 0;
mst(gra,0),mst(E,false),mst(vis,0),mst(mat,0),mst(head,0);
for (register int i = 1;i <= n; ++ i) {
for (register int j = 1;j <= n; ++ j) {
if (G[i][j] && val[i] <= now && val[j] <= now) {
E[i][j] = true;
}
}
}
floyd(E);
int num = 0,ans = 0;
for (register int i = 1;i <= n; ++ i) {
if (val[i] <= now) {
num ++;
for (register int j = 1;j <= n; ++ j) {
if (E[i][j]) {
add(i,j);
}
}
}
}
for (register int i = 1;i <= n; ++ i) {
if (val[i] <= now) {
nowidx ++;
if (dfs(i)) ans ++;
}
}
return num - ans <= m;
}
int main(void) {
m = read() + 1,n = read();
int l = INF,r = 0;
for (register int i = 1,t;i <= n; ++ i) {
val[i] = read(),t = read();
l = std::min(l,val[i]),r = std::max(r,val[i]);
while (t --) {
int x = read();
G[i][x] = true;
}
}
floyd(G);
int ans = -1;
while (l <= r) {
int mid = l + r >> 1;
if (check(mid)) l = mid + 1,ans = mid;
else r = mid - 1;
}
int maxx = 0;
for (register int i = 1;i <= n; ++ i) maxx = std::max(maxx,val[i]);
if (ans + 1 > maxx) puts("AK");
else printf("%d\n",ans + 1);
return 0;
}
标签:head,now,覆盖,int,TJOI2018,点权,include,智力竞赛 来源: https://www.cnblogs.com/Coros-Trusds/p/16387666.html