给定n个宇航员的年龄,平均年龄为 ave,根据下列要求分配任务:

B任务只能分配给年龄<ave的宇航员;

A任务只能分配给年龄>=ave的宇航员;

C任务可以任意分配。

给定m组互相憎恨的宇航员,要求他们不能分配到同一个任务。能否存在这样的一组任务分配。

每个宇航员都只能分配两种任务中的一种:A或C(年龄大于等于ave),B或C(年龄小于ave),那么为每个宇航员设立一个变量xi,xi为0表示分配C任务,为1则分配A或B(根据年龄)。

对于互相仇恨的宇航员,如果属于同一类型,那么应满足xi∨xj,非xi∨非xj,表示xi和xj一真一假;如果类型不同只需要满足不同时分配C任务就可:xi∨xj。

 

直接贴代码好了:

#include <iostream>
#include <sstream>
#include <cstdio>
#include <climits>
#include <cstring>
#include <cstdlib>
#include <string>
#include <stack>
#include <map>
#include <cmath>
#include <vector>
#include <queue>
#include <algorithm>
#define esp 1e-6
#define pi acos(-1.0)
#define pb push_back
#define mp(a, b) make_pair((a), (b))
#define in freopen("in.txt", "r", stdin);
#define out freopen("out.txt", "w", stdout);
#define print(a) printf("%d\n",(a));
#define bug puts("********))))))");
#define stop system("pause");
#define Rep(i, c) for(__typeof(c.end()) i = c.begin(); i != c.end(); i++)
#define pragma comment(linker, "/STACK:102400000, 102400000")
#define inf 0x0f0f0f0f using namespace std;
typedef long long LL;
typedef vector<int> VI;
typedef pair<int, int> pii;
typedef vector<pii,int> VII;
typedef vector<int>:: iterator IT;
const int maxn = 111111;
int A[maxn], tag[maxn];
double sum;
int dblcmp(int x)
{
if(fabs(x) < esp)
return 1;
return x > 0 ? 1 : 0;
} struct TwoSAT
{
int mark[maxn*2], S[maxn*2], c, n;
VI g[maxn*2]; void init(int n)
{
this->n = n;
for(int i = 0; i < n*2; i++)
g[i].clear();
memset(mark, 0, sizeof(mark));
}
bool dfs(int x)
{
if(mark[x^1]) return false;
if(mark[x]) return true;
mark[x] = 1;
S[c++] = x;
for(int i = 0; i < g[x].size(); i++)
if(!dfs(g[x][i])) return false;
return true;
}
void add_clause(int x, int xval, int y, int yval)
{
x = x*2+xval;
y = y*2+yval;
g[x^1].pb(y);
g[y^1].pb(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) mark[S[--c]] = false;
if(!dfs(i+1)) return false;
}
}
return true;
}
void print_ans(void)
{
for(int i = 0; i < n; i++)
{
puts(mark[2*i] ? "C" : (tag[i] == 1 ? "A" : "B"));
}
}
} sat;
bool check(int x)
{
return dblcmp(A[x]-sum);
}
int main(void)
{
int n, m;
while(scanf("%d%d", &n, &m), n||m)
{
sum = 0;
for(int i = 0; i < n; i++)
scanf("%d", A+i), sum += A[i];
sum /= n;
sat.init(n);
for(int i = 0; i < n; i++)
tag[i] = check(i);
while(m--)
{
int u, v;
scanf("%d%d", &u, &v);
u--, v--;
if(tag[u] != tag[v])
{
sat.add_clause(u, 1, v, 1);
}
else
{
sat.add_clause(u, 1, v, 1);
sat.add_clause(u, 0, v, 0);
}
}
if(sat.solve())
sat.print_ans();
else puts("No solution.");
}
return 0;
}

