这是一道裸的2-Sat,只要考虑矛盾条件的判断就好了。

矛盾判断:

对于婚礼现场 x 和 y,x 的第一段可以和 y 的第一段或者第二段矛盾,同理,x 的第二段可以和 y 的第一段或者第二段矛盾,条件是 x 的 1或2 段与 y 的 1或2 段有重合,那么选了 x 某一段就不能选择其矛盾的那一段,那就只能选择 y 中的另一段,建立一条 x (u)-> y ( v的对立 ),同理,x (u的对立)<- y ( v ) 。

真的,2-sat千万不要用邻接链表!!!卡死卡死卡死!!!稠密图!!!不要用!!!

我WA和RE和MLE都怪邻接矩阵!!!!!!!!!!!!!!!!!!!!!!!!!

总代码:

下面第一个代码纪念邻接矩阵这个垃圾的Failure!

 #include <cstdio>
#include <stack>
#include <queue>
#include <cstring> const int N = + ; std :: stack < int > s ;
std :: queue < int > q ; struct ConfirmedHY {
int st ;
int en ;
}
dot [ N ] ; int head [ N ] , nxt [ N ] , to [ N ] , cn ;
int headx [ N ] , nxtx [ N ] , tox [ N ] , cnn ;
int dfn [ N ] , low [ N ] , belong [ N ] , cnt , cnx ;
int ind [ N ] , op [ N ] , color [ N ] ;
int sth , stm , enh , enm , len , n ;
bool ins [ N ] ; int minx ( int a , int b ) {
return a > b ? b : a ;
} void create ( int u , int v ) {
cn ++ ;
to [ cn ] = v ;
nxt [ cn ] = head [ u ] ;
head [ u ] = cn ;
} void create_x ( int u , int v ) {
cnn ++ ;
tox [ cnn ] = v ;
nxtx [ cnn ] = headx [ u ] ;
headx [ u ] = cnn ;
} bool conflict ( int id1 , int id2 ) {
int st1 = dot [ id1 ] . st , en1 = dot [ id1 ] . en ;
int st2 = dot [ id2 ] . st , en2 = dot [ id2 ] . en ;
if ( st1 >= en2 || st2 >= en1 )
return false ;
return true ;
} void rebuild ( ) { for ( int i = ; i <= * n ; i ++ ) {
int tmp1 = belong [ i ] ;
for ( int j = head [ i ] ; j ; j = nxt [ j ] ) {
int tmp2 = belong [ to [ j ] ] ;
if ( tmp1 != tmp2 ) {
create_x ( tmp2 , tmp1 ) ;
ind [ tmp1 ] ++ ;
}
}
} } void opp ( ) {
for ( int i = ; i <= n ; i ++ ) {
op [ belong [ i ] ] = belong [ i + n ] ;
op [ belong [ i + n ] ] = belong [ i ] ;
}
} bool judge () {
for ( int i = ; i <= n ; i ++ )
if ( belong [ i ] == belong [ i + n ] )
return true ;
return false ;
} void topsort ( ) {
for ( int i = ; i <= cnx ; i ++ )
if ( ind [ i ] == )
q . push ( i ) ;
while ( ! q . empty ( ) ) {
int tmp = q . front ( ) ;
q . pop ( ) ;
if ( color [ tmp ] == )
color [ tmp ] = , color [ op [ tmp ] ] = ;
//if ( color [ tmp ] == 2 ) continue ;
for ( int i = headx [ tmp ] ; i ; i = nxtx [ i ] ) {
ind [ tox [ i ] ] -- ;
if ( ! ind [ tox [ i ] ] )
q . push ( tox [ i ] ) ;
}
}
} void tarjan ( int x ) {
cnt ++ ;
dfn [ x ] = low [ x ] = cnt ;
ins [ x ] = true ; s . push ( x ) ;
for ( int i = head [ x ] ; i ; i = nxt [ i ] ) {
int v = to [ i ] ;
if ( ! dfn [ v ] ) {
tarjan ( v ) ;
low [ x ] = minx ( low [ x ] , low [ v ] ) ;
}
else if ( ins [ v ] ) low [ x ] = minx ( low [ x ] , dfn [ v ] ) ;
}
if ( dfn [ x ] == low [ x ] ) {
cnx ++ ;
while ( ) {
int tmp = s . top ( ) ;
s . pop ( ) ;
ins [ tmp ] = false ;
belong [ tmp ] = cnx ;
if ( x == tmp ) break ;
}
}
} int main ( ) { while ( scanf ( "%d" , & n ) == ) { cnx = ; cnt = ; cn = ; cnn = ;
memset ( head , , sizeof ( head ) ) ;
memset ( nxt , , sizeof ( nxt ) ) ;
memset ( to , , sizeof ( to ) ) ;
memset ( headx , , sizeof ( headx ) ) ;
memset ( nxtx , , sizeof ( nxtx ) ) ;
memset ( tox , , sizeof ( tox ) ) ;
memset ( belong , , sizeof ( belong ) ) ;
memset ( dfn , , sizeof ( dfn ) ) ;
memset ( ins , false , sizeof ( ins ) ) ;
memset ( low , , sizeof ( low ) ) ;
memset ( dot , , sizeof ( dot ) ) ;
memset ( ind , , sizeof ( ind ) ) ;
memset ( op , , sizeof ( op ) ) ;
memset ( color , , sizeof ( color ) ) ; for ( int i = ; i <= n ; i ++ ) {
scanf ( "%d:%d %d:%d %d" , & sth , & stm , & enh , & enm , & len ) ;
dot [ i ] . st = sth * + stm ;
dot [ i ] . en = sth * + stm + len ;
dot [ i + n ] . st = enh * + enm - len ;
dot [ i + n ] . en = enh * + enm ;
} for ( int i = ; i <= n ; i ++ )
for ( int j = ; j <= n ; j ++ )
if ( i != j ) {
if ( conflict ( i , j ) ) {
create ( i , j + n ) ;
create ( j , i + n ) ;
}
if ( conflict ( i , j + n ) ) {
create ( i , j ) ;
create ( j + n , i + n ) ;
}
if ( conflict ( i + n , j ) ) {
create ( i + n , j + n ) ;
create ( j , i ) ;
}
if ( conflict ( i + n , j + n ) ) {
create ( i + n , j ) ;
create ( j + n , i ) ;
}
} for ( int i = ; i <= * n ; i ++ )
if ( ! dfn [ i ] )
tarjan ( i ) ; if ( judge ( ) ) {
printf ( "NO\n" ) ;
continue ;
}
else printf ( "YES\n" ) ; rebuild ( ) ; opp ( ) ; topsort ( ) ; for ( int i = ; i <= n ; i ++ )
if ( color [ belong [ i ] ] == )
printf ( "%.2d:%.2d %.2d:%.2d\n" , dot [ i ] . st / , dot [ i ] . st % , dot [ i ] . en / , dot [ i ] . en % ) ;
else
printf ( "%.2d:%.2d %.2d:%.2d\n" , dot [ i + n ] . st / , dot [ i + n ] . st % , dot [ i + n ] . en / , dot [ i + n ] . en % ) ;
} return ;
}

