大白书例题

  1. #include <iostream>
  2. #include <cstdio>
  3. #include <sstream>
  4. #include <cstring>
  5. #include <map>
  6. #include <set>
  7. #include <vector>
  8. #include <stack>
  9. #include <queue>
  10. #include <algorithm>
  11. #include <cmath>
  12. #define rap(a, n) for(int i=a; i<=n; i++)
  13. #define MOD 2018
  14. #define LL long long
  15. #define ULL unsigned long long
  16. #define Pair pair<int, int>
  17. #define mem(a, b) memset(a, b, sizeof(a))
  18. #define _ ios_base::sync_with_stdio(0),cin.tie(0)
  19. //freopen("1.txt", "r", stdin);
  20. using namespace std;
  21. const int maxn = ;
  22.  
  23. struct TwoSAT {
  24. int n;
  25. vector<int> G[maxn*];
  26. bool mark[maxn*];
  27. int S[maxn*], c;
  28.  
  29. bool dfs(int x) {
  30. if (mark[x^]) return false;
  31. if (mark[x]) return true;
  32. mark[x] = true;
  33. S[c++] = x;
  34. for (int i = ; i < G[x].size(); i++)
  35. if (!dfs(G[x][i])) return false;
  36. return true;
  37. }
  38.  
  39. void init(int n) {
  40. this->n = n;
  41. for (int i = ; i < n*; i++)
  42. G[i].clear();
  43. memset(mark, , sizeof(mark));
  44. }
  45.  
  46. void add_clause(int x, int xval, int y, int yval) {
  47. x = x * + xval;
  48. y = y * + yval;
  49. G[x^].push_back(y);
  50. G[y^].push_back(x);
  51. }
  52.  
  53. bool solve() {
  54. for(int i = ; i < n*; i += )
  55. if(!mark[i] && !mark[i+]) {
  56. c = ;
  57. if(!dfs(i)) {
  58. while(c > ) mark[S[--c]] = false;
  59. if(!dfs(i+)) return false;
  60. }
  61. }
  62. return true;
  63. }
  64. };
  65.  
  66. TwoSAT solver;
  67.  
  68. int n, m, total_age, age[maxn];
  69.  
  70. int is_young(int x) {
  71. return age[x] * n < total_age;
  72. }
  73.  
  74. int main() {
  75. while(scanf("%d%d", &n, &m) == && n) {
  76. total_age = ;
  77. for(int i = ; i < n; i++) {
  78. scanf("%d", &age[i]);
  79. total_age += age[i];
  80. }
  81.  
  82. solver.init(n);
  83. for(int i = ; i < m; i++) {
  84. int a, b;
  85. scanf("%d%d", &a, &b);
  86. a--;
  87. b--;
  88. if(a == b) continue;
  89. solver.add_clause(a, , b, ); //不能同时为真
  90. if(is_young(a) == is_young(b))
  91. solver.add_clause(a, , b, ); //不能同时为假
  92. }
  93.  
  94. if(!solver.solve()) printf("No solution.\n");
  95. else {
  96. for(int i = ; i < n; i++)
  97. if(solver.mark[i*]) printf("C\n"); //设的是选c为真 因为年龄小和年龄大的c是相同的 a和b不好直接判断 所以只要c被标记了 那么就是选了c
  98. else if(is_young(i)) printf("B\n"); //如果没选c 那就判断是不是年龄小的
  99. else printf("A\n");
  100. }
  101. }
  102. return ;
  103. }

