Astronauts UVA - 1391(2-sat)

Astronauts UVA - 1391

题意:

有n个人,且给出他们的年龄。这些人的平均年龄是x。

  1. 假如y【i】>= x。那么这个人可以选择 任务 A 或者 任务C
  2. 反之。这个人可以选择 任务 B 或者 任务C
  3. 现在给出这些人的讨厌关系,互相讨厌的人不可以分配到同一个任务。


是否可以分配任务给这n个人。

思路:

  1. 可以先抽象,每个人按照年龄无非就是两种类型 :A ∣ ∣ || C or B ∣ ∣ || C。
  2. 所以可以构造限制条件。

假如两个人互相讨厌,如果是同一种类型。那么要满足 一真一假
p ∨ \lor q 和 p ‾ \overline{p} p ∨ \lor q ‾ \overline{q} q
假如是不同类型,要保证两者都不为C任务即可。
p ∨ \lor q

AC

#include <iostream>
#include <cstring>
#include <algorithm>
#include <vector>
#include <stack>
#define For(i,x,y) for(int i=(x); i<=(y); i++)
#define fori(i,x,y)  for(int i=(x); i<(y); i++)
#define pb push_back
#define mst(x,a) memset(x,a,sizeof(x))
#define fzhead EDGE(int _to, int _next)
#define fzbody to(_to), next(_next)
using namespace std;
const int maxn=1e5+10;
const int maxm=1e5+10;
string ans="ABC";
struct EDGE{
    int to, next;
    EDGE(){}
    fzhead:fzbody{}
}e[2*2*maxn];
int head[2*maxn],tot;
bool mark[maxn*2];
stack<int>s;
bool dfs(int x){
    if(mark[x^1])return false;
    if(mark[x])return true;
    mark[x]=true;
    s.push(x);
    for(int i=head[x]; i!=-1; i=e[i].next){
        int v=e[i].to;
        if(!dfs(v))return false;
    }
    return true;
}
void init(){
    mst(mark,0);
    mst(head,-1);tot=2;
}
void add(int bg, int to){
    e[tot]=EDGE(to,head[bg]);
    head[bg]=tot++;
}
void add_clause(int x, int xval, int y, int yval){
    x = x*2 + xval;
    y = y*2 + yval;
    add(x^1,y);
    add(y^1,x);
}
int n,m;
bool twoset(){
    for(int i=0; i<2*n; i+=2){
        if(!mark[i]&&!mark[i+1]){
            while(!s.empty())s.pop();
            if(!dfs(i)){
                while(!s.empty())mark[s.top()]=false,s.pop();
                if(!dfs(i+1))return false;
            }
        }
    }
    return true;
}
int x, y[maxn];
int main()
{
    ios::sync_with_stdio(0);cin.tie(0);cout.tie(0);
    while(cin>>n>>m,n||m){
        init();
        x=0;
        fori(i,0,n)cin>>y[i],x+=y[i];
        For(i,1,m){
            int u,v;cin>>u>>v;
            u--;v--;
            int mo1=(n*y[u]>=x)?0:1;
            int mo2=(n*y[v]>=x)?0:1;
            add_clause(u,1,v,1);
            if(mo1==mo2)add_clause(u,0,v,0);
        }
        if(twoset()){
            for(int i=0; i<n; i++){
                int mo=(n*y[i]>=x)?0:1;
                if(mo){
                    if(mark[i*2+1])cout<<'B'<<endl;
                    else cout<<'C'<<endl;
                }else {
                    if(mark[i*2+1])cout<<'A'<<endl;
                    else cout<<'C'<<endl;
                }
            }
        }else cout<<"No solution."<<endl;
    }
    return 0;
}//fasdfadsf
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值