题目链接

题意:有A、B、C3个任务分配给n个宇航员,当中每一个宇航员恰好分配一个任务。如果n个宇航员的平均年龄为x,仅仅有年龄大于x的才干领取A任务;仅仅有年龄严格小于x的才干领取B任务,而任务C没有限制。有m对宇航员相互讨厌,因此不能分配同一任务。

求出能否找出符合的任务方案。

思路:用xi表示第i个宇航员的分配方案。

年龄大于等于x的能够选择A(xi = true)和C(xi+1 = false),年龄小雨x的能够选择B(xi = true)和C(xi+1 = false)。考虑一对互相讨厌的宇航员的话,当不属于同一类时。能够为xi V xj,即两个之中要有一个为真;当属于同一类时,要用两个语句表示xi V xj、~xi V ~xj。即前者表示一个为true,后者表示一个为false。

代码:

#include<cstdio>
#include<vector>
#include<cstring>
#include<algorithm>
using namespace std; const int maxn = 100005; struct 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));
} //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 < 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;
}
}; TwoSAT solver; int n, m, total_age, age[maxn]; int is_young(int x) {
return age[x] * n < total_age;
} int main() {
while(scanf("%d%d", &n, &m) == 2 && n) {
total_age = 0;
for(int i = 0; i < n; i++) {
scanf("%d", &age[i]);
total_age += age[i];
} solver.init(n);
for(int i = 0; i < m; i++) {
int a, b;
scanf("%d%d", &a, &b);
a--;
b--;
if(a == b) continue;
solver.add_clause(a, 1, b, 1);
if(is_young(a) == is_young(b))
solver.add_clause(a, 0, b, 0);
} if(!solver.solve()) printf("No solution.\n");
else {
for(int i = 0; i < n; i++)
if(solver.mark[i*2]) printf("C\n");
else if(is_young(i)) printf("B\n");
else printf("A\n");
}
}
return 0;
}

版权声明:本文博主原创文章。博客,未经同意不得转载。

UVALive3713-Astronauts(2-SAT)的更多相关文章

  1. UVAlive3713 Astronauts(2-SAT)

    题目链接:http://acm.hust.edu.cn/vjudge/problem/viewProblem.action?id=18511 [思路] 2-SAT. 设分得A或B类任务为1 C类任务为 ...

  2. UVALive-3713 Astronauts (2-SAT)

    题目大意:有三个任务A.B.C,n个已知年龄的人.A任务只能被年龄不小于平均年龄的人做,B任务只能被平均年龄以下的人做,C任务不限,相互讨厌的两个人不能做同一件任务,现在已知厌恶关系,求一种任务分配方 ...

  3. 多边形碰撞 -- SAT方法

    检测凸多边形碰撞的一种简单的方法是SAT(Separating Axis Theorem),即分离轴定理. 原理:将多边形投影到一条向量上,看这两个多边形的投影是否重叠.如果不重叠,则认为这两个多边形 ...

  4. UVALive - 3713 Astronauts

    给定n个宇航员的年龄,平均年龄为 ave,根据下列要求分配任务: B任务只能分配给年龄<ave的宇航员: A任务只能分配给年龄>=ave的宇航员: C任务可以任意分配. 给定m组互相憎恨的 ...

  5. POJ 3678 Katu Puzzle(2 - SAT) - from lanshui_Yang

    Description Katu Puzzle is presented as a directed graph G(V, E) with each edge e(a, b) labeled by a ...

  6. UVA 3713 Astronauts

    The Bandulu Space Agency (BSA) has plans for the following three space missions: • Mission A: Landin ...

  7. UVALive - 3713 - Astronauts(图论——2-SAT)

    Problem   UVALive - 3713 - Astronauts Time Limit: 3000 mSec Problem Description Input The input cont ...

  8. Map Labeler POJ - 2296(2 - sat 具体关系建边)

    题意: 给出n个点  让求这n个点所能建成的正方形的最大边长,要求不覆盖,且这n个点在正方形上或下边的中点位置 解析: 当然是二分,但建图就有点还行..比较难想..行吧...我太垃圾... 2 - s ...

  9. 学习笔记(two sat)

    关于two sat算法 两篇很好的论文由对称性解2-SAT问题(伍昱), 赵爽 2-sat解法浅析(pdf). 一些题目的题解 poj 3207 poj 3678 poj 3683 poj 3648 ...

  10. LA 3211 飞机调度(2—SAT)

    https://vjudge.net/problem/UVALive-3211 题意: 有n架飞机需要着陆,每架飞机都可以选择“早着陆”和“晚着陆”两种方式之一,且必须选择一种,第i架飞机的早着陆时间 ...

随机推荐

  1. 帝国cms在任意位置调用指定id的栏目名称和链接

    注意,这个代码无须放在灵动标签中,直接写入模板相应的位置就行了.[1]调用栏目名称: <?=$class_r[栏目ID]['classname']?>   示例:<?=$class_ ...

  2. bitVector@ java bit自我practice##Q&A:为何int 来初始化size of bitVector?long,甚至是BigInteger等策略

    /* * BitSets are packed into arrays of "words." Currently a word is * a long, which consis ...

  3. css 多出一行或多行后显示...的方法

    一行超出显示... .mui-ellipsis { overflow: hidden; white-space: nowrap; text-overflow: ellipsis; } 两行超出的显示. ...

  4. "The connection for the USB device '###' was unsuccessful. The device is currently in use"

    一.问题描述 1.情景描述 笔者的物理主机系统是“windows7 64位”,想使用“摄像头录像大师”.这个软件在录制视频的过程中,需要调用windows自带的"windows media ...

  5. jsp表单提交中文乱码的解决

    <%@ page language="Java" contentType="text/html; charset=utf-8 "    pageEncod ...

  6. Java客户端协议处理框架简介

    无论FTP客户程序,还是HTTP客户程序,或是其他基于特定应用层协议的客户程序,在与远程服务器通信时,都需要建立与远程服务器的连接,然后发送和接收与协议相符的数据.客户程序还需要对服务器发送的数据进行 ...

  7. [BZOJ 1115] [POI2009] 石子游戏Kam 【阶梯博弈】

    题目链接:BZOJ - 1115 题目分析 首先看一下阶梯博弈: 阶梯博弈是指:初始有 n 堆石子,每次可以从任意的第 i 堆拿若干石子放到第 i - 1 堆.最终不能操作的人失败. 解法:将奇数位的 ...

  8. XP系统VPN设置

    为了解除公司上网策略限制,或者为了上Google,Facebook,都可以通过设置VPN实现. 要使用VPN需要到VPN服务商注册,链接VPN服务商. ======================== ...

  9. QSplashScreen开机画面(不断的repaint)

    QApplication a(argc, argv);    QPixmap pixmap(":/Image/start.png");//绑定启动图片    QSplashScre ...

  10. 【HDOJ】3549 Flow Problem

    网络流基础题目,Edmonds_Karp可解. /* 3549 */ #include <iostream> #include <string> #include <ma ...