洛谷P4589 [TJOI2018]智力竞赛(二分答案 二分图匹配)
程序员文章站
2022-04-28 11:09:42
题意 "题目链接" 给出一个带权有向图,选出n + 1n+1条链,问能否全部点覆盖,如果不能,问不能覆盖的点权最小值最大是多少 Sol ~~TJOI怎么净出板子题~~ 二分答案之后直接二分图匹配check一下。 多读读题就会发现题目要求的就是可相交的最小路径覆盖,那么按照套路先floyd一遍,如果能 ......
题意
给出一个带权有向图,选出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; } /* */
下一篇: 10个经典的Java面试题集合