POJ 3683 Priest John's Busiest Day 【2-Sat】
这是一道裸的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】的更多相关文章
- poj 3683 Priest John's Busiest Day【2-SAT+tarjan+拓扑】
转换成2-SAT模型,建边是如果时间(i,j)冲突就连边(i,j'),其他同理 tarjan缩点,判可行性 返图拓扑,输出方案 #include<iostream> #include< ...
- 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 ...
- POJ 3683 Priest John's Busiest Day(2-SAT+方案输出)
Priest John's Busiest Day Time Limit: 2000MS Memory Limit: 65536K Total Submissions: 10010 Accep ...
- POJ 3683 Priest John's Busiest Day (2-SAT)
Priest John's Busiest Day Time Limit: 2000MS Memory Limit: 65536K Total Submissions: 6900 Accept ...
- 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 ...
- poj - 3683 - Priest John's Busiest Day(2-SAT)
题意:有N场婚礼,每场婚礼的开始时间为Si,结束时间为Ti,每场婚礼有个仪式,历时Di,这个仪式要么在Si时刻开始,要么在Ti-Di时刻开始,问能否安排每场婚礼举行仪式的时间,使主持人John能参加所 ...
- POJ 3683 Priest John's Busiest Day (2-SAT)
题意:有n对新人要在同一天结婚.结婚时间为Ti到Di,这里有时长为Si的一个仪式需要神父出席.神父可以在Ti-(Ti+Si)这段时间出席也可以在(Di-Si)-Si这段时间.问神父能否出席所有仪式,如 ...
- POJ 3683 Priest John's Busiest Day (2-SAT,常规)
题意: 一些人要在同一天进行婚礼,但是牧师只有1个,每一对夫妻都有一个时间范围[s , e]可供牧师选择,且起码要m分钟才主持完毕,但是要么就在 s 就开始,要么就主持到刚好 e 结束.因为人数太多了 ...
- POJ 3683 Priest John's Busiest Day
2-SAT简单题,判断一下两个开区间是否相交 #include<cstdio> #include<cstring> #include<cmath> #include ...
随机推荐
- C# 委托/Func() 中 GetInvocationList() 方法的使用 | 接收委托多个返回值
在日常使用委托时,有以下常用方法 方法名称 说明 Clone 创建委托的浅表副本. GetInvocationList 按照调用顺序返回此多路广播委托的调用列表. GetMethodIm ...
- python接口测试-项目实践(八) 完成的接口类和执行脚本
脱敏后脚本 projectapi.py: 项目接口类 # -*- coding:utf-8 -*- """ xx项目接口类 2018-11 dinghanhua &quo ...
- struct class 内存结构初探-内存模型顺序和变量的实际添加顺序一致
typedef struct structTest { char xchar; int xint; int yint; }xStruct; @interface ViewController () { ...
- HTML5旋转立方体
http://42.121.104.41/templets/default/test1.htm 须要源代码的留言邮箱哈~
- 【jQuery】jQuery与Ajax的应用
1.demo1 <script language="javascript" type="text/javascript"> //通过这个函数来异步获 ...
- 2、Android-UI(常用控件)
2.1.如何编写程序页面 Android中有许多编写程序的方式可供选择 Android Studio和Eclipse中都提供了响应的可视化编辑器 可以直接再进行拖动创建布局 推荐使用手动编写方式进行开 ...
- ethereumjs/merkle-patricia-tree-1-简介
https://github.com/ethereumjs/merkle-patricia-tree SYNOPSIS概要 This is an implementation of the modif ...
- rabbitmq关于guest用户登录失败解决方法
刚安装完rabbitmq,登录的时候出现了: login failed问题: 查看rabbitmq的文档,发现在3.3.1以后的版中,处于安全的考虑,guest这个默认的用户只能通过localhos ...
- 【转】Linux下RabbitMQ服务器搭建(单实例)
阅读目录 系统环境 安装步骤 注意事项 参考资料 回到顶部 系统环境 操作系统:CentOS6.9 erlang:OTP 19.3 rabbitmq:rabbitmq-server 3.6.12 回到 ...
- Dubbo实践(一)入门示例
dubbo是一个分布式服务框架,致力于提供高性能和透明化的RPC远程服务调用方案,以及SOA服务治理方案.简单的说,dubbo就是个服务框架,如果没有分布式的需求,其实是不需要用的,只有在分布式的时候 ...