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 ...
随机推荐
- [转载] Relearning to Learn - 学会学习
学会学习 说明: 本文是在阅读了下述博客后, 所作的梳理与总结, 原文链接是: 学会学习 阅读和理解是不够的, 你还需要记住你学的内容. 可通过把知识讲给不懂的人听, 抓住细节, 讲清讲透, 从而加深 ...
- [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 ...
- Chapter 5 Blood Type——9
He grimaced. 他扮了一个鬼脸. "Or better," I continued, the pent-up annoyance flowing freely now, ...
- Chapter 4 Invitations——28
"Oh, thanks, now that's all cleared up." Heavy sarcasm. “哦,真感谢,现在一切都清楚了.” 我很讽刺的说道 I realiz ...
- mtools-你可能没用过的mongodb神器
前言 接触 mongodb 已经有一段时间了,从一开始的不了解,到现在已慢慢适应这个nosql领域的佼佼者,还是经历了不少波折. 在进行数据库选型的时候,许多人总是喜欢拿 mongodb和mysql. ...
- 5分钟入门git模式开发
本文由云+社区发表 作者:唐维黎 导语 基于gui工具TortoiseGit让你快速进入git开发模式. 目前项目已逐步从svn移步到git开发模式,其中也针对git统一协议了适合git的开发规范, ...
- EF操作数据库的步骤和一些简单操作语句
这里是写给我自己做记录的,不会写成一篇很好的博客,也不会置顶,如果有朋友看到了,而且觉得里面的内容不咋的,希望见谅哈! 关于这部分内容,这里推荐一篇总结的非常好的博客,如果你点击进来了,那么请略过下面 ...
- Scrapy爬虫错误日志汇总
1.数组越界问题(list index out of range) 原因:第1种可能情况:list[index]index超出范围,也就是常说的数组越界. 第2种可能情况:list是一个空的, 没有一 ...
- Elasticsearch系列(4):基本搜索
空搜索 搜索API的最基础的形式是没有指定任何查询的空搜索 ,它简单地返回集群中所有索引下的所有文档,如下命令: GET /_search 返回如下结果: 查询结果解释: 1,hits 返回结果中最重 ...
- Go开发之路 -- 函数详解
声明语法 func 函数名 (参数列表) [(返回值列表)] {} Golang函数特点 a. 不支持重载,一个包不能有两个名字一样的函数 b. 函数是一等公民,函数也是一种类型,一个函数可以赋值给变 ...