题目链接

题意:有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. 《JavaScript高级程序设计 第3版》-学习笔记-2

    P31-P82页 1.相等不相等与全等不全等 相等不相等:先转换后比较.对于只有一个对象,调用valueOf方法得到基本类型值再按基本类型转换:如果两个都是对象,则比较他们是否是同一个对象(引用或指针 ...

  2. 搭建hive到eclipse里面

    (1)下载源码 git clone https://git-wip-us.apache.org/repos/asf/hive.git git clone https://github.com/apac ...

  3. SQL Server与Oracle中的隔离级别

    在SQL92标准中,事务隔离级别分为四种,分别为:Read Uncommitted.Read Committed.Read Repeatable.Serializable 其中Read Uncommi ...

  4. JUnit扩展:引入新注解Annotation

    发现问题 JUnit提供了Test Suite来帮助我们组织case,还提供了Category来帮助我们来给建立大的Test Set,比如BAT,MAT, Full Testing. 那么什么情况下, ...

  5. 从零开始学习MySQL1---MySQL基础

    数据库基础 数据库是一个长期存储在计算机内的.有组织的.有共享的.统一管理的.数据集合.它是一个按数据结构来存储和管理数据的计算机软件系统.数据库包含两层含义:保管数据的仓库,以及数据管理的方法和技术 ...

  6. PC硬件之我见——CPU篇

    写在最前面:     最近身边很多朋友都购置电脑的想法,往往也会选择性价比较高的DIY攒机方式.不幸的是,并不是所有人都对电脑硬件有一定的了解的,何况在现在这种社会风气下,盲目的相信商家是不明智的.所 ...

  7. Linux下Java 编译运行说明

    命令行环境下Java编译运行 1. java的运行机制的基本概念: 源文件 也就是我们熟知的.java文件. 类文件  .class文件是编译器由.java文件编译而成.众所周知,Java的跨平台性在 ...

  8. Seven Steps to Success Machine Learning in Practice

    Seven Steps to Success Machine Learning in Practice Project failures in IT are all too common. The r ...

  9. jquery deferred

    http://www.ruanyifeng.com/blog/2011/08/a_detailed_explanation_of_jquery_deferred_object.html http:// ...

  10. gridview数据导出到word和excel以及excel的导入

    using System;using System.Collections.Generic;using System.Linq;using System.Web;using System.Web.UI ...