给定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. this 函数内部属性

    前言:在javascript中我们会经常碰到this,然后this经常出现在function方法里面,有时候可能因为代码很多,无法判断this指向的是谁,其实很简单,一句话总结:谁点出这个this,这 ...

  2. myeclipse的新建severlet不见解决方法

    点击myeclipse中的window菜单里面选择myeclipse java Enterprise 选项就可以恢复到默认.

  3. TextView实现跑马灯效果

    网上有很多跑马灯的介绍,有很多跑马灯的代码.或许我的不是最好的,但是应该很容易明白的. 我们先来介绍一个跑马灯的代码 <LinearLayout xmlns:android="http ...

  4. (poj)1064 Cable master 二分+精度

    题目链接:http://poj.org/problem?id=1064 Description Inhabitants of the Wonderland have decided to hold a ...

  5. C++里的int 和string类型相互转换

    C++不像Java和C#一样在进行数据类型转换时直接调用一些类方法就可以了,使用起来很简单. 一个很简单的例子就是string str=“D:\\”+1+“.txt”;这在Java或者C#里面是可以自 ...

  6. 九度OJ 1104 整除问题

    题目地址:http://ac.jobdu.com/problem.php?pid=1104 题目描述: 给定n,a求最大的k,使n!可以被a^k整除但不能被a^(k+1)整除. 输入: 两个整数n(2 ...

  7. PHP页面间参数传递的四种方法详解

    2016-04-16 定义page01.php和page02.php两个php文件,将page01中的内容想办法传递到page02,然后供我们继续使用.------------------------ ...

  8. Sublime Text 3插件安装方法

    安装Sublime Tex 3t插件的方法: 按快捷键Ctrl + ~ 调出console 粘贴以下代码到console并回车: import urllib.request,os; pf = 'Pac ...

  9. Java Web开发之详解JSP

    JSP作为Java Web开发中比较重要的技术,一般当作视图(View)的技术所使用,即用来展现页面.Servlet由于其本身不适合作为表现层技术,所以一般被当作控制器(Controller)所使用, ...

  10. [转] Js获取 本周、本月、本季度、本年、上月、上周、上季度、去年时间段

    /** * 针对时间的工具类 */ var DateTimeUtil = function () { /*** * 获得当前时间 */ this.getCurrentDate = function ( ...