UVALive - 3713 - Astronauts(图论——2-SAT)
Time Limit: 3000 mSec
![]() Problem Description
 Problem Description

Input
The input contains several blocks of test cases. Each case begins with a line containing two integers 1 ≤ n ≤ 100000 and 1 ≤ m ≤ 100000. The number n is the number of astronauts. The next n lines specify the age of the n astronauts; each line contains a single integer number between 0 and 200. The next m lines contains two integers each, separated by a space. A line containing i and j (1 ≤ i,j ≤ n) means that the i-th astronaut and the j-th astronaut hate each other. The input is terminated by a block with n = m = 0.
![]() Output
Output
For each test case, you have to output n lines, each containing a single letter. This letter is either ‘A’, ‘B’, or ‘C’. The i-th line describes which mission the i-th astronaut is assigned to. Astronauts that hate each other should not be assigned to the same mission, only young astronauts should be assigned to Mission B and only senior astronauts should be assigned to Mission A. If there is no such assignment, then output the single line ‘No solution.’ (without quotes).
![]() Sample Input
Sample Input
![]() Sample Output
 Sample Output
B C C B C B C B A C C A C A C A
题解:几乎是2-SAT板子题,节点之间的关系稍微复杂了一点,总体来说并不困难。两种人,老年人只能做A,C,年轻人只能做B,C,所以每种人分别对应两种人格(其实就是true和false)如果相互厌恶的两人同类,那么不能同为true,也不能同为false,如果不同类,就只需不同为false(对应C)即可,至于输出方案,mark数组就是答案。
#include <bits/stdc++.h> using namespace std; #define REP(i, n) for (int i = 1; i <= (n); i++)
#define sqr(x) ((x) * (x)) const int maxn = + ;
const int maxm = + ;
const int maxs = + ; typedef long long LL;
typedef pair<int, int> pii;
typedef pair<double, double> pdd; const LL unit = 1LL;
const int INF = 0x3f3f3f3f;
const LL mod = ;
const double eps = 1e-;
const double inf = 1e15;
const double pi = acos(-1.0); struct TwoSAT
{
int n, mark[maxn * ];
vector<int> G[maxn * ];
int S[maxn * ], c; void init(int n)
{
this->n = n;
memset(mark, , sizeof(mark));
for (int i = ; i < * n; i++)
{
G[i].clear();
}
} bool dfs(int x)
{
if (mark[x ^ ])
return false;
if (mark[x])
return true; mark[x] = true;
S[c++] = x;
for (auto v : G[x])
{
if (!dfs(v))
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]] = ;
}
if (!dfs(i + ))
return false;
}
}
}
return true;
} void add_clause(int x, int xval, int y, int yval)
{
x = x * + xval;
y = y * + yval;
G[x ^ ].push_back(y);
G[y ^ ].push_back(x);
}
}; int n, m, sum;
int age[maxn];
TwoSAT solver; bool is_young(int x)
{
return x * n < sum;
} main()
{
ios::sync_with_stdio(false);
cin.tie();
//freopen("input.txt", "r", stdin);
//freopen("output.txt", "w", stdout);
while (cin >> n >> m && (n || m))
{
solver.init(n);
sum = ;
for (int i = ; i < n; i++)
{
cin >> age[i];
sum += age[i];
}
int u, v;
for (int i = ; i < m; i++)
{
cin >> u >> v;
if (u == v)
continue;
u--, v--;
if (is_young(age[u]) == is_young(age[v]))
{
solver.add_clause(u, , v, );
solver.add_clause(u, , v, );
}
else
{
solver.add_clause(u, , v, );
}
}
if (solver.solve())
{
for (int i = ; i < * n; i += )
{
int a = age[i / ];
if (a * n >= sum)
{
if (solver.mark[i])
{
cout << "C" << endl;
}
else
{
cout << "A" << endl;
}
}
else
{
if (solver.mark[i])
{
cout << "C" << endl;
}
else
{
cout << "B" << endl;
}
}
}
}
else
{
cout << "No solution." << endl;
}
}
return ;
}
UVALive - 3713 - Astronauts(图论——2-SAT)的更多相关文章
- UVALive - 3713 Astronauts
		给定n个宇航员的年龄,平均年龄为 ave,根据下列要求分配任务: B任务只能分配给年龄<ave的宇航员: A任务只能分配给年龄>=ave的宇航员: C任务可以任意分配. 给定m组互相憎恨的 ... 
