UVALive - 3713 - Astronauts(图论——2-SAT)
Time Limit: 3000 mSec
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
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 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 ...
随机推荐
- 初探Java设计模式4:JDK中的设计模式
JDK中设计模式 本文主要是归纳了JDK中所包含的设计模式,包括作用和其设计类图.首先来个总结,具体的某个模式可以一个一个慢慢写,希望能对研究JDK和设计模式有所帮助.一.设计模式是什么(1)反复出现 ...
- 重写equals的详细说明
返回目录 原文地址Java equals() 方法总结 equals() 超类 Object 中有这个 equals() 方法,该方法主要用于比较两个对象是否相等.该方法的源码如下: public b ...
- [Leetcode]669 Trim a Binary Search Tree
Given a binary search tree and the lowest and highest boundaries as L and R, trim the tree so that a ...
- npm用法
查看包信息npm info mongodb 查看包的最新版本npm view mongodb version 安装npm install mongodb@2.2.33 已安装的包修改版本npm ins ...
- 痞子衡嵌入式:一表全搜罗常见短距离无线通信协议(Wi-Fi/Bluetooth/ZigBee/Thread...)
大家好,我是痞子衡,是正经搞技术的痞子.今天痞子衡给大家介绍的是常见短距离无线通信协议. 短距离无线通信是物联网的基础,随着物联网IoT的火热发展,各种短距离无线通信协议也是层出不穷,这些协议标准各有 ...
- js内存深入学习(二)
继上一篇文章 js内存深入学习(一) 3. 内存泄漏 对于持续运行的服务进程(daemon),必须及时释放不再用到的内存.否则,内存占用越来越高,轻则影响系统性能,重则导致进程崩溃. 对于不再用到的内 ...
- JaveWeb学习之Servlet(二):ServletConfig和ServletContext
原文同步发表至个人博客[夜月归途] 原文链接:http://www.guitu18.com/se/java/2018-07-26/20.html 作者:夜月归途 出处:http://www.guitu ...
- WebBrowser Cookie
WebBrowser的Cookie操作 .在WebBrowser中获取Cookie CookieContainer myCookieContainer = new CookieContainer(); ...
- 变量内容的删除、取代与替换 (Optional)
变量除了可以直接设置来修改原本的内容之外,有没有办法通过简单的动作来将变量的内容进行微调呢? 举例来说,进行变量内容的删除.取代与替换等!是可以的!我们可以通过几个简单的小步骤来进行变量内容的微调喔! ...
- winform中获取指定文件夹下的所有图片
方法一: C#的IO自带了一个方法DirectoryInfo dir = new DirectoryInfo("文件夹名称");dir.getFiles();//这个方法返回值就是 ...