Astronauts UVA - 1391
题意:
有n个人,且给出他们的年龄。这些人的平均年龄是x。
- 假如y【i】>= x。那么这个人可以选择 任务 A 或者 任务C。
- 反之。这个人可以选择 任务 B 或者 任务C。
- 现在给出这些人的讨厌关系,互相讨厌的人不可以分配到同一个任务。
问:
是否可以分配任务给这n个人。
思路:
- 可以先抽象,每个人按照年龄无非就是两种类型 :A ∣ ∣ || ∣∣C or B ∣ ∣ || ∣∣C。
- 所以可以构造限制条件。
假如两个人互相讨厌,如果是同一种类型。那么要满足 一真一假。
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