- UVALive 3713 Astronauts (2-SAT,变形)
		题意: 有A,B,C三种任务,每个人必获得1个任务,大于等于平均年龄的可以选择A和C,小于平均年龄的可以选择B和C.这些人有一些是互相讨厌的,必须不能执行同任务,问能否安排他们工作?若行,输出任意一组 ... 
- 训练指南 UVALive - 3713 (2-SAT)
		layout: post title: 训练指南 UVALive - 3713 (2-SAT) author: "luowentaoaa" catalog: true mathja ... 
- 【UVALive - 3713】Astronauts (2-SAT)
		题意: 有n个宇航员,按照年龄划分,年龄低于平均年龄的是年轻宇航员,而年龄大于等于平均年龄的是老练的宇航员. 现在要分配他们去A,B,C三个空间站,其中A站只有老练的宇航员才能去,而B站是只有年轻的才 ... 
- Astronauts UVALive - 3713(2-SAT)
		大白书例题 #include <iostream> #include <cstdio> #include <sstream> #include <cstrin ... 
- UVA 3713 Astronauts
		The Bandulu Space Agency (BSA) has plans for the following three space missions: • Mission A: Landin ... 
- 2-sat 分类讨论  UVALIVE 3713
		蓝书326 //看看会不会爆int!数组会不会少了一维! //取物问题一定要小心先手胜利的条件 #include <bits/stdc++.h> using namespace std; ... 
- LA 3713 Astronauts
		给个题目链接: https://icpcarchive.ecs.baylor.edu/index.php?option=com_onlinejudge&Itemid=8&page=sh ... 
- UVA Live 3713 Astronauts (2-SAT)
		用布尔变量表示状态,把限制条件转化为XνY的形式以后跑2SAT,根据变量取值输出方案. #include<bits/stdc++.h> using namespace std; ; #de ... 
随机推荐
- ES 03 - 初探Elasticsearch的主要配置文件(以6.6.0版本为例)
			目录 1 elasticsearch.yml(ES服务配置) 1.1 Cluster集群配置 1.2 Node节点配置 1.3 Paths路径配置 1.4 Memory内存配置 1.5 Network ... 
- 使用MQ来保证分布式事务的最终一致性
			前言 之前我们讨论了如何拆分一个订单下单的一个服务(https://www.cnblogs.com/linkstar/p/9610268.html) 从单体到微服务的拆分,当时我们只是对原来的整个服务 ... 
- ThreadPoolExecutor系列二——ThreadPoolExecutor 代码流程图
			ThreadPoolExecutor 代码流程图 本文系作者原创,转载请注明出处:http://www.cnblogs.com/further-further-further/p/7681648.ht ... 
- 【Java基础】【08面向对象_继承&方法&final】
			08.01_面向对象(代码块的概述和分类)(了解)(面试的时候会问,开发不用或者很少用) A:代码块概述 在Java中,使用{}括起来的代码被称为代码块. B:代码块分类 根据其位置和声明的不同,可以 ... 
- DocX开源WORD操作组件的学习系列三
			DocX学习系列 DocX开源WORD操作组件的学习系列一 : http://www.cnblogs.com/zhaojiedi1992/p/zhaojiedi_sharp_001_docx1.htm ... 
- JS引擎线程的执行过程的三个阶段(二)
			继续JS引擎线程的执行过程的三个阶段(一) 内容, 如下: 三. 执行阶段 1. 网页的线程 永远只有JS引擎线程在执行JS脚本程序,其他三个线程只负责将满足触发条件的处理函数推进事件队列,等待JS引 ... 
- MySQL/MariaDB系列文章目录
			以下是本系列文章的大纲,此页博文完全原创,花费了作者本人的极大心血,如转载,请务必标明原文链接. 如果觉得文章不错,还请帮忙点下"推荐",各位的支持,能激发和鼓励我更大的写作热情. ... 
- Linux find常用用法示例
			在此处只给出find的基本用法示例,都是平时我个人非常常用的搜索功能.如果有不理解的部分,则看后面的find运行机制详解对于理论的说明,也建议在看完这些基本示例后阅读一遍理论说明,它是本人翻译自fin ... 
- wpf 无缝滚动
			很早以前有项目就需要文字无缝滚动的效果但无奈当时技术不到位 人也比较懒惰(大概程序猿都是这个样子吧) 此方法并非只文字无缝其实任何内容都可以 <ScrollViewer Name="s ... 
- 你必须知道的.net读书笔记第四回:后来居上:class和struct
			基本概念 1.1. 什么是class? class(类)是面向对象编程的基本概念,是一种自定义数据结构类型,通常包含字段.属性.方法.属性.构造函数.索引器.操作符等.因为是基本的概念,所以不必在此 ... 
