给定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. 1.1、Mybatis一级缓存测试

    package me.gacl.test; import me.gacl.domain.User; import me.gacl.util.MyBatisUtil; import org.apache ...

  2. 最新版spark1.1.0集群安装配置

    和分布式文件系统和NoSQL数据库相比而言,spark集群的安装配置还算是比较简单的: 很多教程提到要安装java和scala,但我发现spark最新版本是包含scala的,JRE采用linux内嵌的 ...

  3. iOS 9 适配需要注意的问题

    iOS 9 适配需要注意的问题 1`网络适配_改用更安全的HTTPS iOS9把所有的http请求都改为https了:iOS9系统发送的网络请求将统一使用TLS 1.2 SSL.采用TLS 1.2 协 ...

  4. mysql学习笔记6——用phpmyadmin和在腾讯微云中创建数据库

    安装phpmyadmin就不多说了,对于新手,推荐使用wamp(windows系统),傻瓜式安装,很好用.安装完后在浏览器栏输入localhost

  5. POJ 1080 Human Gene Functions -- 动态规划(最长公共子序列)

    题目地址:http://poj.org/problem?id=1080 Description It is well known that a human gene can be considered ...

  6. html元素li移动动态效果

    在日常工作当中遇到了一个问题,平铺型列表修改单个内容设置排序时列表排序应与之对应.一下是一个小小的例子:简单的解决了此类问题,以浮动的形式改变当前的数据的显示顺序.有不足之处欢迎指点,后期还会做一个更 ...

  7. 排序算法THREE:归并排序MergeSort

    /** *归并排序思路:分治法思想 O(nlogn) * 把数组一分为二,二分为四 * 四和为二,二和为一 * */ /** * 归并排序主方法 *@params 待排序的数组 *@params 初始 ...

  8. Node安装与环境配置

    1.nodejs(npm)安装 下载nodejs(http://nodejs.cn/)安装后,cmd下如输入 node -v  与  npm -v 出现下图版本提示就是完成了NodeJS的安装 2.n ...

  9. javascript学习笔记4

    1. 分析一下代码执行结果 分析为什么? var  a = 12;  b = 34; c = 56; ++a;    //a结果 13 a++;    //a结果 14 c = ++a + b;   ...

  10. Rsync+Inotify-tools实现数据实时同步

    inotify是一种强大的,细粒度的,异步文件系统时间监控机制,它可以替代crond实现与rsync的触发式文件同步,从而监控文件系统中添加,删除,修改,移动等细粒事件,从LINUX 2.6.13起, ...