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
随机推荐
- yii自动登陆的验证机制浅析
一直在使用yii进行开发, 也知道如何去使用, 也仅仅是知道怎么去用罢了, 终归是没研究过源码, 心里发虚, 今天遇到一个问题, 关于自动登陆的问题. 要求就是, 修改登陆保存session天数为自定 ...
- HDU 5211 筛法求约数
给出n个数a1,a2...an,定义函数 f[i]=j,(i<j),表示aj mod ai=0 的最小j,其中j大于i,如果不存在这样的数,则f[i]=0 求n个数所有f[]值的和 先用筛法o( ...
- C#解leetcode 16. 3Sum Closest
Given an array S of n integers, find three integers in S such that the sum is closest to a given num ...
- PT与PX,em(%)区别
字体大小的设置单位,常用的有2种:px.pt.这两个有什么区别呢?先搞清基本概念:px就是表示pixel,像素,是屏幕上显示数据的最基本的点:pt就是point,是印刷行业常用单位,等于1/72英寸. ...
- scn转换为十进制
- AMD和CMD的区别
1.cmd define(function(require,export){ var b = 1; var a = require('./a'); a.dosomething(); }); 2.amd ...
- int, NSInteger, NSUInteger, NSNumber的区别
新手在接触iOS或者Mac开发的时候,看到int和NSInteger,一般不清楚应该用哪个比较合适.我们先来查看下NSInteger的定义 #if __LP64__ || (TARGET_OS_EMB ...
- html5插入视频
- html中插入flash代码详解(转载)
<object classid="clsid:D27CDB6E-AE6D-11cf-96B8-444553540000" codebase="http://down ...
- 使用Gson进行json数据转换(list to json 和json to list)
文章借鉴自:http://blog.csdn.net/binyao02123202/article/details/7540407 下面是一个简单的例子: Java代码 public class Pe ...