本文版权归ljh2000和博客园共有,欢迎转载,但须保留此声明,并给出原文链接,谢谢合作。

本文作者:ljh2000
作者博客:http://www.cnblogs.com/ljh2000-jump/
转载请注明出处,侵权必究,保留最终解释权!

题目链接:CF27D

正解:$2—SAT$

解题报告:

  也是$2-SAT$裸题,判断线段相交之后,直接上$2-SAT$就好了。

  但是判线段相交有一点难受啊,换了几种写法才搞对,最好是双向判断一下…'

  注意输出方案的时候一般的做法就是将图反向之后拓扑排序,一路染色过去,我写的做法是直接根据强连通分量编号从小到大染色,每次染完之后就把另一个染为另一种颜色就好了。

  1. //It is made by ljh2000
  2. //有志者,事竟成,破釜沉舟,百二秦关终属楚;苦心人,天不负,卧薪尝胆,三千越甲可吞吴。
  3. #include <algorithm>
  4. #include <iostream>
  5. #include <cstring>
  6. #include <vector>
  7. #include <cstdio>
  8. #include <string>
  9. #include <queue>
  10. #include <cmath>
  11. #include <ctime>
  12. using namespace std;
  13. typedef long long LL;
  14. const int MAXN = 520;
  15. const int MAXM = 100011;
  16. int n,m;
  17. inline int getint(){
  18. int w=0,q=0; char c=getchar(); while((c<'0'||c>'9') && c!='-') c=getchar();
  19. if(c=='-') q=1,c=getchar(); while (c>='0'&&c<='9') w=w*10+c-'0',c=getchar(); return q?-w:w;
  20. }
  21.  
  22. namespace SAT_2{
  23. int ecnt,first[MAXN],to[MAXM],next[MAXM],dfn[MAXN],low[MAXN],bel[MAXN],top,stack[MAXN],scnt,col[MAXN];
  24. bool pd[MAXN],hav[MAXN];
  25. vector<int>w[MAXN];
  26. inline void link(int x,int y){ next[++ecnt]=first[x]; first[x]=ecnt; to[ecnt]=y; }
  27. inline void getlink(int x,int y){//连边需要连完整所有的推导关系!
  28. x<<=1; y<<=1;
  29. link(x,y|1); link(x|1,y);
  30. link(y,x|1); link(y|1,x);
  31. }
  32.  
  33. inline void tarjan(int x){
  34. dfn[x]=low[x]=++ecnt; pd[x]=1; stack[++top]=x;
  35. for(int i=first[x];i;i=next[i]) {
  36. int v=to[i];
  37. if(!dfn[v]) {
  38. tarjan(v);
  39. low[x]=min(low[x],low[v]);
  40. }
  41. else if(pd[v]) low[x]=min(low[x],low[v]);
  42. }
  43. if(dfn[x]==low[x]) {
  44. scnt++;
  45. while(stack[top]!=x) {
  46. pd[stack[top]]=0; w[scnt].push_back(stack[top]);
  47. bel[stack[top]]=scnt;
  48. top--;
  49. }
  50. pd[x]=0; bel[x]=scnt;
  51. w[scnt].push_back(x);
  52. top--;
  53. }
  54. }
  55.  
  56. inline void work(){
  57. ecnt=0;
  58. for(int i=2;i<=(m<<1)+1;i++) if(!dfn[i]) tarjan(i);
  59. for(int i=1;i<=m;i++) if(bel[i<<1]==bel[i<<1|1]) { puts("Impossible"); return ; }
  60. for(int i=1;i<=scnt;i++) {
  61. for(int j=0,ss=w[i].size();j<ss;j++) {
  62. int v=w[i][j];
  63. hav[v]=hav[v^1]=true;
  64. for(int l=first[v];l;l=next[l]) {
  65. int vv=to[l]; //if(hav[vv]) continue;
  66. hav[vv]=hav[vv^1]=true; col[vv>>1]=(vv&1);
  67. }
  68. col[v>>1]=(v&1);
  69. }
  70. }
  71. for(int i=1;i<=m;i++) {
  72. if(col[i]) printf("o");
  73. else printf("i");
  74. }
  75. }
  76. }
  77.  
  78. struct edge{ int x,y; }e[MAXN];
  79. inline bool Cross(edge q,edge qq){
  80. if(q.x<qq.x && qq.x<q.y && q.y<qq.y) return true;
  81. return false;
  82. }
  83.  
  84. inline void work(){
  85. using namespace SAT_2;
  86. n=getint(); m=getint(); for(int i=1;i<=m;i++) { e[i].x=getint(),e[i].y=getint(); if(e[i].x>e[i].y) swap(e[i].x,e[i].y); }
  87. for(int i=1;i<=m;i++)
  88. for(int j=1;j<=m;j++)//双向!!!
  89. if(Cross(e[i],e[j]))
  90. getlink(i,j);
  91. SAT_2::work();
  92. }
  93.  
  94. int main()
  95. {
  96. #ifndef ONLINE_JUDGE
  97. freopen("27.in","r",stdin);
  98. freopen("27.out","w",stdout);
  99. #endif
  100. work();
  101. return 0;
  102. }
  103. //有志者,事竟成,破釜沉舟,百二秦关终属楚;苦心人,天不负,卧薪尝胆,三千越甲可吞吴。

  

