1. /*
  2. 思路:http://blog.csdn.net/string_yi/article/details/12686873
  3. hdu 1814 输出字典序最小的2-sat
  4. */
  5. #include<stdio.h>
  6. #include<string.h>
  7. #include<math.h>
  8. #define N 16100
  9. #define NN 210000
  10. struct node {
  11. int v,w,next;
  12. }bian[NN*2];
  13. int head[N],cnt,yong,color[N],ans[N];
  14. void init() {
  15. yong=0;
  16. memset(head,-1,sizeof(head));
  17. yong=0;
  18. memset(color,0,sizeof(color));
  19. }
  20. void addedge(int u,int v) {
  21. bian[yong].v=v;
  22. bian[yong].next=head[u];
  23. head[u]=yong++;
  24. }
  25. int dfs(int v)
  26. {
  27. if(color[v]==2)
  28. return 0;
  29. if(color[v]==1)
  30. return 1;
  31. ans[cnt++]=v;
  32. color[v]=1;
  33. color[v^1]=2;
  34. for(int i=head[v];i!=-1;i=bian[i].next)
  35. if(!dfs(bian[i].v))
  36. return 0;
  37. return 1;
  38. }
  39. void slove(int n) {
  40. int i,j;
  41. for(i=0;i<2*n;i++) {
  42. if(color[i])continue;
  43. cnt=0;
  44. if(!dfs(i)) {
  45. for(j=0;j<cnt;j++) {
  46. color[ans[j]]=0;
  47. color[ans[j]^1]=0;
  48. }
  49. if(!dfs(i^1)) {
  50. printf("NIE\n");
  51. return ;
  52. }
  53. }
  54. }
  55. for(i=0;i<2*n;i+=2) {
  56. if(color[i]==1)
  57. printf("%d\n",i+1);
  58. else
  59. printf("%d\n",i+2);
  60. }
  61. }
  62. int main() {
  63. int n,aa,bb,m;
  64. while(scanf("%d%d",&n,&m)!=EOF) {
  65. init();
  66. while(m--) {
  67. scanf("%d%d",&aa,&bb);
  68. aa--;bb--;
  69. addedge(aa,bb^1);
  70. addedge(bb,aa^1);
  71. }
  72. slove(n);
  73. }
  74. return 0;}
  1. 输出任意次序<pre name="code" class="cpp">/*
  2. hit 1917输出任意次序的2-sat
  3. */
  4. #include <iostream>
  5. #include <cstdio>
  6. #include <cstring>
  7. #include <algorithm>
  8. #include <cmath>
  9. #include <queue>
  10. #include <vector>
  11. using namespace std;
  12. const int maxn=16005;
  13. const int maxm=200005;
  14. int head[maxn],next[maxm],to[maxm];
  15. int dfn[maxn],low[maxn],stk[maxn],scc[maxn],ind[maxn],vis[maxn];
  16. int color[maxn],f[maxn];
  17. int tot,top,cnt,id;
  18. vector<int> dag[maxn];
  19. void addEdage(int u,int v)
  20. {
  21. next[tot]=head[u],to[tot]=v,head[u]=tot++;
  22. }
  23. void init()
  24. {
  25. memset(head,-1,sizeof(head));
  26. memset(vis,0,sizeof(vis));
  27. memset(dfn,0,sizeof(dfn));
  28. memset(ind,0,sizeof(ind));
  29. memset(color,0,sizeof(color));
  30. tot=top=cnt=id=0;
  31. }
  32. void tarjan(int v)
  33. {
  34. dfn[v]=low[v]=++cnt;
  35. vis[v]=1;
  36. stk[++top]=v;
  37. for(int i=head[v];i!=-1;i=next[i])
  38. {
  39. int u=to[i];
  40. if(!dfn[u])
  41. {
  42. tarjan(u);
  43. low[v]=min(low[v],low[u]);
  44. }
  45. else if(vis[u])
  46. low[v]=min(low[v],dfn[u]);
  47. }
  48. if(low[v]==dfn[v])
  49. {
  50. id++;
  51. while(true)
  52. {
  53. int u=stk[top--];
  54. vis[u]=0;
  55. scc[u]=id;
  56. if(u==v)break;
  57. }
  58. }
  59. }
  60. void buildDag(int n)
  61. {
  62. for(int u=0;u<2*n;u++)
  63. for(int i=head[u];i!=-1;i=next[i])
  64. {
  65. int v=to[i];
  66. if(scc[v]!=scc[u])
  67. {
  68. dag[scc[v]].push_back(scc[u]);
  69. ind[scc[u]]++;
  70. }
  71. }
  72. }
  73. void topsort()
  74. {
  75. queue<int> q;
  76. for(int i=1;i<=id;i++)
  77. if(!ind[i])q.push(i);
  78. while(!q.empty())
  79. {
  80. int u=q.front();
  81. q.pop();
  82. if(!color[u])
  83. color[u]=1,color[f[u]]=2;
  84. for(int i=0;i<(int)dag[u].size();i++)
  85. {
  86. int v=dag[u][i];
  87. ind[v]--;
  88. if(!ind[v])q.push(v);
  89. }
  90. }
  91. }
  92. void solve(int n)
  93. {
  94. for(int i=0;i<2*n;i++)
  95. if(!dfn[i])tarjan(i);
  96. for(int i=0;i<2*n;i+=2)
  97. if(scc[i]==scc[i+1])
  98. {
  99. printf("NIE\n");
  100. return;
  101. }
  102. else f[scc[i]]=scc[i+1],f[scc[i+1]]=scc[i];
  103. for(int i=0;i<=id;i++)
  104. dag[i].clear();
  105. buildDag(n);
  106. topsort();
  107. for(int i=0;i<2*n;i+=2)
  108. {
  109. if(color[scc[i]]==1)
  110. printf("%d\n",i+1);
  111. else
  112. printf("%d\n",i+2);
  113. }
  114. }
  115. int main()
  116. {
  117. // freopen("in.txt","r",stdin);
  118. int n,m;
  119. while(~scanf("%d%d",&n,&m))
  120. {
  121. init();
  122. for(int i=0,a,b;i<m;i++)
  123. {
  124. scanf("%d%d",&a,&b);
  125. a--,b--;
  126. addEdage(a,b^1);
  127. addEdage(b,a^1);
  128. }
  129. solve(n);
  130. }
  131. return 0;
  132. }
  1.  

