训练指南 UVALive - 3713 (2-SAT)
layout: post
title: 训练指南 UVALive - 3713 (2-SAT)
author: "luowentaoaa"
catalog: true
mathjax: true
tags:
- 2-SAT
- 图论
- 训练指南
Astronauts
题意
有A,B,C三个任务要分配个N个宇航员,每个宇航员恰好要分配一个任务,设平均年龄为X,只有年龄大于或等于X的宇航员才能分配任务A。只有年龄严格小于X的宇航员才能分配任务B。而任务C没有限制。有M对宇航员相互讨厌,因此不能分配到同一任务。编程找出一个满足上述所有要求的任务分配方案。
题解
如果憎恶就代表不能一起去C,如果年龄相同就代表不能一起去A/B;
#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
const ll mod=998244353;
const int maxn=1e6+50;
const ll inf=0x3f3f3f3f3f3f3f3fLL;
struct TwoSAT{
int n;
vector<int> G[maxn*2];
bool mark[maxn*2];
int S[maxn*2],c;
bool dfs(int x){
if(mark[x^1])return false;
if(mark[x])return true;
mark[x]=true;
S[c++]=x;
for(int i=0;i<G[x].size();i++)
if(!dfs(G[x][i]))return false;
return true;
}
void init(int n){
this->n=n;
for(int i=0;i<n*2;i++)G[i].clear();
memset(mark,0,sizeof(mark));
}
/// x=xval or y= yval;
void add_caluse(int x,int xval,int y,int yval){
x=x*2+xval;
y=y*2+yval;
G[x^1].push_back(y);///如果x为真 那么y必须为假;
G[y^1].push_back(x);///如果y为真 那么x必须为假;
}
bool solve(){
for(int i=0;i<n*2;i+=2)
if(!mark[i]&&!mark[i+1]){
c=0;
if(!dfs(i)){
while(c>0)mark[S[--c]]=false;
if(!dfs(i+1))return false;
}
}
return true;
}
};
int n,a[maxn],m;
TwoSAT solver;
int tol;
int is_young(int x){
return a[x]*n<tol;
}
int main()
{
std::ios::sync_with_stdio(false);
std::cin.tie(0);
std::cout.tie(0);
while(cin>>n>>m&&n&&m){
tol=0;
solver.init(n);
for(int i=0;i<n;i++){
cin>>a[i];
tol+=a[i];
}
for(int i=0;i<m;i++){
int a,b;
cin>>a>>b;a--;b--;
solver.add_caluse(a,1,b,1);
if(is_young(a)==is_young(b))
solver.add_caluse(a,0,b,0);
}
if(!solver.solve())cout<<"No solution."<<endl;
else for(int i=0;i<n;i++)
if(solver.mark[i*2])cout<<"C"<<endl;
else if(is_young(i))cout<<"B"<<endl;
else cout<<"A"<<endl;
}
return 0;
}
训练指南 UVALive - 3713 (2-SAT)的更多相关文章
- 训练指南 UVALive - 3126(DAG最小路径覆盖)
layout: post title: 训练指南 UVALive - 3126(DAG最小路径覆盖) author: "luowentaoaa" catalog: true mat ...
- 训练指南 UVALive - 3415(最大点独立集)
layout: post title: 训练指南 UVALive - 3415(最大点独立集) author: "luowentaoaa" catalog: true mathja ...
- 训练指南 UVALive - 3989(稳定婚姻问题)
ayout: post title: 训练指南 UVALive - 3989(稳定婚姻问题) author: "luowentaoaa" catalog: true mathjax ...
- 训练指南 UVALive - 4043(二分图匹配 + KM算法)
layout: post title: 训练指南 UVALive - 4043(二分图匹配 + KM算法) author: "luowentaoaa" catalog: true ...
- 训练指南 UVALive - 5713(最小生成树 + 次小生成树)
layout: post title: 训练指南 UVALive - 5713(最小生成树 + 次小生成树) author: "luowentaoaa" catalog: true ...
- 训练指南 UVALive - 4080(最短路Dijkstra + 边修改 + 最短路树)
layout: post title: 训练指南 UVALive - 4080(最短路Dijkstra + 边修改 + 最短路树) author: "luowentaoaa" ca ...
- 训练指南 UVALive - 4287 (强连通分量+缩点)
layout: post title: 训练指南 UVALive - 4287 (强连通分量+缩点) author: "luowentaoaa" catalog: true mat ...
- 训练指南 UVALive - 5135 (双连通分量)
layout: post title: 训练指南 UVALive - 5135 (双连通分量) author: "luowentaoaa" catalog: true mathja ...
- 训练指南 UVALive - 3523 (双联通分量 + 二分图染色)
layout: post title: 训练指南 UVALive - 3523 (双联通分量 + 二分图染色) author: "luowentaoaa" catalog: tru ...
随机推荐
- [CF107D]Crime Management
题目大意:有一种长度为$n(n\leqslant 10^{18})$的字符串,给定$m(m\leqslant10^3)$种限制,即字符$c$出现的次数为$cnt$,若一个字符有多种限制,则满足任意一个 ...
- [ZJOI2005]沼泽鳄鱼 矩阵乘法
---题面--- 题解: 乍一看还是挺懵逼的.和HH去散步很像,思路也是类似的. 复制一段我在HH去散步的题解里面写的一段话吧: 考虑f[i][j]表示i和j是否右边相连,有为1,否则为0,那么f同时 ...
- AdjustTokenPrivileges启用权限
原文链接地址:http://blog.csdn.net/xbgprogrammer/article/details/7276760 我们有很多操作需要用到OpenProcess函数,而为了使程序 ...
- xiaoluo同志Linux学习之CentOS6.4
小罗同志写的不错,弄个列表过来啊 Linux学习之CentOS(三十六)--FTP服务原理及vsfptd的安装.配置 xiaoluo501395377 2013-06-09 01:04 阅读:56 ...
- peity(jQuery 插件可以将元素内容转换为一个小的 <svg> 饼图,圆环图,条形图和折线图)
API地址:https://www.awesomes.cn/repo/benpickles/peity 实例效果
- 【Foreign】魔法 [组合数][质因数分解]
魔法 Time Limit: 10 Sec Memory Limit: 256 MB Description Input Output 仅一行一个整数表示答案. Sample Input 4 10 ...
- 谈数据中心SDN与NFV
看到一篇谈论SDN与NFV的文章,分析的还不错,贴过来方便自己后续查阅: http://network.chinabyte.com/175/13095675.shtml 论数据中心SDN与NFV技术关 ...
- HDU 1840 Equations (数学)
title: Equations 数学 杭电1840 tags: [数学] 题目链接 Problem Description All the problems in this contest tota ...
- jquery with ajax
with session storage: 1.ajax请求可以放在 $(document).ready(function (){...}); 里. 2. $.ajax({ url: "/a ...
- bootstrap-datetimepicker年视图中endDate设置之后比正常时间提前两个月
问题 bootstrap-datetimepicker年视图中endDate设置结束时间为2016-08,(即8月之后的日期不能选)而在日历上显示时为2016-06,相差两个月,即6月之后的日期不能选 ...