二分答案,2sat判定。

  1. //#pragma comment(linker, "/STACK:1024000000,1024000000")
  2. #include<cstdio>
  3. #include<cstring>
  4. #include<cstdlib>
  5. #include<algorithm>
  6. #include<iostream>
  7. #include<sstream>
  8. #include<cmath>
  9. #include<climits>
  10. #include<string>
  11. #include<map>
  12. #include<queue>
  13. #include<vector>
  14. #include<stack>
  15. #include<set>
  16. using namespace std;
  17. typedef long long ll;
  18. typedef unsigned long long ull;
  19. typedef pair<int,int> pii;
  20. #define pb(a) push(a)
  21. #define INF 0x1f1f1f1f
  22. #define lson idx<<1,l,mid
  23. #define rson idx<<1|1,mid+1,r
  24. #define PI 3.1415926535898
  25. template<class T> T min(const T& a,const T& b,const T& c) {
  26. return min(min(a,b),min(a,c));
  27. }
  28. template<class T> T max(const T& a,const T& b,const T& c) {
  29. return max(max(a,b),max(a,c));
  30. }
  31. void debug() {
  32. #ifdef ONLINE_JUDGE
  33. #else
  34.  
  35. freopen("in.txt","r",stdin);
  36. //freopen("d:\\out1.txt","w",stdout);
  37. #endif
  38. }
  39. int getch() {
  40. int ch;
  41. while((ch=getchar())!=EOF) {
  42. if(ch!=' '&&ch!='\n')return ch;
  43. }
  44. return EOF;
  45. }
  46.  
  47. const int maxn=;
  48. struct TwoSat
  49. {
  50. int n;
  51. vector<int> g[maxn*];
  52. bool mark[maxn*];
  53. int s[maxn*],c;
  54. bool dfs(int x)
  55. {
  56. if(mark[x^])return false;
  57. if(mark[x])return true;
  58. mark[x]=true;
  59. s[c++]=x;
  60. for(int i=;i<g[x].size();i++)
  61. {
  62. if(!dfs(g[x][i]))return false;
  63. }
  64. return true;
  65. }
  66. void init(int n)
  67. {
  68. this->n=n;
  69. for(int i=;i<n*;i++)
  70. g[i].clear();
  71. memset(mark,,sizeof(mark));
  72. }
  73. void add_clause(int x,int xval,int y,int yval)
  74. {
  75. x=x*+xval;
  76. y=y*+yval;
  77. g[x].push_back(y^);
  78. g[y].push_back(x^);
  79. }
  80. bool solve()
  81. {
  82. for(int i=;i<n*;i+=)
  83. {
  84. if(!mark[i]&&!mark[i+])
  85. {
  86. c=;
  87. if(!dfs(i))
  88. {
  89. while(c>)mark[s[--c]]=;
  90. if(!dfs(i+))return false;
  91. }
  92. }
  93. }
  94. return true;
  95. }
  96. };
  97.  
  98. TwoSat solver;
  99.  
  100. const int maxm=;
  101. int a[maxm],b[maxm],c[maxm];
  102. int n,m;
  103. int check(int k)
  104. {
  105. solver.init(n);
  106. for(int i=;i<k;i++)
  107. {
  108. if(c[i]==)
  109. {
  110. int x=a[i],y=b[i];
  111. solver.add_clause(x,,y,);
  112. }
  113. if(c[i]==)
  114. {
  115. int x=a[i],y=b[i];
  116. solver.add_clause(x,,y,);
  117. }
  118. if(c[i]==)
  119. {
  120. int x=a[i],y=b[i];
  121. solver.add_clause(x,,y,);
  122. solver.add_clause(x,,y,);
  123. }
  124. }
  125. return solver.solve();
  126. }
  127. int main()
  128. {
  129. int t;
  130. scanf("%d",&t);
  131. for(int ca=;ca<=t;ca++)
  132. {
  133. scanf("%d%d",&n,&m);
  134. for(int i=;i<m;i++)
  135. scanf("%d%d%d",&a[i],&b[i],&c[i]);
  136. int l=,r=m+;
  137. while(l<r)
  138. {
  139. int mid=(r+l)>>;
  140. if(check(mid))
  141. l=mid+;
  142. else r=mid;
  143. }
  144. printf("%d\n",l-);
  145. }
  146. return ;
  147. }

