UVA Live 3713 Astronauts (2-SAT)
用布尔变量表示状态,把限制条件转化为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)的更多相关文章
- UVALive - 3713 - Astronauts(图论——2-SAT)
Problem UVALive - 3713 - Astronauts Time Limit: 3000 mSec Problem Description Input The input cont ...
- uva 10718 Bit Mask (位运算)
uva 10718 Bit Mask (位运算) Problem A Bit Mask Time Limit 1 Second In bit-wise expression, mask is a ...
- Uva 11729 Commando War (简单贪心)
Uva 11729 Commando War (简单贪心) There is a war and it doesn't look very promising for your country. N ...
- UVa 1395 - Slim Span(最小生成树变形)
链接: https://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&page=show_problem& ...
- UVa 580 - Critical Mass(递推)
链接: https://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&page=show_problem& ...
- UVa 1218 - Perfect Service(树形DP)
链接: https://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&page=show_problem& ...
- UVa 12186 - Another Crisis(树形DP)
链接: https://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&page=show_problem& ...
- uva 253 - Cube painting(相同骰子)
习题4-4 骰子涂色(Cube painting, UVa 253) 输入两个骰子,判断二者是否等价.每个骰子用6个字母表示,如图4-7所示. 图4-7 骰子涂色 例如rbgggr和rggbgr分别表 ...
- UVa 10480:Sabotage (最小割集)
https://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&page=show_problem& ...
随机推荐
- CodeForces - 357C Knight Tournament 伪并查集(区间合并)
Knight Tournament Hooray! Berl II, the king of Berland is making a knight tournament. The king has a ...
- eclipse + tomcat 开发环境配置
一. 下载tomcat和Eclipse 下载tomcat 下载地址:http://tomcat.apache.org/download-70.cgi 下载后解压如下图 下载eclipse 下载地址:h ...
- GIL 已经被杀死了么?
GIL 已经被杀死了么? 本文原创并首发于公众号[Python猫],未经授权,请勿转载. 原文地址:https://mp.weixin.qq.com/s/8KvQemz0SWq2hw-2aBPv2Q ...
- SCUT - 114 - 作业之数学篇 - 杜教筛
https://scut.online/p/114 \(A(n)=\sum\limits_{i=1}^{n} \frac{lcm(i,n)}{gcd(i,n)}\) \(=\sum\limits_{i ...
- ThinkPHP3.2.3学习笔记5---模板(一)
一.模板简介 此文中的学习笔记部分资料是来自于thinkphp官方文档,http://document.thinkphp.cn/manual_3_2.html#template 本章的内容主要讲述了如 ...
- Telnet 命令格式
Telnet host 端口 如:Telnet 127.0.0.1 11211 执行命令进入后 ctr +] ,打开回显,并回车即可
- MySQL索引的学习
MySQL索引的学习 关于使用mysql索引的好处,合理的设计并使用mysql索引能够有效地提高查询效率.对于没有索引的表,单表查询可能几十万数据就是平静,在大型网站单日可能会产生几十万甚至几百万的数 ...
- 用C#编写计算器
零有点问题,而且目前只能做一些简单的运算,+.-.*./.平方.开根号 希望有大佬指正我的错误 感谢 using System;using System.Collections.Generic;usi ...
- PHP保留小数的相关方法
结合一下网上的例子 $num = 10.4567; //第一种:利用round()对浮点数进行四舍五入 但是这个如果没有两位小数也不会"两位精度" echo round($num, ...
- Pycharm2018.3.1永久激活
Pycharm Professional 2018.3.1 版已正式发布,新版本添加对Python3.7的支持.作为强大的开发工具,但每次注册让人头疼,本着分享的心态,提供以下解决方案,亲测有效!本方 ...