【LA3713 训练指南】宇航员分组 【2-sat】
题意
有A,B,C三个任务要分配给n个宇航员,其中每个宇航员恰好要分配一个任务。设所有n个宇航员的平均年龄为x,只有年龄大于或等于x的宇航员才能分配任务A;只有年龄严格小于x的宇航员才能分配任务B,而任务C没有限制。有m对宇航员相互讨厌,因此不能分配到同一任务。编程找出一个满足上述所有要求的任务分配方案。
分析
这个题应该算是比较裸的2-sat了。
对于每个宇航员来说,他的年龄要么大于x要么小于x,所有他只能从A或者B里面选择一个。因此每个宇航员可以选择的任务只有两个A或者B 和C,对应2-sat问题中每个布尔型变量的真和假。有m对宇航员相互讨厌对应2-sat问题中的m条限制。如果两个宇航员年龄都大于x或者都小于x,我们称这两个宇航员为同一种类型。那么xi和xj必须不相同。则用两个条件进行限制。 “xi为真或者xj为真”,“xi为假或者xj为假”。如果两个宇航员为不同类型,那么只需要一个限制“xi为真或者xj为真”。
下面是代码
#include <cstdio>
#include <algorithm>
#include <cstring>
#include <iostream>
#include <vector> using namespace std;
const int maxn=+;
struct TwoSAT{
int n;
vector<int>G[*maxn];
bool mark[maxn*];
int S[maxn*],c;
bool dfs(int x){
if(mark[x^])return false;
if(mark[x])return true;
mark[x]=true;
S[c++]=x;
for(int i=;i<G[x].size();i++){
if(!dfs(G[x][i]))return false;
}
return true;
}
void init(int n){
this->n=n;
for(int i=;i<n*;i++)G[i].clear();
memset(mark,,sizeof(mark));
}
void add_clause(int x,int xval,int y,int yval){
x=x*+xval;
y=y*+yval;
G[x^].push_back(y);
G[y^].push_back(x);
} bool solve(){
for(int i=;i<n*;i+=){
if(!mark[i]&&!mark[i+]){
c=;
if(!dfs(i)){
while(c>)mark[S[--c]]=false;
if(!dfs(i+))return false;
}
}
}
return true;
}
}solver;
int age[maxn],n,m,X;
int kase;
int main(){
kase=;
while(scanf("%d%d",&n,&m)!=EOF&&(n||m)){
if(kase)printf("\n");
++kase;
X=;
solver.init(n);
for(int i=;i<n;i++){
scanf("%d",&age[i]);
X+=age[i];
}
int x,y;
for(int i=;i<=m;i++){
scanf("%d%d",&x,&y);
x--,y--;
if(age[x]*n>=X&&age[y]*n>=X){
solver.add_clause(x,,y,);
solver.add_clause(x,,y,);
}
if(age[x]*n<X&&age[y]*n<X){
solver.add_clause(x,,y,);
solver.add_clause(x,,y,);
}
else{
solver.add_clause(x,,y,);
}
}
if(!solver.solve()){
printf("No solution.");
continue;
}
for(int i=;i<n;i++){
if(solver.mark[*i+]){
if(age[i]*n>=X){
printf("A\n");
}else{
printf("B\n");
}
}else{
printf("C\n");
}
}
}
return ;
}
【LA3713 训练指南】宇航员分组 【2-sat】的更多相关文章
- 【UVA11107 训练指南】Life Forms【后缀数组】
题意 输入n(n<=100)个字符串,每个字符串长度<=1000,你的任务是找出一个最长的字符串使得超过一半的字符串都包含这个字符串. 分析 训练指南上后缀数组的一道例题,据说很经典(估计 ...
- 训练指南 UVALive - 3713 (2-SAT)
layout: post title: 训练指南 UVALive - 3713 (2-SAT) author: "luowentaoaa" catalog: true mathja ...
- poj 1961 Period(KMP训练指南例题)
Period Time Limit: 3000MS Memory Limit: 30000K Total Submissions: 11356 Accepted: 5279 Descripti ...
- 算法竞赛入门经典训练指南——UVA 11300 preading the Wealth
A Communist regime is trying to redistribute wealth in a village. They have have decided to sit ever ...
- [置顶] 刘汝佳《训练指南》动态规划::Beginner (25题)解题报告汇总
本文出自 http://blog.csdn.net/shuangde800 刘汝佳<算法竞赛入门经典-训练指南>的动态规划部分的习题Beginner 打开 这个专题一共有25题,刷完 ...
- 【LA3523 训练指南】圆桌骑士 【双连通分量】
题意 有n个骑士经常举行圆桌会议,商讨大事.每次圆桌会议至少应有3个骑士参加,且相互憎恨的骑士不能坐在圆桌旁的相邻位置.如果发生意见分歧,则需要举手表决,因此参加会议的骑士数目必须是奇数,以防赞同和反 ...
- 训练指南 UVALive - 3126(DAG最小路径覆盖)
layout: post title: 训练指南 UVALive - 3126(DAG最小路径覆盖) author: "luowentaoaa" catalog: true mat ...
- 训练指南 UVALive - 3415(最大点独立集)
layout: post title: 训练指南 UVALive - 3415(最大点独立集) author: "luowentaoaa" catalog: true mathja ...
- 训练指南 UVA - 11419(二分图最小覆盖数)
layout: post title: 训练指南 UVA - 11419(二分图最小覆盖数) author: "luowentaoaa" catalog: true mathjax ...
随机推荐
- docker swarm mode routing mesh 使用
Docker Engine swarm mode makes it easy to publish ports for services to make them available to resou ...
- ubuntu12.04安装KDevelop
1, sudo apt-get update 2, sudo apt-get install kdevelop
- 关于tab的切换之共用html页面,而引发的页面跳转错乱问题
在一个项目中的同一个模块中,有多个tab(并且多个tab对应的页面结构完全一样),tab的每次切换,不同tab调用不同的接口,利用一个switch进行判断,根据当前的类型去调用不同的接口,返回不同数据 ...
- Unit01: Servlet基础 、 HTTP协议
Unit01: Servlet基础 . HTTP协议 在页面上输出当前时间 package web; import java.io.IOException; import java.io.PrintW ...
- Ubuntu---samba(安装、配置、使用)OK
安装 [xt@butbueatiful ~]$ sudo apt-get install samba 创建共享目录 [xt@butbueatiful ~]$ mkdir /home/xt/share ...
- python接口自动化19-requests-toolbelt处理multipart/form-data
requests-toolbelt 1.官方文档地址:requests-toolbelt官方文档 2.环境安装 pip install requests-toolbelt multipart/form ...
- mysql分区表之二:MySQL的表的四种分区类型介绍
一.什么是表分区 通俗地讲表分区是将一大表,根据条件分割成若干个小表.mysql5.1开始支持数据表分区了.如:某用户表的记录超过了600万条,那么就可以根据入库日期将表分区,也可以根据所在地将表分区 ...
- POJ 2674 Linear world(弹性碰撞)
Linear world Time Limit: 3000MS Memory Limit: 65536K Total Submissions: 4426 Accepted: 1006 Desc ...
- 在VritualBox中安装CentOS7
系统:Windows10 位 详细步骤参考: Windows平台上通过VirtualBox安装centos虚拟机 安装virtual box 出现2503错误解决:c:/windows/temp 添加 ...
- Android apk couldn't install
an existing package with the same name and signature is already installed