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& ...
随机推荐
- Asset Catalog Help (三)---Adding Image Sets
Adding Image Sets Organize versions of your images in image sets, which you can add to an asset cata ...
- 3.7-3.10 Hive 企业使用优化1
一.Fetch Task 在执行hive代码的时候,一条简单的命令大部分都会转换成为mr代码在后台执行, 但是有时候我们仅仅只是想获取一部分数据而已,仅仅是获取数据,还需要转化成为mr去执行吗? 那个 ...
- 使用git命令将本地项目上传到GIthub
1.(先进入项目文件夹)通过命令 git init 把这个目录变成git可以管理的仓库 2.把文件添加到版本库中,使用命令 git add .添加到暂存区里面去,不要忘记后面的小数点“.”,意为添加文 ...
- Angular6之ng build | ng build --aot | ng build --prod 差异
由于写了大半年的项目终于要告一段落并且即将进行第二阶段优化开发,emmm 基础版本已经二十多个模块了,必不可少的优化是很重要的,尽管项目上使用多层嵌套懒加载,但是在首屏加载的时候,任然很慢啊,因为一直 ...
- java保留小数位数
System.out.println(String.format("%.5f",new Main().minRadius(n,m)));
- HDU3433 【时间的二分+DP最优解】
题意: 有n个人,X个任务A,Y个任务B, 给出每个人做A做B的时间,一个人只能在某个时刻做一个工作, 问最短时间完成所有工作. 思路: 二分n个人用的时间,判断最优条件用DP. 可以二分就是因为时间 ...
- Codeforces712C【贪心】
看了这篇.. http://blog.csdn.net/queuelovestack/article/details/52503162 直接就是从小到大,那么每次按最大的递增顺序上去,就是了. 因为每 ...
- Unity3D命令行Build
转自:http://www.cnblogs.com/gameprogram/archive/2012/05/11/2496303.html 本来是没想用这个命令行Build方式,可惜电脑不知道怎么的就 ...
- 笔记-JavaWeb学习之旅10
Servlet server applet运行在服务器端的小程序,servlet就是一个接口,定义了Java类被浏览器访问到的规则(Java类重写这个接口,就可以被浏览器(tomcat)识别) Ser ...
- [題解](DP)CF713C_Sonya and Problem Wihtout a Legend
對於不嚴格單調的我們可以n^2DP,首先每個數一定在原數組中出現過,如果沒出現過不如減小到出現過的那個花費更小,效果相同 所以f[i][j]表示把i改到離散化后j的最小代價,每次維護前一狀態最小值mn ...