UVALive - 3713 Astronauts的更多相关文章

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

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

  2. UVALive 3713 Astronauts (2-SAT,变形)

    题意: 有A,B,C三种任务,每个人必获得1个任务,大于等于平均年龄的可以选择A和C,小于平均年龄的可以选择B和C.这些人有一些是互相讨厌的,必须不能执行同任务,问能否安排他们工作?若行,输出任意一组 ...

  3. 训练指南 UVALive - 3713 (2-SAT)

    layout: post title: 训练指南 UVALive - 3713 (2-SAT) author: "luowentaoaa" catalog: true mathja ...

  4. 【UVALive - 3713】Astronauts (2-SAT)

    题意: 有n个宇航员,按照年龄划分,年龄低于平均年龄的是年轻宇航员,而年龄大于等于平均年龄的是老练的宇航员. 现在要分配他们去A,B,C三个空间站,其中A站只有老练的宇航员才能去,而B站是只有年轻的才 ...

  5. Astronauts UVALive - 3713(2-SAT)

    大白书例题 #include <iostream> #include <cstdio> #include <sstream> #include <cstrin ...

  6. UVA 3713 Astronauts

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

  7. 2-sat 分类讨论 UVALIVE 3713

    蓝书326 //看看会不会爆int!数组会不会少了一维! //取物问题一定要小心先手胜利的条件 #include <bits/stdc++.h> using namespace std; ...

  8. LA 3713 Astronauts

    给个题目链接: https://icpcarchive.ecs.baylor.edu/index.php?option=com_onlinejudge&Itemid=8&page=sh ...

  9. UVA Live 3713 Astronauts (2-SAT)

    用布尔变量表示状态,把限制条件转化为XνY的形式以后跑2SAT,根据变量取值输出方案. #include<bits/stdc++.h> using namespace std; ; #de ...

随机推荐

  1. xml约束DTD演示

    此演示xml和DTD在一个文件中 book.xml <?xml version="1.0" encoding="utf-8"?> <!DOCT ...

  2. C#学习笔记14:面向对象继承的特点和里氏转换

    面向对象: 继承:减少代码. 单根性  传递性 子类没有从父类那里继承了构造函数,只是会默认的调用父类那个无参数的构造函数 Class person { Public String Name { Ge ...

  3. SSRS和SSAS是支持VB的

    SSRS和SSAS是支持VB的,而且自定义Code其实也是只支持VB,或者其他语言可以编码成DLL再用咯.下面是官方VB函数库,基本上都能用,保存起来妥妥的. https://msdn.microso ...

  4. C# 调试程序弹出 没有可用于当前位置的源代码 对话框

    解决方案: 1.右键点击解决方案->属性->通用属性->调试源文件. 2.看看你的程序有没有被增加到“不查找这些源文件”这个框里. 3.如果有删除,然后重新编译即可调试,解决问题.

  5. (转)解读Flash矩阵

    转自: http://hi.baidu.com/cabtw/item/d2dbd212d4ae3e9398ce337f 图片看不到请去原网站看 Matrix: scale(a,d); 比例变换就是将平 ...

  6. WTL 中CComboBoxEx显示不了的问题

    在使用WTL的CComboBoxEx时,InsertItem之后,运行程序,ComboBox显不了问题,其原因如下: I guess you want to place combo box to di ...

  7. spring 中的<aop:advisor>和<aop:aspect>的区别

    在AOP中有几个概念: — 方面(Aspect):一个关注点的模块化,这个关注点实现可能另外横切多个对象.事务管理是J2EE应用中一个很好的横切关注点例子.方面用Spring的Advisor或拦截器实 ...

  8. Oracle procedure存储过程/function函数

    --函数的创建 create function func1(dno number) return NUMBER--必须带有返回值 is v_max number;--定义返回值 begin selec ...

  9. PHP常见算法-面试篇(2)

    1.顺序查找 思路分析: 从数组的第一个元素开始一个一个向下查找,如果有和目标一致的元素,查找成功:如果到最后一个元素仍没有目标元素,则查找失败. 代码实现: <?php function se ...

  10. ecshop用户中心订单详情增加快递单物流信息查询显示的功能

    1,themes\default\user_transaction.dwt 找到: <!--{if $action eq order_detail} --> 在下面一行加入: <st ...