我是靠谱客的博主 幸福歌曲,这篇文章主要介绍Codeforces 468B Two Sets(二分图匹配),现在分享给大家,希望可以做个参考。

题目链接:Codeforces 468B Two Sets

题目大意:给出n个数,要求将n个数分配到两个集合中,集合0中的元素x,要求A-x也再0中,同理1集合。

解题思路:类似二分图匹配的方法。

复制代码
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
#include <cstdio> #include <cstring> #include <map> #include <stack> #include <algorithm> using namespace std; const int maxn = 1e5 + 5; int N, A, B, x[maxn], v[maxn], r = 0; map<int, int> G; bool match (int a, int M, int k) { int p = G[a]; if (!G.count(M - a)) return false; int q = G[M - a]; if (v[q] == -1 || a * 2 == M) { v[p] = v[q] = k; } else { if (match(A + B - 2 * M + a, M, k)) v[p] = v[q] = k; else return false; } return true; } bool solve () { if (r >= max(A,B)) return false; for (int i = 1; i <= N; i++) { if (v[i] != -1) continue; if (!match(x[i], A, 0) && !match(x[i], B, 1)) return false; } return true; } int main () { scanf("%d%d%d", &N, &A, &B); memset(v, -1, sizeof(v)); for (int i = 1; i <= N; i++) { scanf("%d", &x[i]); r = max(x[i], r); G[x[i]] = i; } if (solve()) { printf("YESn"); for (int i = 1; i <= N; i++) printf("%d%c", v[i], i == N ? 'n' : ' '); } else printf("NOn"); return 0; }

最后

以上就是幸福歌曲最近收集整理的关于Codeforces 468B Two Sets(二分图匹配)的全部内容,更多相关Codeforces内容请搜索靠谱客的其他文章。

本图文内容来源于网友提供,作为学习参考使用,或来自网络收集整理,版权属于原作者所有。
点赞(63)

评论列表共有 0 条评论

立即
投稿
返回
顶部