UVALive - 3713 Astronauts(2-SAT)
来源:互联网 发布:java8 javascript 编辑:程序博客网 时间:2024/05/20 02:55
题目大意:有A,B,C 3个任务要飞配给n个宇航员,现给出n个宇航员的年龄和每种任务的执行要求。
A任务只有年龄大于等于平均年龄的宇航员才可以执行
B任务只有年龄小于平均年龄的宇航员才可以执行
C任务任何宇航员都可以执行
现在有M对讨厌关系,互相讨厌的宇航员不能执行同一种任务,现要求找出一个满足上述所有要求的任务分配方案
解题思路:年龄大于等于平均年龄的宇航员只能执行A任务或者C任务,假设执行A任务为true
同理,年龄小于平均年龄的宇航员只能执行B任务或者C任务,假设执行B任务为true
如果两个宇航员相互讨厌,相互讨厌的情况得分两种
一种相互讨厌的宇航员是年龄都大于等于平均年龄或者年龄都小于平均年龄的,这种相互讨厌的情况下,两个宇航员只能执行不同的任务,因此要满足 一真一假的要求
另一种情况是年龄在平均年龄两侧的,这种情况的话,只要不同时执行C任务就可以了,因此要满足其中一个非假
#include <cstdio>#include <cstring>#include <vector>using namespace std;#define N 200010#define INF 0x3f3f3f3fstruct TwoSAT{ int n, c; vector<int> G[N]; bool mark[N]; int S[N]; 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 < 2 * n; i++) G[i].clear(); memset(mark, 0, sizeof(mark)); } //x == xval or y == yval 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 < 2 * n; 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; }};TwoSAT solver;int age[N], ave;int n, m;void solve() { if (!solver.solve()) printf("No solution.\n"); else { for (int i = 0; i < n; i++) { if (age[i] >= ave) { printf("%c\n", solver.mark[i * 2] ? 'A': 'C'); } else { printf("%c\n", solver.mark[i * 2] ? 'B': 'C'); } } }}void init() { int Sum = 0; for (int i = 0; i < n; i++) { scanf("%d", &age[i]); Sum += age[i]; } ave = (Sum + n - 1) / n; solver.init(n); int u, v; for (int i = 0; i < m; i++) { scanf("%d%d", &u, &v); u--; v--; if ( (age[u] >= ave && age[v] >= ave) || (age[u] < ave && age[v] < ave)) { solver.add_clause(u, 1, v, 1); solver.add_clause(u, 0, v, 0); } else { solver.add_clause(u, 0, v, 0); } }}int main() { while (scanf("%d%d", &n, &m) != EOF && n + m) { init(); solve(); } return 0;}
1 0
- UVALive 3713 - Astronauts(2-SAT)
- UVALive - 3713 Astronauts(2-SAT)
- UVALive 3713 Astronauts(2SAT)
- 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)
- UvaLive-3713-Astronauts
- UVALive 3713 Astronauts
- UVALive - 3713 Astronauts
- UVALive 3713 Astronauts
- LA3713--Astronauts(2-sat)
- uva1391 - Astronauts 2-SAT
- UVALive3713-Astronauts(2-SAT)
- La3713 Astronauts 2-Sat
- UVALive3713-Astronauts 2-SAT
- UVA1391、LA3713【astronauts...】【2 -SAT】
- 安卓的观察者模式
- fzu 1171 Hard to Believe, but True!
- java怎么用一行代码初始化ArrayList
- [Android Studio 权威教程]离线配置SDK,创建第一个AS项目
- [Android Studio 权威教程]配置出“NB”的Android Studio
- UVALive - 3713 Astronauts(2-SAT)
- 快速幂取模法
- [Android Studio 权威教程]Android Studio 三种添加插件的方式
- 567D One-Dimensional Battle Ships(set)
- CentOS6.3文字界面下使用yum安装Gnome桌面
- linux安装jdk
- [Android Studio 权威教程]最实用的快捷键
- Leetcode -- Roman to Integer
- 设备模型之kobject,kset及其关系