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& ...
随机推荐
- 1.22-1.24 Oozie企业使用案例
一.将hive的表数据用sqoop抽取到mysql 1.编写oozie workflow和Coordinator ## [root@hadoop-senior oozie-apps]# pwd /op ...
- ASP.NET Core会议管理平台实战_汇总贴
ASP.NET Core会议管理平台实战 课程地址:https://ke.qq.com/course/389673?from=800004097#term_id=100464670 ASP.NET C ...
- [CVE-2017-8464]Microsoft Windows远程命令执行漏洞复现
版权声明:本文为博主的原创文章,未经博主同意不得转载 前言 记录下自己的复现,保留意见 2017年6月13日,微软官方发布编号为CVE-2017-8464的漏洞公告,官方介绍Windows系统在解析快 ...
- CodeForces 628B New Skateboard 思维
B. New Skateboard time limit per test 1 second memory limit per test 256 megabytes input standard in ...
- fitnesse(gradle构建)安装步骤
1.安装jdk.ant.gradle(参考http://www.cnblogs.com/274914765qq/p/4401525.html) 2.下载Fitnesse https://github. ...
- HDU 1556【线段树区间更新】
这篇lazy讲的很棒: https://www.douban.com/note/273509745/ if(tree[rt].l == l && r == tree[rt].r) 这里 ...
- 51nod1276(xjb)
题目链接:http://www.51nod.com/onlineJudge/questionCode.html#!problemId=1276 题意:中文题诶- 思路:xjb 通过画图可以发现对于当前 ...
- assembly x86(nasm)的日常
cs的日常打卡. data segment ENG db 'SUNdayS Coming I Wanna Drive My Car,SUN,SUN$' ;43,35 sun1 db 'SUN' swc ...
- JSP && Servlet | AXIS 0配置 入门
Axis2是目前比较流行的WebService引擎.WebService被应用在很多不同的场景.例如,可以使用WebService来发布服务端 Java类的方法,以便使用不同的客户端进行调用.这样可以 ...
- SpringBoot | idea新建项目
1.new ----> Spring Initializr 2.设置相应文件名 3.选择需要配置