uvalive 3713 2-sat
2017-01-13 20:25
218 查看
/** 分析:训练指南图论例题10,p327 2-sat **/ #include <iostream> #include <cstdio> #include <cstring> #include <vector> #include <cmath> #include <algorithm> using namespace std; const int maxn=100005; 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)); } void add_clause(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+=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; } }; TwoSAT solver; int n, m, total_age, age[maxn]; int is_young(int x) { return age[x] * n < total_age; } int main() { while(scanf("%d%d",&n,&m)!=EOF) { if(n==0) break; total_age=0; for(int i=0;i<n;i++) { scanf("%d",&age[i]); total_age+=age[i]; } solver.init(n); for(int i=0;i<m;i++) { int a,b; scanf("%d%d",&a,&b); a--;b--; if(a==b) continue; solver.add_clause(a,1,b,1); if(is_young(a)==is_young(b)) { solver.add_clause(a,0,b,0); } } if(!solver.solve()) printf("No solution.\n"); else { for(int i=0;i<n;i++) { if(solver.mark[i*2]) printf("C\n"); else if(is_young(i)) printf("B\n"); else printf("A\n"); } } } return 0; }
相关文章推荐
- UVA Live 3713 Astronauts (2-SAT)
- 【UVALive - 3713】Astronauts (2-SAT)
- UVAlive3713 Astronauts(2-SAT)
- UVALIVE 3713 Astronauts(2-SAT)
- UVAlive3713 Astronauts(2-SAT)
- UVALive - 3713 Astronauts(2-SAT)
- UVALive 3713 浅谈2-SAT问题图论求解法
- UVALive3713-Astronauts(2-SAT)
- UVALive 3713 Astronauts(2-sat+输出任意路径)
- UVALive 3713 Astronauts (2-SAT,变形)
- UVALive-3713 Astronauts (2-SAT)
- UVALive3713-Astronauts(2-SAT)
- UVALive 3713 - Astronauts(2-SAT)
- [蓝书/Ch5] Astronauts UVALive - 3713 2-SAT
- UVALive - 3713 Astronauts
- UVALive 3713 Astronauts
- UVALive 3211 (训练指南)图论例题9 2-sat+二分
- 【UVALive - 3211】Now or later (二分+2-SAT)
- 宇航员分组(uvalive 3713)
- UVA Live 7957 (Gym 101201F) Illumination 2-SAT