玄学MLE

下面是AC代码

 #include <cstdio>
#include <stack>
#include <queue>
#include <cstring> const int N = + ; std :: stack < int > s ;
std :: queue < int > q ;
std :: vector < int > g [ N ] , rg [ N ] ; struct ConfirmedHY {
int st ;
int en ;
}
dot [ N ] ; int dfn [ N ] , low [ N ] , belong [ N ] , cnt , cnx ;
int ind [ N ] , op [ N ] , color [ N ] ;
int sth , stm , enh , enm , len , n ;
bool ins [ N ] ; int minx ( int a , int b ) {
return a > b ? b : a ;
} void create ( int u , int v ) {
g [ u ] . push_back ( v ) ;
} void create_x ( int u , int v ) {
rg [ u ] . push_back ( v ) ;
} bool conflict ( int id1 , int id2 ) {
int st1 = dot [ id1 ] . st , en1 = dot [ id1 ] . en ;
int st2 = dot [ id2 ] . st , en2 = dot [ id2 ] . en ;
if ( st1 >= en2 || st2 >= en1 )
return false ;
return true ;
} void rebuild ( ) { for ( int i = ; i <= * n ; i ++ ) {
int tmp1 = belong [ i ] ;
for ( int j = ; j < g [ i ] . size ( ) ; j ++ ) {
int tmp2 = belong [ g [ i ] [ j ] ] ;
if ( tmp1 != tmp2 ) {
create_x ( tmp2 , tmp1 ) ;
ind [ tmp1 ] ++ ;
}
}
} } void opp ( ) {
for ( int i = ; i <= n ; i ++ ) {
op [ belong [ i ] ] = belong [ i + n ] ;
op [ belong [ i + n ] ] = belong [ i ] ;
}
} bool judge () {
for ( int i = ; i <= n ; i ++ )
if ( belong [ i ] == belong [ i + n ] )
return true ;
return false ;
} void topsort ( ) {
for ( int i = ; i <= cnx ; i ++ )
if ( ind [ i ] == )
q . push ( i ) ;
while ( ! q . empty ( ) ) {
int tmp = q . front ( ) ;
q . pop ( ) ;
if ( color [ tmp ] == )
color [ tmp ] = , color [ op [ tmp ] ] = ;
//if ( color [ tmp ] == 2 ) continue ;
for ( int i = ; i < rg [ tmp ] . size ( ) ; i ++ ) {
int v = rg [ tmp ] [ i ] ;
ind [ v ] -- ;
if ( ! ind [ v ] )
q . push ( v ) ;
}
}
} void tarjan ( int x ) {
cnt ++ ;
dfn [ x ] = low [ x ] = cnt ;
ins [ x ] = true ; s . push ( x ) ;
for ( int i = ; i < g [ x ] . size ( ) ; i ++ ) {
int v = g [ x ] [ i ] ;
if ( ! dfn [ v ] ) {
tarjan ( v ) ;
low [ x ] = minx ( low [ x ] , low [ v ] ) ;
}
else if ( ins [ v ] ) low [ x ] = minx ( low [ x ] , dfn [ v ] ) ;
}
if ( dfn [ x ] == low [ x ] ) {
cnx ++ ;
while ( ) {
int tmp = s . top ( ) ;
s . pop ( ) ;
ins [ tmp ] = false ;
belong [ tmp ] = cnx ;
if ( x == tmp ) break ;
}
}
} int main ( ) { while ( scanf ( "%d" , & n ) == ) { cnx = ; cnt = ;
memset ( belong , , sizeof ( belong ) ) ;
memset ( dfn , , sizeof ( dfn ) ) ;
memset ( ins , false , sizeof ( ins ) ) ;
memset ( low , , sizeof ( low ) ) ;
memset ( dot , , sizeof ( dot ) ) ;
memset ( ind , , sizeof ( ind ) ) ;
memset ( op , , sizeof ( op ) ) ;
memset ( color , , sizeof ( color ) ) ; for ( int i = ; i <= n ; i ++ ) {
scanf ( "%d:%d %d:%d %d" , & sth , & stm , & enh , & enm , & len ) ;
dot [ i ] . st = sth * + stm ;
dot [ i ] . en = sth * + stm + len ;
dot [ i + n ] . st = enh * + enm - len ;
dot [ i + n ] . en = enh * + enm ;
} for ( int i = ; i <= n ; i ++ )
for ( int j = ; j <= n ; j ++ )
if ( i != j ) {
if ( conflict ( i , j ) ) {
create ( i , j + n ) ;
create ( j , i + n ) ;
}
if ( conflict ( i , j + n ) ) {
create ( i , j ) ;
create ( j + n , i + n ) ;
}
if ( conflict ( i + n , j ) ) {
create ( i + n , j + n ) ;
create ( j , i ) ;
}
if ( conflict ( i + n , j + n ) ) {
create ( i + n , j ) ;
create ( j + n , i ) ;
}
} for ( int i = ; i <= * n ; i ++ )
if ( ! dfn [ i ] )
tarjan ( i ) ; if ( judge ( ) ) {
printf ( "NO\n" ) ;
continue ;
}
else printf ( "YES\n" ) ; rebuild ( ) ; opp ( ) ; topsort ( ) ; for ( int i = ; i <= n ; i ++ )
if ( color [ belong [ i ] ] == )
printf ( "%.2d:%.2d %.2d:%.2d\n" , dot [ i ] . st / , dot [ i ] . st % , dot [ i ] . en / , dot [ i ] . en % ) ;
else
printf ( "%.2d:%.2d %.2d:%.2d\n" , dot [ i + n ] . st / , dot [ i + n ] . st % , dot [ i + n ] . en / , dot [ i + n ] . en % ) ; for ( int i = ; i <= * n ; i ++ ) {
g [ i ] . clear ( ) ;
rg [ i ] . clear ( ) ;
}
} return ;
}

