UVAlive3713 Astronauts(2-SAT)
题目链接:http://acm.hust.edu.cn/vjudge/problem/viewProblem.action?id=18511
【思路】
2-SAT。
设分得A或B类任务为1 C类任务为0,考虑互相讨厌的两人:如果年龄阶段相同,则需要满足a!=b,即(a==1 or b==1)=1 && (a==0 or b==0)=1,如果年龄阶段不同,则需要满足ab不同时为C,即(a==1 or b==1)=1。
【代码】
#include<cstdio>
#include<vector>
#include<cstring>
#include<iostream>
#include<algorithm>
using namespace std; const int maxn = +; struct TwoSAT {
int n;
vector<int> G[maxn*];
bool mark[maxn*];
int S[maxn*],c; void init(int n) {
this->n=n;
for(int i=;i<n*;i++) G[i].clear();
memset(mark,,sizeof(mark));
}
void addc(int x,int xval,int y,int yval) {
x=x*+xval;
y=y*+yval;
G[x^].push_back(y);
G[y^].push_back(x);
}
bool dfs(int x) {
if(mark[x^]) return false;
if(mark[x]) return true;
mark[x]=true;
S[c++]=x;
for(int i=;i<G[x].size();i++)
if(!dfs(G[x][i])) return false;
return true;
}
bool solve() {
for(int i=;i<n*;i+=)
if(!mark[i] && !mark[i+]) {
c=;
if(!dfs(i)) {
while(c>) mark[S[--c]]=false;
if(!dfs(i+)) return false;
}
}
return true;
}
}ts; int n,m;
int a[maxn]; int main() {
while(scanf("%d%d",&n,&m)== && n) {
int u,v,x=;
for(int i=;i<n;i++) scanf("%d",&a[i]),x+=a[i];
ts.init(n);
for(int i=;i<m;i++) {
scanf("%d%d",&u,&v);
u--,v--;
int k1=a[u]*n<x? :;
int k2=a[v]*n<x? :;
if(k1==k2) {
ts.addc(u,,v,);
ts.addc(u,,v,);
}
else ts.addc(u,,v,);
}
if(!ts.solve()) printf("No solution.\n");
else
for(int i=;i<n;i++)
if(ts.mark[i*]) printf("C\n");
else {
if(a[i]*n<x) printf("B\n");
else printf("A\n");
}
}
return ;
}
UVAlive3713 Astronauts(2-SAT)的更多相关文章
- UVALive - 3713 - Astronauts(图论——2-SAT)
Problem UVALive - 3713 - Astronauts Time Limit: 3000 mSec Problem Description Input The input cont ...
- POJ 3678 Katu Puzzle(2 - SAT) - from lanshui_Yang
Description Katu Puzzle is presented as a directed graph G(V, E) with each edge e(a, b) labeled by a ...
- 学习笔记(two sat)
关于two sat算法 两篇很好的论文由对称性解2-SAT问题(伍昱), 赵爽 2-sat解法浅析(pdf). 一些题目的题解 poj 3207 poj 3678 poj 3683 poj 3648 ...
- Katu Puzzle POJ - 3678 (2 - sat)
有N个变量X1X1~XNXN,每个变量的可能取值为0或1. 给定M个算式,每个算式形如 XaopXb=cXaopXb=c,其中 a,b 是变量编号,c 是数字0或1,op 是 and,or,xor 三 ...
- LA 3211 飞机调度(2—SAT)
https://vjudge.net/problem/UVALive-3211 题意: 有n架飞机需要着陆,每架飞机都可以选择“早着陆”和“晚着陆”两种方式之一,且必须选择一种,第i架飞机的早着陆时间 ...
- 【UVALive - 3713】Astronauts (2-SAT)
题意: 有n个宇航员,按照年龄划分,年龄低于平均年龄的是年轻宇航员,而年龄大于等于平均年龄的是老练的宇航员. 现在要分配他们去A,B,C三个空间站,其中A站只有老练的宇航员才能去,而B站是只有年轻的才 ...
- spring定时任务详解(@Scheduled注解)( 转 李秀才的博客 )
在springMVC里使用spring的定时任务非常的简单,如下: (一)在xml里加入task的命名空间 xmlns:task="http://www.springframework.or ...
- MongoDB 聚合管道(Aggregation Pipeline)
管道概念 POSIX多线程的使用方式中, 有一种很重要的方式-----流水线(亦称为"管道")方式,"数据元素"流串行地被一组线程按顺序执行.它的使用架构可参考 ...
- mysql触发器,答题记录表同步教学跟踪(用户列表)
aaarticlea/png;base64,iVBORw0KGgoAAAANSUhEUgAABVQAAAOOCAIAAABgEw4AAAAgAElEQVR4nOy92VcT27r/zX+xLtflvt
随机推荐
- 今日推荐(三)AndroidResideMenu类似QQ侧滑效果
效果图: DEMO 本代码即是DEMO,您可以下载后选择您喜欢的IDE运行.SDK版本建议使用4.0以上 Version Migration 从 v1.0, v1.1, v1.2, v1.3 升级到 ...
- Day5 - Python基础5 常用模块学习
Python 之路 Day5 - 常用模块学习 本节大纲: 模块介绍 time &datetime模块 random os sys shutil json & picle shel ...
- xUtils3源码分析(一):view的绑定
概述 xUtils3是国人开发的一款功能丰富的Android快速开发框架,值得研究下.zip包下载:[ZIP]xutils主要分以下几个模块 视图绑定模块 网络请求模块 数据库模块 图片加载模块 我们 ...
- 转载:浅析C#深拷贝与浅拷贝
原文地址 :http://www.cnblogs.com/xugang/archive/2010/09/09/1822555.html 感谢博主分享! 也许会有人这样解释C# 中浅拷贝与深拷贝区别 ...
- python3下的super()
大家都知道super是用来解决python钻石多重继承出现的基类重复调用的问题,这个就不赘述了,不了解的请点击. 但是我发现还有个问题在于不是钻石继承时继承先后顺序的问题,也就是如果mixin与继承的 ...
- java org.apache.struts.taglib.html.BEAN 没有找到
index.jsp <body> <a href="login2.do">登陆(struts标签)</a><br> </bod ...
- iOS开发实现登陆
Assumption假设:iOS端加载Web页,然后用户输入用户名密码登陆,WebServer会把用户登陆信息记载在Cookie.那么iOS客户端如何取到Cookie中的登陆信息. 客户端监听 NSH ...
- 动态加载下拉框列表并添加onclick事件
1. js动态加载元素并设置属性 摘自(http://www.liangshunet.com/ca/201408/336848696.htm) <div id="parent&quo ...
- Nginx配置文件nginx.conf详细说明
Nginx配置文件nginx.conf详细说明 #worker_processes 8; #worker_cpu_affinity 00000001 00000010 00000100 0000100 ...
- 12 Integer to Roman(int转罗马数字Medium)
题目意思:1-3999转罗马数字 思路:从大往小减 ps:这题有点蛋疼 class Solution { public: string intToRoman(int num) { string a[] ...