hdu 1814 2-sat 输出字典最小的和任意序列的 模板题的更多相关文章

  1. HDU 1814 Peaceful Commission

    2-SAT,输出字典序最小的解,白书模板. //TwoSAT输出字典序最小的解的模板 //注意:0,1是一组,1,2是一组..... #include<cstdio> #include&l ...

  2. HDU 3264 区间内的最大最小之差

    题目链接:http://poj.org/problem?id=3264 题目大意:在给定一堆牛的数量以及其高度的时候,每次给定一段区间,求这个区间内最高的牛和最矮的牛的高度之差为多少. 可以直接利用R ...

  3. HDU 2544 最短路 【Dijkstra模板题】

    传送门:http://acm.hdu.edu.cn/showproblem.php?pid=2544 思路:最短路的模板题 Dijkstra 算法是一种类似于贪心的算法,步骤如下: 1.当到一个点时, ...

  4. Peaceful Commission HDU - 1814(输出最小的一组解)

    Description 根据宪法,Byteland民主共和国的公众和平委员会应该在国会中通过立法程序来创立. 不幸的是,由于某些党派代表之间的不和睦而使得这件事存在障碍. 此委员会必须满足下列条件: ...

  5. HDU 1385 Minimum Transport Cost (输出字典序最小路径)【最短路】

    <题目链接> 题目大意:给你一张图,有n个点,每个点都有需要缴的税,两个直接相连点之间的道路也有需要花费的费用.现在进行多次询问,给定起点和终点,输出给定起点和终点之间最少花费是多少,并且 ...

  6. hdu 1814 字典序最小的2sat(暴力深搜)

    题意:      题意就是最基础的2sat,关系只有矛盾关系,然后二选一,关键是这个题目是输出字典序最小的那组解. 思路:      输出字典序最小,用强连通那个实现不了(起码没看到有人实现),其实我 ...

  7. HDU 1814 Peaceful Commission / HIT 1917 Peaceful Commission /CJOJ 1288 和平委员会(2-sat模板题)

    HDU 1814 Peaceful Commission / HIT 1917 Peaceful Commission /CJOJ 1288 和平委员会(2-sat模板题) Description T ...

  8. 输入n个整数,输出其中最小的k个

    描述 输入n个整数,输出其中最小的k个. 详细描述: 接口说明 原型: bool GetMinK(unsignedint uiInputNum, int * pInputArray, unsigned ...

  9. 排序,求几个最值问题,输入n个整数,输出其中最小的k个元素。

    看完两个求最大值算法之后的一些感想. 如果想直接看算法的可以跳过.但是我觉得我这些想法还是比较有用的,至少对我将来的算法设计是这样的. 算法的功能越强大,必然意味着速度慢,因为根据丛林法则,那种慢又功 ...

随机推荐

  1. "HIBERNATE_SEQUENCE" does not exist问题处理

    JavaWeb应用在MySQL环境下可以正常运行,数据迁移至Oracle或者人大金仓后应用运行爆出如下错误: 严重: Servlet.service() for servlet [JeeCmsAdmi ...

  2. 比较C#中几种常见的复制字节数组方法的效率[转]

    [原文链接] 在日常编程过程中,我们可能经常需要Copy各种数组,一般来说有以下几种常见的方法:Array.Copy,IList<T>.Copy,BinaryReader.ReadByte ...

  3. CentOS安装GlassFish4.0 配置JDBC连接MySQL

    转自:http://linux.it.net.cn/CentOS/course/2014/0724/3319.html 版本glassfish-4.0.zip 1.解压,拷贝到指定安装路径   unz ...

  4. 死磕 java魔法类之Unsafe解析

    问题 (1)Unsafe是什么? (2)Unsafe只有CAS的功能吗? (3)Unsafe为什么是不安全的? (4)怎么使用Unsafe? 简介 本章是java并发包专题的第一章,但是第一篇写的却不 ...

  5. vim插件minibuf配置

    1.去下载网站下载minibufexpl.vim文件放入到~/vim/plugins中,有的系统路径是~/.vim/plugins; 下载网址如下 https://www.vim.org/script ...

  6. 处理不同jQuery版本的兼容性问题

    众所周知,jquery版本很多,而且有些版本的冲突也非常明显,有一些网上流传的很实用的插件是用A版本写的,但是要实现另各功能又必須用B版本.所以实现版本之間的和平相处很重要. 1.这里介绍一个函数,可 ...

  7. Laravel Mix编译前端资源

    目前项目是使用的vue+laravel来写的,其中laravel和vue分别放了一个目录,但是这样有个问题,那就是vue需要经常更新,不然运行项目会经常出现各种问题,这里就看了看laravel的文档, ...

  8. oracle 表之间的连接

    排序 - - 合并连接(Sort Merge Join, SMJ): a) 对于非等值连接,这种连接方式的效率是比较高的. b) 如果在关联的列上都有索引,效果更好. c) 对于将2个较大的row s ...

  9. 09CSS高级定位

    CSS高级定位 定位方式——position position:static|absolute|relative static表示为静态定位,是默认设置.  absolute表示绝对定位,与下位置属 ...

  10. python selenium定位总结(转)

    转自:http://www.cnblogs.com/yufeihlf/p/5717291.html 父子定位元素 查找有父亲元素的标签名为span,它的所有标签名叫input的子元素 find_ele ...