Vector邻矩AC


 
 

POJ 3683 Priest John's Busiest Day 【2-Sat】的更多相关文章

  1. poj 3683 Priest John's Busiest Day【2-SAT+tarjan+拓扑】

    转换成2-SAT模型,建边是如果时间(i,j)冲突就连边(i,j'),其他同理 tarjan缩点,判可行性 返图拓扑,输出方案 #include<iostream> #include< ...

  2. POJ 3683 Priest John's Busiest Day / OpenJ_Bailian 3788 Priest John's Busiest Day(2-sat问题)

    POJ 3683 Priest John's Busiest Day / OpenJ_Bailian 3788 Priest John's Busiest Day(2-sat问题) Descripti ...

  3. POJ 3683 Priest John's Busiest Day(2-SAT+方案输出)

    Priest John's Busiest Day Time Limit: 2000MS   Memory Limit: 65536K Total Submissions: 10010   Accep ...

  4. POJ 3683 Priest John's Busiest Day (2-SAT)

    Priest John's Busiest Day Time Limit: 2000MS   Memory Limit: 65536K Total Submissions: 6900   Accept ...

  5. POJ 3683 Priest John's Busiest Day(2-SAT 并输出解)

    Description John is the only priest in his town. September 1st is the John's busiest day in a year b ...

  6. poj - 3683 - Priest John's Busiest Day(2-SAT)

    题意:有N场婚礼,每场婚礼的开始时间为Si,结束时间为Ti,每场婚礼有个仪式,历时Di,这个仪式要么在Si时刻开始,要么在Ti-Di时刻开始,问能否安排每场婚礼举行仪式的时间,使主持人John能参加所 ...

  7. POJ 3683 Priest John's Busiest Day (2-SAT)

    题意:有n对新人要在同一天结婚.结婚时间为Ti到Di,这里有时长为Si的一个仪式需要神父出席.神父可以在Ti-(Ti+Si)这段时间出席也可以在(Di-Si)-Si这段时间.问神父能否出席所有仪式,如 ...

  8. POJ 3683 Priest John's Busiest Day (2-SAT,常规)

    题意: 一些人要在同一天进行婚礼,但是牧师只有1个,每一对夫妻都有一个时间范围[s , e]可供牧师选择,且起码要m分钟才主持完毕,但是要么就在 s 就开始,要么就主持到刚好 e 结束.因为人数太多了 ...

  9. POJ 3683 Priest John's Busiest Day

    2-SAT简单题,判断一下两个开区间是否相交 #include<cstdio> #include<cstring> #include<cmath> #include ...

随机推荐

  1. Shell使用手册

    1.循环数组 list=(20180531 20180430 20180331 20180228 20180131 20171231 20171130 20171031 20170930 201708 ...

  2. windows系统镜像 微软官方资源便捷下载教程

    今天跟小师弟学到了一个下载软件的好办法,省得到各种网站下载带有病毒,插件的资源. 这个神奇的网站叫做   MSDN, 我告诉你,这是一个私人维护的网站,里面有各种官方软件的下载地址.可以直接用下载工具 ...

  3. lambdas vs. method groups

    Update: Due to a glitch in my code I miscalculated the difference. It has been updated. See full his ...

  4. 协议森林09 爱的传声筒 (TCP连接)

    作者:Vamei 出处:http://www.cnblogs.com/vamei 欢迎转载,也请保留这段声明.谢谢! 在TCP协议与"流"通信中,我们概念性的讲解了TCP通信的方式 ...

  5. Android 之 GridView具体解释

    工作这么久以来,都是以解决需求为目标.渐渐发现这样的学习方式不好,学到的知识能立即解决这个问题,但没有经过梳理归纳. 故想系统总结下一些有趣味的知识点. 在这篇博客中想以一个样例系统解说下GridVi ...

  6. gluoncv rpn 正负样本

    https://github.com/dmlc/gluon-cv/blob/master/gluoncv/model_zoo/rpn/rpn_target.py def forward(self, i ...

  7. 发送邮件 html格式

    下面黄色代码为实现发送邮件 FileStream fs = new FileStream(Server.MapPath("../ImportUserIOExcel/productConsul ...

  8. Exp6 信息收集与漏洞扫描 20164314

    一.实践目标 掌握信息搜集的最基础技能与常用工具的使用方法. 二.实践内容 1.各种搜索技巧的应用 2.DNS IP注册信息的查询 3.基本的扫描技术:主机发现.端口扫描.OS及服务版本探测.具体服务 ...

  9. linux(Centos7系统)中安装JDK、Tomcat、Mysql

    安装前准备两个工具:(360可以安装) 1.JDK的安装 使用yum命令安装 .查看是否已安装JDK # yum list installed |grep java .卸载CentOS系统Java环境 ...

  10. Java 数据库操作oracle增删改查,通用封装基于hashmap

    pt1:首先安装oracle连接驱动 下载地址:https://pan.baidu.com/s/1jW_ofgU4eJmAn7Y2J5B46A  密码:epkz 1.将ojdbc6.jar导入项目中 ...