【UVALive - 3713】Astronauts (2-SAT)
题意:
有n个宇航员,按照年龄划分,年龄低于平均年龄的是年轻宇航员,而年龄大于等于平均年龄的是老练的宇航员。
现在要分配他们去A,B,C三个空间站,其中A站只有老练的宇航员才能去,而B站是只有年轻的才能去,C站都可以去。
有m对宇航员相互讨厌,不能让他们在同一个空间站工作。
输出每个宇航员应分配到哪个空间站,如果没有则输出No solution.
分析:
对于每个宇航员,有两种选择,(A,B)或C。第一个选择中取A还是取B取决于年龄。
构图,2-SAT找满足题意的方案再输出即可。
代码如下:
#include<cstdio>
#include<cstdlib>
#include<cstring>
#define Maxn 100010
#define Maxm 100010 int n,m;
int age[Maxn],first[*Maxn],mark[*Maxn],s[*Maxn];
int c,v; struct node
{
int x,y,next;
}t[*Maxm];int len; void ins(int x,int y)
{
t[++len].x=x;t[len].y=y;
t[len].next=first[x];first[x]=len;
} bool dfs(int x)
{
if(mark[x^]) return ;
if(mark[x]) return ;
mark[x]=;
s[++c]=x;
for(int i=first[x];i;i=t[i].next)
if(!dfs(t[i].y)) return ;
return ;
} bool solve()
{
memset(mark,,sizeof(mark));
for(int i=;i<n;i++)
if(!mark[*i]&&!mark[*i+])
{
c=;
if(!dfs(*i))
{
while(c>) mark[s[c--]]=;
if(!dfs(*i+)) return ;
}
}
return ;
} void output()
{
for(int i=;i<n;i++)
if(mark[i*+]) printf("C\n");
else if(age[i]*n>=v) printf("A\n");
else printf("B\n");
} int main()
{
while()
{ scanf("%d%d",&n,&m);
len=;v=;
if(n==&&m==) break;
memset(first,,sizeof(first));
for(int i=;i<n;i++) scanf("%d",&age[i]),v+=age[i];
for(int i=;i<=m;i++)
{
int x,y;
scanf("%d%d",&x,&y);x--;y--;
ins(x*+,y*);ins(y*+,x*);
if(!((age[x]*n>=v)^(age[y]*n>=v))) ins(x*,y*+),ins(y*,x*+);
}
if(!solve()) printf("No solution.\n");
else output();
}
return ;
}
[LA3713]
2016-03-18 13:19:17
【UVALive - 3713】Astronauts (2-SAT)的更多相关文章
- 【 UVALive - 5095】Transportation(费用流)
Description There are N cities, and M directed roads connecting them. Now you want to transport K un ...
- 【LOJ#6060】Set(线性基)
[LOJ#6060]Set(线性基) 题面 LOJ 题解 好题啊QwQ. 首先\(x1\oplus x2=s\)是定值.而\(s\)中假设某一位上是\(1\),则\(x1,x2\)上必定有一个是\(1 ...
- 【LOJ#6073】距离(主席树)
[LOJ#6073]距离(主席树) 题面 LOJ 题解 两点间的距离是\(dep[x]+dep[y]-2dep[LCA]\). 那么题目要求的东西拆开维护,唯一不好做的就是\(2dep[LCA]\). ...
- 【LOJ#6029】市场(线段树)
[LOJ#6029]市场(线段树) 题面 LOJ 题解 看着就是一个需要势能分析的线段树. 不难发现就是把第二个整除操作化为减法. 考虑一下什么时候整除操作才能变成减法. 假设两个数为\(a,b\). ...
- 【BZOJ3817/UOJ42】Sum(类欧)
[BZOJ3817/UOJ42]Sum(类欧) 题面 BZOJ UOJ 题解 令\(x=\sqrt r\),那么要求的式子是\[\sum_{d=1}^n(-1)^{[dx]}\] 不难发现,对于每个\ ...
- 【UOJ#188】Sanrd(min_25筛)
[UOJ#188]Sanrd(min_25筛) 题面 UOJ 题解 今天菊开讲的题目.(千古神犇陈菊开,扑通扑通跪下来) 题目要求的就是所有数的次大质因子的和. 这个部分和\(min\_25\)筛中枚 ...
- 【POJ 1201】 Intervals(差分约束系统)
[POJ 1201] Intervals(差分约束系统) 11 1716的升级版 把原本固定的边权改为不固定. Intervals Time Limit: 2000MS Memory Limit: ...
- 【LOJ#573】【LNR#2】单枪匹马(线段树)
[LOJ#573][LNR#2]单枪匹马(线段树) 题面 LOJ 题解 考虑拿线段树维护这个值,现在的问题就是左右怎么合并,那么就假设最右侧进来的那个分数是\(\frac{x}{y}\)的形式,那么就 ...
- 【UVALive - 3487】 Duopoly(网络流-最小割)
Description The mobile network market in country XYZ used to be dominated by two large corporations, ...
随机推荐
- hadoop错误INFO util.NativeCodeLoader - Unable to load native-hadoop library for your platform... using builtin-java classes where applicable
报如下错误: 解决方法: 1.增加调试信息 在HADOOP_HOME/etc/hadoop/hadoop-env.sh文件中添加如下信息 2.再执行一次操作,看看报什么错误 上面信息显示,需要2.14 ...
- Load Balance Tomcat with Nginx and Store Sessions in Redis--reference
An awkward title, but that’s exactly what we’re going to do. For some time, I was looking for a way ...
- 15个Linux Yum命令实例--安装/卸载/更新
在linux环境中, 安装, 卸载更新包是一个极为常见的操作.大部分的linux环境都提供了包的管理工具, 例如:apt-get, dpkg, rpm, yum等等. 一些Linux环境中,yum是默 ...
- PHP 正则通配符
$a = preg_match('/ph+p/','aaaphpbbbp'); +的前导就是h $a = preg_match('/ph+p/','aaaphhhhhhhhhhpbbbp'); //第 ...
- C# 实现的多线程异步Socket数据包接收器框架
转载自Csdn : http://blog.csdn.net/jubao_liang/article/details/4005438 几天前在博问中看到一个C# Socket问题,就想到笔者2004年 ...
- myeclipse10 中修改html,servlet,jsp等的生成模板
1.进入myeclipse的安装目录 2.用减压软件,(如winrar)打开common\plugins\com.genuitec.eclipse.wizards_9.0.0.me2011080913 ...
- window.event对象详细介绍
1.event代表事件的状态,例如触发event对象的元素.鼠标的位置及状态.按下的键等等.event对象只在事件发生的过程中才有效.event的某些属性只对特定的事件有意义.比如,fromEleme ...
- iOS Core Animation学习总结(1)--CALayer常用属性
图层是core animation的基础, UIView之所以能显示在屏幕上,靠的是其内部的这个图层,即每个UIView 都有 CALayer,可通过UIView.layer或者[UIView lay ...
- ios专题 - Scrum
什么是Scrum? Scrum是一个敏捷开发框架,是一个增量的.迭代的开发过程.在这个框架中,整个开发过程由若干个短的迭代周期组成,一个短的迭代周期称为一个 Sprint,每个Sprint的建议长度是 ...
- 生产者与消费者(三)---BlockingQueue
前面阐述了实现生产者与消费者问题的两种方式:wait() / notify()方法 和 await() / signal()方法,本文继续阐述多线程的经典问题---生产者与消费者的第三种方式:Bloc ...