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& ...
随机推荐
- mosquitto.conf之log配置
# ================================================================= # Logging # 日志信息 # ============= ...
- sql之视图、触发器、函数、存储过程、事务
视图 # 视图也是一张表,但在data文件里只有表结构,没有表数据 # 不建议使用,扩展性差,程序需改变时,依赖的视图也要改变 # 视图牵涉到多张表时,视图中的记录不能修改. create view ...
- Java代码加密与反编译(一):利用混淆器工具proGuard对jar包加密
Java 代码编译后生成的 .class 中包含有源代码中的所有信息(不包括注释),尤其是在其中保存有调试信息的时候.所以一个按照正常方式编译的 Java .class 文件可以非常轻易地被反编译.通 ...
- 1004 Counting Leaves (30 分)
A family hierarchy is usually presented by a pedigree tree. Your job is to count those family member ...
- HDU2594 【KMP】
题意: 给两个字符串s1,s2,求最长的s1前缀匹配s2后缀的字符串,以及长度 思路: 利用KMP看下最终匹配到了哪个位置:一个是利用常规匹配,另一个是利用next数组的跳转. #include< ...
- HDU 3729【二分匹配】
题意: 给出n个同学的排名,代表每个排名在哪个区间,要求保证最多人说的是实话,并在此前提下求一个说真话人最大字典序. 思路: 最后感觉就是点去填区间,点和区间建个边,然后跑个二分图,然后sort一发. ...
- vim 快速定位到文件末尾、头部
gg : 跳转到文件头 Shift+g : 跳转到文件末尾 行数+gg : 跳转到指定行,例跳转到123行:123gg
- Java 分析工具汇总
http://blog.csdn.net/fenglibing/article/details/6411999 jps jps -mlvV jmap jmap -heap <pid> ...
- typescript学习笔记(三)---接口
关于第二章的学习笔记是变量声明. 接口:TypeScript的核心原则之一是对值所具有的结构进行类型检查. 它有时被称做“鸭式辨型法”或“结构性子类型化”. 在TypeScript里,接口的作用就是为 ...
- 分布式通信-tcp/ip 单播
服务端 public class SingleBroadCastSocketServer { public static void main(String[] args) { ServerSocket ...