codeforces27D Ring Road 2的更多相关文章

  1. CodeForces 24A Ring road(dfs)

    A. Ring road time limit per test 2 seconds memory limit per test 256 megabytes input standard input ...

  2. CodeForces 27D - Ring Road 2 构图2-sat..并输出选择方案

        题意             n个数1~n按顺序围成一个圈...现在在某些两点间加边..边可以加在圈内或者圈外..问是否会发生冲突?如果不发生冲突..输每一条边是放圈内还是圈外.     题解 ...

  3. [CF24A]Ring road(2019-11-15考试)

    题目大意 给你一个\(n\)个点的环,每条边有方向,改变第\(i\)条边的方向代价为\(w_i\),问将其改为强连通图的最小代价.\(n\leqslant100\) 题解 求出把边全部改为顺时针和全部 ...

  4. English trip V1 - B 15. Giving Personal Information 提供个人信息 Teacher:Solo Key: Do/Does

    In this lesson you will learn to answer simple questions about yourself.  本节课讲学到回答关于自己的一些简单问题 课上内容(L ...

  5. October 26th, 2017 Week 43rd Thursday

    For success, attitude is equally as important as ability. 为取得成功,态度与能力一样重要. Today I read a news about ...

  6. sgu 146. The Runner 取模技巧 难度:1

    146. The Runner time limit per test: 0.25 sec.memory limit per test: 4096 KB input: standard inputou ...

  7. CSP2019-S游记

    目录 CSP2019-S游记 Day -2(UPDATE:2019-11-14) Day -1(UPDATE:2019-11-15) Day 1(UPDATE:2019-11-16) Day 2(UP ...

  8. CSP2019-S宝典

    目录 CSP2019-S宝典 模板 博客 快读 vim配置 对拍 CSP2019-S注意事项 考前 考时 考后 游记 Day -2(UPDATE:2019-11-14) Day -1(UPDATE:2 ...

  9. Codeforces 图论题板刷(2000~2400)

    前言 首先先刷完这些在说 题单 25C Roads in Berland 25D Roads not only in Berland 9E Interestring graph and Apples ...

随机推荐

  1. KVm中EPT逆向映射机制分析

    2017-05-30 前几天简要分析了linux remap机制,虽然还有些许瑕疵,但总算大致分析的比较清楚.今天分析下EPT下的逆向映射机制.EPT具体的工作流程可参考前面博文,本文对于EPT以及其 ...

  2. CListCtrl控件使用方法总结

    今天第一次用CListCtrl控件,遇到不少问题,查了许多资料,现将用到的一些东西总结如下: 以下未经说明,listctrl默认view 风格为report 相关类及处理函数 MFC:CListCtr ...

  3. Linux more命令

    more命令类似与cat命令,却比cat命令强大,它以全屏幕的方式按页显示文本文件的内容,支持vi中的关键字定位操作. 1.快捷键 space, z 向下翻页b,ctrl+b       向上翻页 E ...

  4. Linux touch命令

    touch命令不常用,一般用于更改文件时间戳,或创建一个空文件 命令选项 -a:只更改访问时间 -c:--no-create 不创建任何文件 -d:--date=字符串 使用指定字符串表示时间而非当前 ...

  5. python服务器环境搭建——安装相关软件

    在上一篇我们在本地的虚拟服务器上安装好CentOS7后,我们的python web服务.自定义的python service或python脚本需要在服务器上运行,还需要在服务器安装各种相关的软件才行, ...

  6. JavaWeb—Base64编码(转载)

    基本概念 Base64这个术语最初是在“MIME内容传输编码规范”中提出的.Base64不是一种加密算法,虽然编码后的字符串看起来有点加密的赶脚.它实际上是一种“二进制到文本”的编码方法,它能够将给定 ...

  7. Springboot+shiro配置笔记+错误小结

    软件152 尹以操 springboot不像springmvc,它没有xml配置文件,那该如何配置shiro呢,其实也不难,用java代码+注解来解决这个问题.仅以此篇记录我对shiro的学习,如有对 ...

  8. POJ1088:滑雪(简单dp)

    题目链接:  http://poj.org/problem?id=1088 题目要求: 一个人可以从某个点滑向上下左右相邻四个点之一,当且仅当高度减小.求可以滑落的最长长度. 题目解析: 首先要先排一 ...

  9. centOS7下安装laravel + composer

    1.wget https://dl.laravel-china.org/composer.phar -O /usr/local/bin/composer chmod a+x /usr/local/bi ...

  10. PHP程序执行时间过长,超时了怎么办

    解决办法:修改php.ini文件,把最大的执行时间改为0,0表示不限制时间. max_execution_time = 0