Astronauts UVALive - 3713(2-SAT)的更多相关文章

  1. 训练指南 UVALive - 3713 (2-SAT)

    layout: post title: 训练指南 UVALive - 3713 (2-SAT) author: "luowentaoaa" catalog: true mathja ...

  2. UVALive - 3211 (2-SAT + 二分)

    layout: post title: 训练指南 UVALive - 3211 (2-SAT + 二分) author: "luowentaoaa" catalog: true m ...

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

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

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

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

  5. UVALive - 6436 —(DFS+思维)

    题意:n个点连成的生成树(n个点,n-1条边,点与点之间都连通),如果某个点在两点之间的路径上,那这个点的繁荣度就+1,问你在所有点中,最大繁荣度是多少?就比如上面的图中的C点,在A-B,A-D,A- ...

  6. Remember the Word UVALive - 3942(dp+trie)

    题意: 给S个不同的单词和一个长字符串 问将其分解为若干个单词有多少种方法(单词可重复使用) 解析: dp[i]表示在这个字符串中以某个位置i为起点的 的一段子字符串 则这个子字符串若存在某个前缀恰好 ...

  7. UVALive 5873 (几何+思维)

    唉 被秀了... 还是太弱,说好的数形结合呢,列个式子出来后就被吓到了,然后就懵逼了. 题意: 有一条狗,从原点出发,沿n个向量走,每个向量只走一次,沿着一个向量(x,y)走时,既可以往(x,y)方向 ...

  8. POJ 3678 Katu Puzzle(2 - SAT) - from lanshui_Yang

    Description Katu Puzzle is presented as a directed graph G(V, E) with each edge e(a, b) labeled by a ...

  9. 学习笔记(two sat)

    关于two sat算法 两篇很好的论文由对称性解2-SAT问题(伍昱), 赵爽 2-sat解法浅析(pdf). 一些题目的题解 poj 3207 poj 3678 poj 3683 poj 3648 ...

随机推荐

  1. day2 HTML - body

    <body>内常用标签 1.基本标签 所有标签分为: #  块级标签: div(白板),H系列(加大加粗),p标签(段落和段落之间有间距) # 行内标签: span(白板) 1. 图标,  ...

  2. ElasticSearch 聚合查询百分比

    这里用的是es5.6.9 bucket_script :它执行一个脚本,该脚本可以对多桶聚合中的指定度量执行每桶计算,指定的度量标准必须为数字,并且脚本必须返回数值. 官方语法 https://www ...

  3. Intellij IDEA破解激活

    本文使用破解补丁激活Intellij IDEA 第一步:下载破解补丁(http://idea.lanyus.com/) 第二步:将下载好的补丁放置磁盘的任意位置,下面是我放置的位置 第三步:打开Int ...

  4. PHP双向队列

    假定队列的左边为头部,右边为尾部 <?php class myDeque { private $deque=array(); /** *头部进队列 */ public function lPus ...

  5. 设计模式C++实现(1)——策略(Strategy)模式

    目录 策略模式 应用案例 实现的关键 Talk is cheap,let's See The Code 设计思想 参考 策略模式 策略模式定义了一系列算法和行为(也就是策略),他们可以在运行时相互替换 ...

  6. Python基础入门(迭代器和生成器)

    1 Python迭代器 迭代器是一个可以记住遍历的位置的对象. 迭代器对象从集合的第一个元素开始访问,直到所有的元素被访问完结束. 迭代器只能往前不会后退. 迭代器有两个基本的方法:iter() 和 ...

  7. Dsniff简介

    原文发表于:2010-09-25 转载至cu于:2012-07-21 前两天因为看局域网安全的视频中介绍dsniff,也想自己安装下来看看效果.简单的使用没什么难的(高级使用就需要研究文档了),但是安 ...

  8. Python20-Day02

    1.数据 数据为什么要分不同的类型 数据是用来表示状态的,不同的状态就应该用不同类型的数据表示: 数据类型 数字(整形,长整形,浮点型,复数),字符串,列表,元组,字典,集合 2.字符串 1.按索引取 ...

  9. linux-ubuntu配置通过22端口远程连接

    当安装好ubuntu后获取到对应主机的ip地址,要想通过类似xshell这样的远程连接工具连接到ubuntu主机,需要在你刚刚安装好的ubuntu主机上安装openssh这个软件,才能通过远程来连接u ...

  10. Scrum立会报告+燃尽图(Beta阶段第五次)

    此作业要求参见:https://edu.cnblogs.com/campus/nenu/2018fall/homework/2387 项目地址:https://coding.net/u/wuyy694 ...