UVALive 5010 Go Deeper 2sat的更多相关文章

  1. LA 5010 Go Deeper 2-SAT 二分

    题意: 有\(n\)个布尔变量\(x_i\),有一个递归函数.如果满足条件\(x[a[dep]] + x[b[dep]] \neq c[dep]\),那么就再往深递归一层. 问最多能递归多少层. 分析 ...

  2. UVALive 3713 Astronauts (2-SAT,变形)

    题意: 有A,B,C三种任务,每个人必获得1个任务,大于等于平均年龄的可以选择A和C,小于平均年龄的可以选择B和C.这些人有一些是互相讨厌的,必须不能执行同任务,问能否安排他们工作?若行,输出任意一组 ...

  3. zoj3422Go Deeper(2-sat + 二分)

    题目请戳这里 题目大意: go(int dep, int n, int m) begin output the value of dep. if dep < m and x[a[dep]] + ...

  4. Go Deeper(2010成都现场赛题)(2-sat)

    G - Go Deeper Time Limit:3000MS     Memory Limit:0KB     64bit IO Format:%lld & %llu Description ...

  5. HDU 3715 Go Deeper(2-sat)

    HDU 3715 Go Deeper 题目链接 题意:依据题意那个函数,构造x数组.问最大能递归层数 思路:转化为2-sat问题,因为x仅仅能是0.1,c仅仅能是0,1.2那么问题就好办了,对于0, ...

  6. POJ 2296 Map Labeler / ZOJ 2493 Map Labeler / HIT 2369 Map Labeler / UVAlive 2973 Map Labeler(2-sat 二分)

    POJ 2296 Map Labeler / ZOJ 2493 Map Labeler / HIT 2369 Map Labeler / UVAlive 2973 Map Labeler(2-sat ...

  7. UVALive - 3713 - Astronauts(图论——2-SAT)

    Problem   UVALive - 3713 - Astronauts Time Limit: 3000 mSec Problem Description Input The input cont ...

  8. UVALive - 3211 - Now or later(图论——2-SAT)

    Problem   UVALive - 3211 - Now or later Time Limit: 9000 mSec Problem Description Input Output Sampl ...

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

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

随机推荐

  1. C++11:POD数据类型

    版权声明:本文为博主原创文章,未经博主允许不得转载. 目录(?)[+] 啥是POD类型? POD全称Plain Old Data.通俗的讲,一个类或结构体通过二进制拷贝后还能保持其数据不变,那么它就是 ...

  2. 另一种遍历Map的方式: Map.Entry 和 Map.entrySet()

    源网址: http://blog.csdn.net/mageshuai/article/details/3523116 今天看Think in java 的GUI这一章的时候,里面的TextArea这 ...

  3. 在脚本中刷新impala元信息

    刷新impala元信息 impala-shell -q 'invalidate metadata' -i hslave1 impala-shell -q 'select count(*) from p ...

  4. HDFS--(HA)初始化与启动

    1.启动zk 2.启动journalnode:         hadoop-daemons.sh start journalnode 3.格式化zkfc--让在zookeeper中生成ha节点    ...

  5. spring数据源配置

    <?xml version="1.0" encoding="UTF-8"?> <!DOCTYPE beans PUBLIC "-// ...

  6. 【转】Using Gamma 2.2

    This is a detailed description of the work with Gamma 2.2. If you are only interested in exact instr ...

  7. [主页]大牛系列01:Microsoft Research的Johannes Kopf

    时间:2015.11.21 版本:初稿 -------------------------------------------------------------------------------- ...

  8. Linux_03------Linux的基本命令

    /** * 基本命令格式 * 命令 [选项] [参数] */ /** * 目录处理命令 * mkdir dirname 创建目录 * mkdir -p dir1/dir 递归创建目录 * cd dir ...

  9. PHP生成静态页

    代码如下: <?    function makedir($mudir) //创建目录   {   $file = "./$mudir";   @mkdir($file,07 ...

  10. Linux内核分析第三周学习总结:构造一个简单的Linux系统MenuOS

    韩玉琪 + 原创作品转载请注明出处 + <Linux内核分析>MOOC课程http://mooc.study.163.com/course/USTC-1000029000 一.Linux内 ...