洛谷P4589 [TJOI2018]智力竞赛(二分答案 二分图匹配)

题意

题目链接

给出一个带权有向图,选出n + 1n+1条链,问能否全部点覆盖,如果不能,问不能覆盖的点权最小值最大是多少

Sol

TJOI怎么净出板子题

二分答案之后直接二分图匹配check一下。

多读读题就会发现题目要求的就是可相交的最小路径覆盖,那么按照套路先floyd一遍,如果能联通的话就再二分图中加边,然后判一下最大匹配数就行了。刚开始以为因为有的点可以不选,要在匈牙利的时候进行玄学贪心,其实是不用的,因为我们已经求过传递闭包了。所以直接求就是对的

因为\(M \leqslant 500\),所以Floyd的时候要用bitset优化一下

#include<bits/stdc++.h> 
#define Pair pair<int, int>
#define MP(x, y) make_pair(x, y)
#define fi first
#define se second
//#define int long long 
#define ull signed long long 
#define LL long long 
#define Fin(x) {freopen(#x".in","r",stdin);}
#define Fout(x) {freopen(#x".out","w",stdout);}
using namespace std;
const int MAXN = 1001, mod = 1e9 + 7, INF = 1e9 + 10;
const double eps = 1e-9;
template <typename A, typename B> inline bool chmin(A &a, B b){if(a > b) {a = b; return 1;} return 0;}
template <typename A, typename B> inline bool chmax(A &a, B b){if(a < b) {a = b; return 1;} return 0;}
template <typename A, typename B> inline LL add(A x, B y) {if(x + y < 0) return x + y + mod; return x + y >= mod ? x + y - mod : x + y;}
template <typename A, typename B> inline void add2(A &x, B y) {if(x + y < 0) x = x + y + mod; else x = (x + y >= mod ? x + y - mod : x + y);}
template <typename A, typename B> inline LL mul(A x, B y) {return 1ll * x * y % mod;}
template <typename A, typename B> inline void mul2(A &x, B y) {x = (1ll * x * y % mod + mod) % mod;}
template <typename A> inline void debug(A a){cout << a << '\n';}
template <typename A> inline LL sqr(A x){return 1ll * x * x;}
inline int read() {
    char c = getchar(); int x = 0, f = 1;
    while(c < '0' || c > '9') {if(c == '-') f = -1; c = getchar();}
    while(c >= '0' && c <= '9') x = x * 10 + c - '0', c = getchar();
    return x * f;
}
int N, M, a[MAXN], ans[MAXN], vis[MAXN], tim = 1, link[MAXN], st[MAXN], top;
bitset<MAXN> can[MAXN];
bool dfs(int x) {
	for(int i = 1; i <= top; i++) {
		if(can[st[x]][st[i]]) {
			if(vis[i] == tim) continue;
			vis[i] = tim;
			if(!link[i] || (dfs(link[i]))) {link[i] = x; return 1;}
		}
	}
	return 0;
}
bool check(int val) {
	memset(link, 0, sizeof(link)); top = 0;
	for(int i = 1; i <= M; i++) if(a[i] < val) st[++top] = i;
	int ans = 0;
	for(int i = 1; i <= top; i++, tim++) if(dfs(i)) ans++;
	return top - ans <= N + 1;
}
signed main() {
	N = read(); M = read(); 
	int mx = 0;
	for(int i = 1; i <= M; i++) {
		a[i] = read(); int k = read(); mx = max(a[i], mx);
		for(int j = 1; j <= k; j++) can[i][read()] = 1;
	}
	for(int k = 1; k <= M; k++)
		for(int i = 1; i <= M; i++)
			if(can[i][k])
				can[i] |= can[k];
	int l = 0, r = mx, ans = 0;
	while(l <= r) {
		int mid = l + r >> 1;
		if(check(mid)) ans = mid, l = mid + 1;
		else r = mid - 1;
	}
	if(ans >= mx) puts("AK");
	else cout << ans;
    return 0;
}
/*

*/
posted @ 2019-03-01 16:38  自为风月马前卒  阅读(392)  评论(1编辑  收藏  举报

Contact with me