uva1391 2-SAT 问题
题意在大白书上。
有3 种工作 abc 大于等于平均年龄的可以去做a c 工作, 小于平均年龄的可以去做 bc , 同样转化为2 -sat 去做, 因为对于每个人也只有2 种情况可以作为选择
#include <iostream>
#include <algorithm>
#include <string.h>
#include <cstdio>
#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 inti(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 n,m,total_age;
int age[maxn];
bool isyoung(int x){
return x*n<total_age;
}
int main()
{
while(scanf("%d%d",&n,&m)==&&n){
total_age=; for(int i=; i<n; ++i){
scanf("%d",&age[i]);
total_age+=age[i];
} solver.inti(n);
for(int i=; i<m; ++i){
int u,v;
scanf("%d%d",&u,&v); u--; v--;
if(u==v)continue;
solver.add_clause(u, , v , );
if(isyoung(age[u])==isyoung(age[v])){
solver.add_clause( u, , v , );
}
}
if(!solver.solve()) printf("No solution.\n");
else { for(int i=; i<n; i++)
if(solver.mark[i*]) printf("C\n");
else if(isyoung(age[i])) printf("B\n");
else printf("A\n");
}
} return ;
}
uva1391 2-SAT 问题的更多相关文章
- 多边形碰撞 -- SAT方法
检测凸多边形碰撞的一种简单的方法是SAT(Separating Axis Theorem),即分离轴定理. 原理:将多边形投影到一条向量上,看这两个多边形的投影是否重叠.如果不重叠,则认为这两个多边形 ...
- POJ 3678 Katu Puzzle(2 - SAT) - from lanshui_Yang
Description Katu Puzzle is presented as a directed graph G(V, E) with each edge e(a, b) labeled by a ...
- Map Labeler POJ - 2296(2 - sat 具体关系建边)
题意: 给出n个点 让求这n个点所能建成的正方形的最大边长,要求不覆盖,且这n个点在正方形上或下边的中点位置 解析: 当然是二分,但建图就有点还行..比较难想..行吧...我太垃圾... 2 - s ...
- 学习笔记(two sat)
关于two sat算法 两篇很好的论文由对称性解2-SAT问题(伍昱), 赵爽 2-sat解法浅析(pdf). 一些题目的题解 poj 3207 poj 3678 poj 3683 poj 3648 ...
- LA 3211 飞机调度(2—SAT)
https://vjudge.net/problem/UVALive-3211 题意: 有n架飞机需要着陆,每架飞机都可以选择“早着陆”和“晚着陆”两种方式之一,且必须选择一种,第i架飞机的早着陆时间 ...
- HIT 1917 2—SAT
题目大意:一国有n个党派,每个党派在议会中都有2个代表, 现要组建和平委员会,要从每个党派在议会的代表中选出1人,一共n人组成和平委员会. 已知有一些代表之间存在仇恨,也就是说他们不能同时被选为和平委 ...
- 2 - sat 模板(自用)
2-sat一个变量两种状态符合条件的状态建边找强连通,两两成立1 - n 为第一状态(n + 1) - (n + n) 为第二状态 例题模板 链接一 POJ 3207 Ikki's Story IV ...
- UVA1391/LA3713 Astronauts
题意:有A.B.C3个任务分配给n个宇航员,其中每个宇航员恰好分配一个任务.假设n个宇航员的平均年龄为x,只有年龄大于x的才能领取A任务:只有年龄严格小于x的才能领取B任务,而任务C没有限制.有m对宇 ...
- SAT考试里最难的数学题? · 三只猫的温暖
问题 今天无意中在Quora上看到有人贴出来一道号称是SAT里最难的一道数学题,一下子勾起了我的兴趣.于是拿起笔来写写画画,花了差不多十五分钟搞定.觉得有点意思,决定把解题过程记下来.原帖的图太小,我 ...
随机推荐
- weblogic11g重置控制密码
Reset the AdminServer Password in WebLogic 11g and 12c If you forget the AdminServer password for yo ...
- easyui 时间定格为 时分
$.fn.datetimebox.defaults.formatter = function (date) { console.log('dt formatting ' + date); if (!( ...
- On iPad, UIImagePickerController must be presented via UIPopoverController
本文转载至:http://blog.csdn.net/k12104/article/details/8537695 On iPad, UIImagePickerController must be p ...
- HTML5文件拖拽上传记录
JS文件: var FileName = ""; var FileStr = ""; (function () { function $id(id) { ret ...
- linux 下配置vncserver
vncserver是使用非常方便和广泛的远程桌面服务,配置也相对简单. 下面记录了在centos系统上进行配置vncserver的过程. 安装 查看centos下是否已经安装了vncserver rp ...
- Android 获取系统默认输入法
import android.provider.Settings; import android.text.TextUtils; 获取默认输入法包名: private String getDefaul ...
- Java初学者笔记三:关于字符串和自实现数组常见操作以及异常处理
一.字符串常见的操作 1.结合我在python中对于字符串常见处理总结出下面三个应该能算是字符串造作的TOP 3 //字符串查找子串,存在返回index,不存在返回-1 等于python的find: ...
- Android动态加载ListView中的Item
我这周上网看到动态增加listview的每一项item的布局,今天抽空自己写了一个,方便自己日后使用,这个效果还是很不错的,用到了Adapter的notifyDataSetChanged()方法,当点 ...
- onethink判断是否是手机访问?
第一步:找到:Application / Common / Common / function.php 添加判断是否是手机访问的代码: /** * 判断当前访问的用户是 PC端 还是 手机端 返回tr ...
- 170720、springboot编程之properties文件讲解
但是在实际开发过程中有更复杂的需求,我们在对properties进一步的升华.在本篇博客中您将会学到如下知识(这节中有对之前的知识的温故,对之前的升华): (1) 在application.prope ...