给定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的语法与创建

    xml语法很简单,但很严格,如果出现错误则不能正常解析,而HTML如果出现局部的错误,照样解析 xml第一行必须写xml头<?xml version='1.0' encoding='utf8'? ...

  2. 理解Java的引用对象

    SoftReferenceWeakReference 的特性基本一致, 最大的区别在于 SoftReference会尽可能长的保留引用,不会在GC时就回收对象,而是直到JVM 内存不足时才会被回收(虚 ...

  3. Sublime Text2上搭建C/C++环境

    环境:Sublime Text2            win7 64位 1.下载Sublime Text2并安装     下载地址:http://www.sublimetext.com/ 2.需要用 ...

  4. ###《Effective STL》--Chapter7

    点击查看Evernote原文. #@author: gr #@date: 2014-08-31 #@email: forgerui@gmail.com Chapter7 在程序中使用STL Topic ...

  5. ajax跨域请求的解决方案

    一直打算改造一下自己传统做网站的形式. 我是.Net程序员,含辛茹苦数年也没混出个什么名堂. 最近微信比较火, 由于现在大环境的影响和以前工作的总结和经验,我打算自己写一个数据,UI松耦合的比较新潮的 ...

  6. 鼠标点击input时,placeholder中的提示信息消失

    html代码: <input type="text" placeholder="多个关键词空格隔开"> 鼠标点击input时,placeholder ...

  7. Java实战之03Spring-04Spring的数据库访问

    四.Spring的数据库访问 1.DAO模式 /** * 抽取的一个类 * @author zhy * */ public class JdbcDaoSupport { private QueryRu ...

  8. 07_Java8新增的Lambda表达式

    [Lambda表达式概述] Lambda表达式支持将代码块作为方法参数,Lambda表达式允许将使用简洁的代码来创建只有一个抽象方法的接口的实例.(这种接口称为函数式接口) [入门实例] packag ...

  9. [PR & ML 4] [Introduction] Model Selection & The Curse of Dimension

    这两部分内容比较少,都是直觉上的例子和非正式的定义,当然这本书中绝大多数定义都是非正式的,但方便理解.后面深入之后会对这两个章节有详细的阐述.

  10. 暑假集训(3)第四弹 -----Frogger(Poj2253)

    题意梗概:青蛙王子最近喜欢上了另一只经常坐在荷叶上的青蛙公主.不过这件事不小心走漏了风声,被某fff团团员知 道了,在青蛙王子准备倾述心意的那一天,fff团团员向湖泊中注入大量的充满诅咒力量的溶液.这 ...