UVALIVE 3713 Astronauts(2-SAT)
来源:互联网 发布:淘宝怎样找货源 编辑:程序博客网 时间:2024/05/20 01:38
题意:有A,B,C三个任务要分给n个宇航员,年龄大于等于平均年龄的分A, 年龄小于平均年龄的分在B,C组没有界限,互为敌人的两个宇航员不可以在同一组.求出一个分配的方案。
思路:可以发现每个宇航员只有两种选择,选c或者不选c,用一个布尔变量xi代表第i个宇航员的选择,
对于相互为敌的两个年龄同时大于等于或同时小于平均年龄的宇航员,需要满足!(xi && xj)和!((!xi) && !(!xj))
对于年龄在平均年龄两侧的两个宇航员,只需要满足!(xi && xj),得到这些条件就可以建图然后用2-SAT算法求得答案。
#include<cstdio> #include<cstring> #include<cmath> #include<cstdlib> #include<iostream> #include<algorithm> #include<vector> #include<map> #include<queue> #include<stack> #include<string>#include<map> #include<set>#define eps 1e-6 #define LL long long using namespace std; const int maxn = 100000 + 1000;//const int INF = 0x3f3f3f3f;//2-satstruct TwoSat {int n;vector<int> G[maxn*2];bool mark[maxn*2];int S[maxn*2], c;bool dfs(int x) {if(mark[x^1]) return false;if(mark[x]) return true;mark[x] = true;S[c++] = x;for(int i = 0; i < G[x].size(); i++) {if(!dfs(G[x][i])) return false;}return true;}void init(int n) {this->n = n;for(int i = 0; i < n*2; i++) G[i].clear();memset(mark, 0, sizeof(mark));}void add_clause(int x, int xval, int y, int yval) {x = x*2 + xval;y = y*2 + yval;G[x^1].push_back(y);G[y^1].push_back(x);}bool solve() {for(int i = 0; i < n*2; i += 2) {if(!mark[i]&&!mark[i+1]) {c = 0;if(!dfs(i)) {while(c > 0) mark[S[--c]] = false;if(!dfs(i+1)) return false;}}}return true;}} solver;int n, m, age[maxn];int main() {//freopen("input.txt", "r", stdin);while(scanf("%d%d", &n, &m)==2 && n) {solver.init(n);int su = 0, ave;for(int i = 0; i < n; i++) scanf("%d", &age[i]), su += age[i];ave = su%n==0 ? su/n : su/n+1;for(int i = 0; i < m; i++) {int u, v; scanf("%d%d", &u, &v);u--; v--;if(age[u]<ave&&age[v]<ave || age[u]>=ave&&age[v]>=ave) {solver.add_clause(u, 0, v, 0);solver.add_clause(u, 1, v, 1);}else {solver.add_clause(u, 0, v, 0);}}if(!solver.solve()) puts("No solution.");else {for(int i = 0; i < n; i++) {if(solver.mark[2*i]) {if(age[i] >= ave) puts("A");else puts("B");} else puts("C");}}} return 0;}
0 0
- UVALive 3713 Astronauts(2SAT)
- UVALIVE 3713 Astronauts(2-SAT)
- UVALive 3713 - Astronauts(2-SAT)
- UVALive - 3713 Astronauts(2-SAT)
- UVALive 3713 Astronauts(2-sat+输出任意路径)
- LA 3713 - Astronauts 2-SAT
- uva 3713 Astronauts (2-sat)
- LA 3713 Astronauts(2-SAT)
- UVAlive3713 Astronauts(2-SAT)
- UvaLive-3713-Astronauts
- UVALive 3713 Astronauts
- UVALive - 3713 Astronauts
- UVALive 3713 Astronauts
- uva 1391 - Astronauts(2sat)
- LA3713--Astronauts(2-sat)
- uva1391 - Astronauts 2-SAT
- UVALive3713-Astronauts(2-SAT)
- La3713 Astronauts 2-Sat
- 彻底根除“无法安装64位版本的Office,因为在您的PC上找到了以下32位程序:”的问题
- Ugly Number
- STL学习----入门(1)[forward_list]
- 文章标题
- JAVA wait(), notify(),sleep详解
- UVALIVE 3713 Astronauts(2-SAT)
- 同一字符串,MD5加密后出现不同的结果
- Android中Timer使用方法
- Android中View绘制优化之一---- 优化布局层次
- 解决Ubuntu系统中文乱码显示问题
- 从dll得到lib文件
- 黑马程序员-----正则表达式
- constexpr和常量表达式
- Cannot set TextView width using setWidth or setLayoutParams