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
随机推荐
- openstack初探
一 .openstack三大核心功能: 计算--Nova.存储--Cinder.网络--Neutron. Nova:提供了计算资源的管理,可以管理跨服务网络的VM实例.还提供对多种Hypervisor ...
- HTML5 微信二维码提示框
这是一个js的小案例,主要效果是显示一个微信二维码的提示框,非常简单实用. 源码如下: JS部分 <script src="js/jquery-1.8.3.min.js"&g ...
- MVC3中 ViewBag、ViewData和TempData的使用和区别(不是自己写的)
(网上抄的,并未消化)在MVC3开始,视图数据可以通过ViewBag属性访问,在MVC2中则是使用ViewData.MVC3中保留了ViewData的使用.ViewBag 是动态类型(dynamic) ...
- StringBuilder - new line.
//use this to implement platform-cross new-line. StringBuilder sb = new StringBuilder(); sb.append(S ...
- ArcGis(01)——地图切片以及发布底图服务
ArcGis(01)——地图切片以及发布底图服务 环境 操作系统:win10_x64 Gis版本:Arcis server 10.2 准备 1.tif格式地图资源 2.Arcis server 10. ...
- (转载)小课堂UI-有关配色的一个小技巧
- 解决SQL Server Management Studio Express不支持更新全文目录的方法
微软的说法:https://msdn.microsoft.com/zh-cn/library/ms365247.aspx 可以用命令创建: A.创建唯一索引.全文目录和全文索引 以下示例对 Adven ...
- Sql 格式化工具
SQL Pretty Printer:目前提供4种使用方式,桌面版本,SSMS(SQL Server Management Studio)插件,VS插件,和提供API接口. SQL Pretty Pr ...
- python:UnboundLocalError: local variable 'xxx' referenced before assignment
近来一直都在学习python语言,偶然在伯乐在线看到2017年京东C/C++的面试题.就打算用python+ST3 IDE顺便敲下面试题代码. 原题 C语言: #include <stdio.h ...
- 如何编写pythonGNURADIO应用
三个基本概念: flow graph 很大一部分GNURADIO程序就是一个流图.流图包括若干信号处理模块(BLock),以及一些用表示数据流的边. BLock 所有信号处理过程在block中进行.在 ...