给个题目链接:

https://icpcarchive.ecs.baylor.edu/index.php?option=com_onlinejudge&Itemid=8&page=show_problem&problem=1714

显然每个人只有两种选择:C或者A/B。而且后一种只和这个人的年龄有关。

这就是一个很显然的2-SAT模型了,显然有矛盾的不能都选C,如果两个人的年龄段还一样,那就更悲剧了,也不能同时选A/B。

#include<iostream>
#include<cstdio>
#include<cstdlib>
#include<algorithm>
#include<cmath>
#include<vector>
#include<cstring>
#define pb push_back
#define ll long long
#define maxn 200005
#define M(x) memset(x,0,sizeof(x))
using namespace std;
int n,m;
struct TOW_SAT{
vector<int> g[maxn];
int tp[maxn],age[maxn];
int tot,node[maxn],num;
bool mark[maxn<<1|1]; inline void init(){
for(int i=(n<<1)-1;i>=0;i--) g[i].clear();
M(tp),M(age);
M(mark);
tot=num=0;
} inline void add(int x,int y){
int Cx=x<<1,Ax=Cx+1;
int Cy=y<<1,Ay=Cy+1; g[Cx].pb(Ay),g[Cy].pb(Ax);
if(tp[x]==tp[y]){
g[Ax].pb(Cy);
g[Ay].pb(Cx);
}
} bool dfs(int x){
if(mark[x^1]) return 0;
if(mark[x]) return 1;
mark[x]=1,node[++num]=x; for(int i=g[x].size()-1;i>=0;i--)
if(!(dfs(g[x][i]))) return 0; return 1;
} inline bool work(){
int cl=n<<1;
for(int i=0;i<cl;i+=2) if(!mark[i]&&!mark[i+1]){
num=0;
if(!dfs(i)){
while(num) mark[node[num--]]=0;
if(!dfs(i+1)) return 0;
}
} return 1;
} inline void solve(){
init();
for(int i=0;i<n;i++){
scanf("%d",age+i);
tot+=age[i];
} for(int i=0;i<n;i++) if(age[i]*n>=tot) tp[i]=1; int uu,vv;
for(int i=1;i<=m;i++){
scanf("%d%d",&uu,&vv);
uu--,vv--,add(uu,vv);
} if(!work()) puts("No solution");
else{
for(int i=0;i<n;i++){
if(mark[i<<1]) puts("C");
else if(tp[i]) puts("A");
else puts("B");
}
}
}
}mine; int main(){
while(scanf("%d%d",&n,&m)==2&&n&&m) mine.solve();
return 0;
}

  

LA 3713 Astronauts的更多相关文章

  1. UVALive - 3713 - Astronauts(图论——2-SAT)

    Problem   UVALive - 3713 - Astronauts Time Limit: 3000 mSec Problem Description Input The input cont ...

  2. LA 3713

    The Bandulu Space Agency (BSA) has plans for the following three space missions: Mission A: Landing ...

  3. UVA 3713 Astronauts

    The Bandulu Space Agency (BSA) has plans for the following three space missions: • Mission A: Landin ...

  4. LA 3713 宇航员分组

    题目链接:http://vjudge.net/contest/142615#problem/B 题意:有A,B,C三个人物要分配个N个宇航员,每个宇航员恰好要分配一个任务,设平均年龄为X,只有年龄大于 ...

  5. UVALive 3713 Astronauts (2-SAT,变形)

    题意: 有A,B,C三种任务,每个人必获得1个任务,大于等于平均年龄的可以选择A和C,小于平均年龄的可以选择B和C.这些人有一些是互相讨厌的,必须不能执行同任务,问能否安排他们工作?若行,输出任意一组 ...

  6. UVALive - 3713 Astronauts

    给定n个宇航员的年龄,平均年龄为 ave,根据下列要求分配任务: B任务只能分配给年龄<ave的宇航员: A任务只能分配给年龄>=ave的宇航员: C任务可以任意分配. 给定m组互相憎恨的 ...

  7. UVA Live 3713 Astronauts (2-SAT)

    用布尔变量表示状态,把限制条件转化为XνY的形式以后跑2SAT,根据变量取值输出方案. #include<bits/stdc++.h> using namespace std; ; #de ...

  8. 图论$\cdot$2-SAT问题

    2-SAT问题是这样的:有$n$个布尔变量$x_i$,另有$m$个需要满足的条件,每个条件的形式都是“$x_i$为真/假或者$x_j$为真/假”.比如:"$x_1$为真或者$x_3$为假“. ...

  9. 2-SAT 问题与解法小结

    2-SAT 问题与解法小结 这个算法十分的奇妙qwq... 将一类判定问题转换为图论问题,然后就很容易解决了. 本文有一些地方摘录了一下赵爽<2-SAT解法浅析> (侵删) 一些概念: \ ...

随机推荐

  1. WPF调用摄像头

    添加程序集:WPFMediaKit.dll 更关键代码如下: 界面设计代码如下: <Window x:Class="摄像头调用.MainWindow" xmlns=" ...

  2. Linux常用命令及工具记录(持续更新)

    一.命令 convmv   作用:文件名的编码转换   安装:sudo apt-get install convmv   使用:convmv * -f gbk -t utf8 --notest   c ...

  3. Node rescue/unrescue相关代码流程图

  4. HDU 4669 Mutiples on a circle 动态规划

    参考了官方题解给的方法: 对于处理循环,官方给了一种很巧妙的方法: #include <cstdio> #include <cstring> #include <cstd ...

  5. try...catch 语句

    一般情况下,我们很少用到 try...catch 语句,但是有时候为了测试代码中的错误,也有可能会用到.小白我也在工作中用到过.那么好的程序设计,什么时候会用到呢? try...catch 一般用来捕 ...

  6. PHP字符串基本操作函数

    常用函数: trim():去除字符串的空格及传入的参数 strlen():获取字符串长度 substr():按照两个参数截取字符串 str_replace():字符串替换 str_split():将字 ...

  7. 关于tap设备

    $QEMU_PATH \ -nographic \ -drive file=./rootfs.ext4,format=raw \ -net nic,vlan=0 -net tap,vlan=0,ifn ...

  8. hdu 3354 Probability One

    Probability One Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)T ...

  9. 浅析_tmain() 与 main() 函数的区别

    _tmain()是为了支持Unicode所使用的main的一个别名,既然是别名,应该有宏定义过的,在<stdafx.h>里 #include <stdio.h> #indlud ...

  10. [poj] 2286 The Rotation Game || ID-DFS

    原题 有1234四个数字,每个数字八个.有八种方向的移动,使得操作后中间八个方块的数字相同,求最小操作步数. 对于这种求最小步数的看起来就是dfs的题,就ID-DFS就好了. //不知道为什么都是ID ...