codeforces27D Ring Road 2
本文版权归ljh2000和博客园共有,欢迎转载,但须保留此声明,并给出原文链接,谢谢合作。
本文作者:ljh2000
作者博客:http://www.cnblogs.com/ljh2000-jump/
转载请注明出处,侵权必究,保留最终解释权!
题目链接:CF27D
正解:$2—SAT$
解题报告:
也是$2-SAT$裸题,判断线段相交之后,直接上$2-SAT$就好了。
但是判线段相交有一点难受啊,换了几种写法才搞对,最好是双向判断一下…'
注意输出方案的时候一般的做法就是将图反向之后拓扑排序,一路染色过去,我写的做法是直接根据强连通分量编号从小到大染色,每次染完之后就把另一个染为另一种颜色就好了。
//It is made by ljh2000
//有志者,事竟成,破釜沉舟,百二秦关终属楚;苦心人,天不负,卧薪尝胆,三千越甲可吞吴。
#include <algorithm>
#include <iostream>
#include <cstring>
#include <vector>
#include <cstdio>
#include <string>
#include <queue>
#include <cmath>
#include <ctime>
using namespace std;
typedef long long LL;
const int MAXN = 520;
const int MAXM = 100011;
int n,m;
inline int getint(){
int w=0,q=0; char c=getchar(); while((c<'0'||c>'9') && c!='-') c=getchar();
if(c=='-') q=1,c=getchar(); while (c>='0'&&c<='9') w=w*10+c-'0',c=getchar(); return q?-w:w;
} namespace SAT_2{
int ecnt,first[MAXN],to[MAXM],next[MAXM],dfn[MAXN],low[MAXN],bel[MAXN],top,stack[MAXN],scnt,col[MAXN];
bool pd[MAXN],hav[MAXN];
vector<int>w[MAXN];
inline void link(int x,int y){ next[++ecnt]=first[x]; first[x]=ecnt; to[ecnt]=y; }
inline void getlink(int x,int y){//连边需要连完整所有的推导关系!
x<<=1; y<<=1;
link(x,y|1); link(x|1,y);
link(y,x|1); link(y|1,x);
} inline void tarjan(int x){
dfn[x]=low[x]=++ecnt; pd[x]=1; stack[++top]=x;
for(int i=first[x];i;i=next[i]) {
int v=to[i];
if(!dfn[v]) {
tarjan(v);
low[x]=min(low[x],low[v]);
}
else if(pd[v]) low[x]=min(low[x],low[v]);
}
if(dfn[x]==low[x]) {
scnt++;
while(stack[top]!=x) {
pd[stack[top]]=0; w[scnt].push_back(stack[top]);
bel[stack[top]]=scnt;
top--;
}
pd[x]=0; bel[x]=scnt;
w[scnt].push_back(x);
top--;
}
} inline void work(){
ecnt=0;
for(int i=2;i<=(m<<1)+1;i++) if(!dfn[i]) tarjan(i);
for(int i=1;i<=m;i++) if(bel[i<<1]==bel[i<<1|1]) { puts("Impossible"); return ; }
for(int i=1;i<=scnt;i++) {
for(int j=0,ss=w[i].size();j<ss;j++) {
int v=w[i][j];
hav[v]=hav[v^1]=true;
for(int l=first[v];l;l=next[l]) {
int vv=to[l]; //if(hav[vv]) continue;
hav[vv]=hav[vv^1]=true; col[vv>>1]=(vv&1);
}
col[v>>1]=(v&1);
}
}
for(int i=1;i<=m;i++) {
if(col[i]) printf("o");
else printf("i");
}
}
} struct edge{ int x,y; }e[MAXN];
inline bool Cross(edge q,edge qq){
if(q.x<qq.x && qq.x<q.y && q.y<qq.y) return true;
return false;
} inline void work(){
using namespace SAT_2;
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); }
for(int i=1;i<=m;i++)
for(int j=1;j<=m;j++)//双向!!!
if(Cross(e[i],e[j]))
getlink(i,j);
SAT_2::work();
} int main()
{
#ifndef ONLINE_JUDGE
freopen("27.in","r",stdin);
freopen("27.out","w",stdout);
#endif
work();
return 0;
}
//有志者,事竟成,破釜沉舟,百二秦关终属楚;苦心人,天不负,卧薪尝胆,三千越甲可吞吴。
codeforces27D Ring Road 2的更多相关文章
- CodeForces 24A Ring road(dfs)
A. Ring road time limit per test 2 seconds memory limit per test 256 megabytes input standard input ...
- CodeForces 27D - Ring Road 2 构图2-sat..并输出选择方案
题意 n个数1~n按顺序围成一个圈...现在在某些两点间加边..边可以加在圈内或者圈外..问是否会发生冲突?如果不发生冲突..输每一条边是放圈内还是圈外. 题解 ...
- [CF24A]Ring road(2019-11-15考试)
题目大意 给你一个\(n\)个点的环,每条边有方向,改变第\(i\)条边的方向代价为\(w_i\),问将其改为强连通图的最小代价.\(n\leqslant100\) 题解 求出把边全部改为顺时针和全部 ...
- 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 ...
- October 26th, 2017 Week 43rd Thursday
For success, attitude is equally as important as ability. 为取得成功,态度与能力一样重要. Today I read a news about ...
- sgu 146. The Runner 取模技巧 难度:1
146. The Runner time limit per test: 0.25 sec.memory limit per test: 4096 KB input: standard inputou ...
- 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 ...
- CSP2019-S宝典
目录 CSP2019-S宝典 模板 博客 快读 vim配置 对拍 CSP2019-S注意事项 考前 考时 考后 游记 Day -2(UPDATE:2019-11-14) Day -1(UPDATE:2 ...
- Codeforces 图论题板刷(2000~2400)
前言 首先先刷完这些在说 题单 25C Roads in Berland 25D Roads not only in Berland 9E Interestring graph and Apples ...
随机推荐
- 20.Bulk Write Operations-官方文档摘录
1.有序操作列表将会串行执行,但如果在一个写操作过程出现异常错误,则不会处理剩余的任何写操作 2.无序操作列表将会并发执行,如果在一个写操作过程出现异常错误,则不影响,继续执行(并发无序) 3.对比无 ...
- 006-Hadoop Hive sql语法详解1-数据结构和Hive表建立
1.认识hive: Hive 是基于Hadoop 构建的一套数据仓库分析系统,它提供了丰富的SQL查询方式来分析存储在Hadoop 分布式文件系统中的数据,可以将结构化的数据文件映射为一张数据库表, ...
- python练习题(持续更新中。。。。。)
1.检验注册用户是否合法:需要输入用户名,校验用户名是否被注册,如已注册,提示已经注册过,没注册就可以注册:用户名不能为空:用户名长度必须在6-13位之间:最多只能输入三次. users = ['aa ...
- 玩转DOM遍历——用NodeIterator实现getElementById,getElementsByTagName方法
先声明一下DOM2中NodeIterator和TreeWalker这两类型真的只是用来玩玩的,因为性能不行遍历起来超级慢,在JS中基本用不到它们,除了<高程>上有两三页对它的讲解外,谷歌的 ...
- Ubuntu apt-get更换阿里云源
sudo vim /etc/apt/sources.list deb http://mirrors.aliyun.com/ubuntu/ trusty main restricted universe ...
- Python eval() 的使用:将字符串转换为列表,元祖,字典
eval() 函数用来执行一个字符串表达式,并返回表达式的值. 语法 以下是 eval() 方法的语法: eval(expression[, globals[, locals]]) 参数 expres ...
- python16_day24【restful、crm表构、认证】
一.restful 1. pip install djangorestframework 2.settings.py INSTALLED_APPS = ( ... 'rest_framework', ...
- Java基础知识陷阱(四)
本文发表于本人博客. 今天我们来说说关于java继承以及反射有关的问题,大家先看下下面代码,试问可以编译通过不,为什么具体说说原因? public class Test{ public static ...
- php写守护进程(转载 http://blog.csdn.net/tengzhaorong/article/details/9764655)
守护进程(Daemon)是运行在后台的一种特殊进程.它独立于控制终端并且周期性地执行某种任务或等待处理某些发生的事件.守护进程是一种很有用的进程.php也可以实现守护进程的功能. 1.基本概念 进程 ...
- javascript 类型 内存 对象
var box =0 function test() { alert(box) //全局 }