layout: post

title: 训练指南 UVALive - 3713 (2-SAT)

author: "luowentaoaa"

catalog: true

mathjax: true

tags:

- 2-SAT

- 图论

- 训练指南


Astronauts

UVALive - 3713

题意

有A,B,C三个任务要分配个N个宇航员,每个宇航员恰好要分配一个任务,设平均年龄为X,只有年龄大于或等于X的宇航员才能分配任务A。只有年龄严格小于X的宇航员才能分配任务B。而任务C没有限制。有M对宇航员相互讨厌,因此不能分配到同一任务。编程找出一个满足上述所有要求的任务分配方案。

题解

如果憎恶就代表不能一起去C,如果年龄相同就代表不能一起去A/B;

  1. #include<bits/stdc++.h>
  2. using namespace std;
  3. typedef long long ll;
  4. const ll mod=998244353;
  5. const int maxn=1e6+50;
  6. const ll inf=0x3f3f3f3f3f3f3f3fLL;
  7. struct TwoSAT{
  8. int n;
  9. vector<int> G[maxn*2];
  10. bool mark[maxn*2];
  11. int S[maxn*2],c;
  12. bool dfs(int x){
  13. if(mark[x^1])return false;
  14. if(mark[x])return true;
  15. mark[x]=true;
  16. S[c++]=x;
  17. for(int i=0;i<G[x].size();i++)
  18. if(!dfs(G[x][i]))return false;
  19. return true;
  20. }
  21. void init(int n){
  22. this->n=n;
  23. for(int i=0;i<n*2;i++)G[i].clear();
  24. memset(mark,0,sizeof(mark));
  25. }
  26. /// x=xval or y= yval;
  27. void add_caluse(int x,int xval,int y,int yval){
  28. x=x*2+xval;
  29. y=y*2+yval;
  30. G[x^1].push_back(y);///如果x为真 那么y必须为假;
  31. G[y^1].push_back(x);///如果y为真 那么x必须为假;
  32. }
  33. bool solve(){
  34. for(int i=0;i<n*2;i+=2)
  35. if(!mark[i]&&!mark[i+1]){
  36. c=0;
  37. if(!dfs(i)){
  38. while(c>0)mark[S[--c]]=false;
  39. if(!dfs(i+1))return false;
  40. }
  41. }
  42. return true;
  43. }
  44. };
  45. int n,a[maxn],m;
  46. TwoSAT solver;
  47. int tol;
  48. int is_young(int x){
  49. return a[x]*n<tol;
  50. }
  51. int main()
  52. {
  53. std::ios::sync_with_stdio(false);
  54. std::cin.tie(0);
  55. std::cout.tie(0);
  56. while(cin>>n>>m&&n&&m){
  57. tol=0;
  58. solver.init(n);
  59. for(int i=0;i<n;i++){
  60. cin>>a[i];
  61. tol+=a[i];
  62. }
  63. for(int i=0;i<m;i++){
  64. int a,b;
  65. cin>>a>>b;a--;b--;
  66. solver.add_caluse(a,1,b,1);
  67. if(is_young(a)==is_young(b))
  68. solver.add_caluse(a,0,b,0);
  69. }
  70. if(!solver.solve())cout<<"No solution."<<endl;
  71. else for(int i=0;i<n;i++)
  72. if(solver.mark[i*2])cout<<"C"<<endl;
  73. else if(is_young(i))cout<<"B"<<endl;
  74. else cout<<"A"<<endl;
  75. }
  76. return 0;
  77. }

