UVA1391【LA3713】 经典2-SAT
来源:互联网 发布:女性情趣用品淘宝 编辑:程序博客网 时间:2024/06/06 19:47
题意:给出一些宇航员他们的年龄,x是他们的平均年龄,其中A任务只能给年龄大于等于x的人,B任务只能给小于x的人,C任务没有限制。再给出m对人,他们不能同任务。现在要你输出一组符合要求的任务安排。
这道题是刘汝佳蓝书上讲了的题,在这里套用他的模板,实在是好用啊…
#include<stdio.h>#include<vector>#include<cstring>using namespace std;const int maxn=100005;double sum,ave;int age[maxn],u,v,n,m;inline int read(){ register int x=0,f=1; register char ch=getchar(); while(ch<'0'||ch>'9'){if(ch=='-')f*=-1;ch=getchar();} while(ch>='0'&&ch<='9')x=(x<<3)+(x<<1)+ch-'0',ch=getchar(); return f*x;}struct SAT{ 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,false,sizeof(mark)); } void add(int x,int xval,int y,int yval){ x=x*2+xval; y=y*2+yval; G[x^1].push_back(y); G[y^1].push_back(x); } bool solve(){ for(int i=0;i<n*2;i=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; }};SAT Mercer;int main(){ while(n=read(),m=read(),n||m){ Mercer.init(n);sum=0; for(register int i=0;i<n;i++) age[i]=read(),sum+=age[i]; ave=sum/n; for(register int i=0;i<m;i++){ u=read(),v=read(); u--,v--; if((age[u]<ave&&age[v]<ave)||(age[u]>=ave&&age[v]>=ave)){ Mercer.add(u,0,v,0); Mercer.add(u,1,v,1); } else Mercer.add(u,1,v,1); } if(!Mercer.solve()) puts("No solution."); else{ for(int i=0;i<2*n;i=i+2){ if(Mercer.mark[i]) puts("C"); else printf("%c\n",age[i/2]>=ave?'A':'B'); } } } return 0;}
阅读全文
0 0
- UVA1391【LA3713】 经典2-SAT
- UVA1391、LA3713【astronauts...】【2 -SAT】
- 【2-sat】uva1391
- uva1391 - Astronauts 2-SAT
- LA3713--Astronauts(2-sat)
- 2-SAT问题(LA3713)
- La3713 Astronauts 2-Sat
- UVA1391 Astronauts(LA3713)
- 【UVa1391】宇航员分组Astronauts【2-SAT】【强连通分量】
- hdu 3622 2-sat经典问题
- hdu 1824 && hdU 3062 2-SAT经典
- bzoj1823 JSOI2010 满汉全席 2-SAT 经典建模
- hdu 1815 2-SAT经典题
- LA3713 Astronauts
- poj3678 Katu Puzzle(2-SAT+经典建图)
- POJ 3678 经典AND,OR,XOR的2-sat问题
- poj 3678 Katu Puzzle 【2-sat 经典建图】
- hdoj 4115 Eliminate the Conflict 【2-sat 经典建图】
- 多数据源配置-使用spring配置多个数据源实现读写分离
- 函数指针和指针函数的区别
- 排序列表转换为二分查找树-LintCode
- JAVA虚拟机系列(六)
- 刨根问底KVC
- UVA1391【LA3713】 经典2-SAT
- Java中String类的concat方法
- MyBatis的resultMap简介
- 基于freemarker在本地生成word(.doc)文档,只看这一篇就够了
- Java学习day5
- Spring框架03
- 基于邻接矩阵的图的各种遍历
- NIS安装配置备忘
- JAVA虚拟机系列(七)