用布尔变量表示状态,把限制条件转化为XνY的形式以后跑2SAT,根据变量取值输出方案。

#include<bits/stdc++.h>
using namespace std;
const int maxn = 1e5+; #define PB push_back
bool vis[maxn*];
vector<int> G[maxn*];
int N,S[maxn*],top;
void initGraph(int n)
{
N = n*;
for(int i = ; i < N; i++) G[i].clear();
memset(vis,,sizeof(bool)*(n<<|));
} bool dfs(int x)
{
if(vis[x^]) return false;
if(vis[x]) return true;
vis[x] = true;
S[top++] = x;
for(int i = ; i < (int)G[x].size(); i++){
if(!dfs(G[x][i])) return false;
}
return true;
} bool twoSAT()
{
for(int i = ; i < N; i+=){
if(!vis[i]&&!vis[i^]){
top = ;
if(!dfs(i)){
while(top>) vis[S[--top]] = false;
if(!dfs(i^)) return false;
}
}
}
return true;
} void add_clause(int x,int xv,int y,int yv)
{
x = x<<|xv;
y = y<<|yv;
G[x^].PB(y);
G[y^].PB(x);
} int tp[maxn];
int age[maxn];
char bin[][] = {{'C','B'},{'C','A'}}; int main()
{
//freopen("in.txt","r",stdin);
int n,m;
while(scanf("%d%d",&n,&m),n){
int sum = ;
for(int i = ; i < n; i++){
scanf("%d",age+i);
sum += age[i];
}
for(int i = ; i < n; i++){
if(age[i]*n >= sum) tp[i] = ;
else tp[i] = ;
}
initGraph(n);
for(int i = ; i < m; i++){
int u,v; scanf("%d%d",&u,&v);
if(tp[--u]^tp[--v]){
add_clause(u,,v,);
}else {
add_clause(u,,v,);
add_clause(u,,v,);
}
}
if(twoSAT()){
for(int i = ; i < N; i+=){
putchar(bin[tp[i>>]][vis[i^]]);
putchar('\n');
}
}else {
puts("No solution.");
}
}
return ;
}

UVA Live 3713 Astronauts (2-SAT)的更多相关文章

  1. UVALive - 3713 - Astronauts(图论——2-SAT)

    Problem   UVALive - 3713 - Astronauts Time Limit: 3000 mSec Problem Description Input The input cont ...

  2. uva 10718 Bit Mask (位运算)

    uva 10718  Bit Mask  (位运算) Problem A Bit Mask Time Limit 1 Second In bit-wise expression, mask is a ...

  3. Uva 11729 Commando War (简单贪心)

    Uva 11729  Commando War (简单贪心) There is a war and it doesn't look very promising for your country. N ...

  4. UVa 1395 - Slim Span(最小生成树变形)

    链接: https://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&page=show_problem& ...

  5. UVa 580 - Critical Mass(递推)

    链接: https://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&page=show_problem& ...

  6. UVa 1218 - Perfect Service(树形DP)

    链接: https://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&page=show_problem& ...

  7. UVa 12186 - Another Crisis(树形DP)

    链接: https://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&page=show_problem& ...

  8. uva 253 - Cube painting(相同骰子)

    习题4-4 骰子涂色(Cube painting, UVa 253) 输入两个骰子,判断二者是否等价.每个骰子用6个字母表示,如图4-7所示. 图4-7 骰子涂色 例如rbgggr和rggbgr分别表 ...

  9. UVa 10480:Sabotage (最小割集)

    https://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&page=show_problem& ...

随机推荐

  1. mysql慢日志记录

    DBA工作:通过日志找到执行慢的sql语句 慢日志: - 执行时间 > 10 - 未命中索引 配置: - 基于内存 show variables like '%query%'; set glob ...

  2. eclipse neon 离线安装插件

    我的eclipse版本是4.6.3,以安装svn插件为例. 网上查到很多资料都是说找到eclipse目录下"plugins"和"features"文件夹,将下载 ...

  3. JavaScript之——对象Object(一)

    1. 新建对象.删除和访问: (1).新建 var obj1 = {b: 2}; //对象文本表示法 var obj2 = new Object(); obj2.a = 1; (2).访问 //第一种 ...

  4. C++中的声明与定义

    转自:http://www.cnblogs.com/kingcat/archive/2011/12/30/2307426.html C++是一种复杂的计算机语言,很多在现代高级语言中已经不需要程序员操 ...

  5. 16.join 用法(拼接列表时里面必须为str类型)

    s1='alex' s2='+'.join(s1) print(s2,type(s2))#a+l+e+x <class 'str'> l1=['小红','小刚','小明'] 前提:列表中的 ...

  6. echarts相关属性设置(3)环状图

    option = { grid: { left: '3%', top: '0%', // height: 500, right: '30%', containLabel: true, }, legen ...

  7. mysql8必知必会7 连接 内连接 外连接 交叉连接

    /* 2 需求:建立产品和分类表 1.查询每种分类的产品数量,没有产品的分类也要统计.(cname,quantity) 2.根据分类名称查询分类中的所有产品 */ -- --------------- ...

  8. Yac - PHP扩展

    1:首先你要安装Git [root@localhost]# git clone https://github.com/laruence/yac 2:进入yac目录进行配置 [root@localhos ...

  9. jmeter diff测试(调用JAR包处理)

    1.准备接口数据(对比字段,即json数据中需要提取的key对应的值进行对比) 2.配置获取EXCEL数据 3.新建线程,并建两个http请求,分别用于请求新旧接口 4.提取需要对比的内容 5.赋值变 ...

  10. scrapy的中间件Downloader Middleware实现User-Agent随机切换

    scrapy的中间件Download Middleware实现User-Agent随机切换   总架构理解Middleware 通过scrapy官网最新的架构图来理解: 从图中我们可以看出,在spid ...