训练指南 UVALive - 3713 (2-SAT)的更多相关文章

  1. 训练指南 UVALive - 3126(DAG最小路径覆盖)

    layout: post title: 训练指南 UVALive - 3126(DAG最小路径覆盖) author: "luowentaoaa" catalog: true mat ...

  2. 训练指南 UVALive - 3415(最大点独立集)

    layout: post title: 训练指南 UVALive - 3415(最大点独立集) author: "luowentaoaa" catalog: true mathja ...

  3. 训练指南 UVALive - 3989(稳定婚姻问题)

    ayout: post title: 训练指南 UVALive - 3989(稳定婚姻问题) author: "luowentaoaa" catalog: true mathjax ...

  4. 训练指南 UVALive - 4043(二分图匹配 + KM算法)

    layout: post title: 训练指南 UVALive - 4043(二分图匹配 + KM算法) author: "luowentaoaa" catalog: true ...

  5. 训练指南 UVALive - 5713(最小生成树 + 次小生成树)

    layout: post title: 训练指南 UVALive - 5713(最小生成树 + 次小生成树) author: "luowentaoaa" catalog: true ...

  6. 训练指南 UVALive - 4080(最短路Dijkstra + 边修改 + 最短路树)

    layout: post title: 训练指南 UVALive - 4080(最短路Dijkstra + 边修改 + 最短路树) author: "luowentaoaa" ca ...

  7. 训练指南 UVALive - 4287 (强连通分量+缩点)

    layout: post title: 训练指南 UVALive - 4287 (强连通分量+缩点) author: "luowentaoaa" catalog: true mat ...

  8. 训练指南 UVALive - 5135 (双连通分量)

    layout: post title: 训练指南 UVALive - 5135 (双连通分量) author: "luowentaoaa" catalog: true mathja ...

  9. 训练指南 UVALive - 3523 (双联通分量 + 二分图染色)

    layout: post title: 训练指南 UVALive - 3523 (双联通分量 + 二分图染色) author: "luowentaoaa" catalog: tru ...

随机推荐

  1. [ZJOI2008]骑士 DP dfs

    ---题解--- 题解: 观察题面可以很快发现这是一棵基环内向树(然而并没有什么用...) 再稍微思考一下,假设将这个环中的任意一点设为root,然后去掉root到下面的特殊边(即构成环的那条边),那 ...

  2. CF762D Maximum Path

    题目戳这里. 首先明确一点,数字最多往左走一次,走两次肯定是不可能的(因为只有\(3\)行). 然后我们用\(f_{i,j}\)表示前\(i\)行,第\(i\)行状态为\(j\)的最优解.(\(j\) ...

  3. js操作div的显隐

    <!DOCTYPE html><html> <head> <title> new document </title> <meta ht ...

  4. kubernetes 参考资料

    kubernetes 参考资料 非常建议先花20分钟,完成这个官方的交互式指南:https://kubernetes.io/docs/tutorials/kubernetes-basics/ 这个教程 ...

  5. video视频在结束之后回到初始状态

    目前尝试了两种解决方案,但是方案1在安卓移动端无法生效(猜测是因为移动端安卓启动的是原生的视频播放控件的原因) 方案一: 重新load资源,这种方法比较简洁,但是在安卓下不适用 video.addEv ...

  6. js中Date()对象详解

    var myDate = new Date(); myDate.getYear(); //获取当前年份(2位) myDate.getFullYear(); //获取完整的年份(4位,1970-???? ...

  7. HDU2057

    http://acm.hdu.edu.cn/showproblem.php?pid=2057 涉及到16进制内的加法,可以用%I64x直接来处理,要注意到16进制中负数是用补码来表示的.一个比较困惑的 ...

  8. 单源最短路模板_SPFA_Dijkstra(堆优化)_C++

    随手一打就是标准的SPFA,默认1号节点为出发点,当然不用 f 判断是否在队里也可以,只是这样更优化一点 void spfa() { int i,x,k; ;i<=n;i++) { d[i]=o ...

  9. andriod开发增加一个菜单

      第一步: E:\01.prj\pyscrapy\Cet4\res\menu\main.xml <menu xmlns:android="http://schemas.android ...

  10. 干货:MySQL数据库优化参考

    本文整理了一些MySQL的通用优化方法,做个简单的总结分享,旨在帮助那些没有专职MySQL DBA的企业做好基本的优化工作,至于具体的SQL优化,大部分通过加适当的索引即可达到效果,更复杂的就需要具体 ...