POJ 3683 Priest John's Busiest Day[2-SAT 构造解]
题意:
$n$对$couple$举行仪式,有两个时间段可以选择,问是否可以不冲突举行完,并求方案
两个时间段选择对应一真一假,对于有时间段冲突冲突的两人按照$2-SAT$的规则连边(把不冲突的时间段连起来)
然后本题需要构造解,所以要$SCC$缩点反向建图记录否定再拓扑排序$dfs$染色,好麻烦...
#include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm>
#include <cmath>
using namespace std;
const int N=,M=1e6+;
typedef long long ll;
inline int read(){
char c=getchar();int x=,f=;
while(c<''||c>''){if(c=='-')f=-;c=getchar();}
while(c>=''&&c<=''){x=x*+c-'';c=getchar();}
return x*f;
}
int n,m;
struct person{
int a,b;
}p[N];
struct edge{
int v,ne;
}e[M],e2[M];
int h[N],h2[N],cnt=;
inline void ins(int u,int v){
cnt++;
e[cnt].v=v;e[cnt].ne=h[u];h[u]=cnt;
}
inline void ins2(int u,int v){
cnt++;
e2[cnt].v=v;e2[cnt].ne=h2[u];h2[u]=cnt;
}
inline bool isIn(int x,int y){
if(p[x].b<=p[y].a||p[x].a>=p[y].b) return false;
return true;
}
void buildGraph(){
for(int i=;i<=n;i++)
for(int j=i+;j<=n;j++){
int x=i<<,y=j<<;
if(isIn(x-,y-)) ins(x-,y),ins(y-,x);
if(isIn(x-,y)) ins(x-,y-),ins(y,x);
if(isIn(x,y-)) ins(x,y),ins(y-,x-);
if(isIn(x,y)) ins(x,y-),ins(y,x-);
}
}
int dfn[N],low[N],dfc,belong[N],scc;
int st[N],top;
void dfs(int u){
dfn[u]=low[u]=++dfc;
st[++top]=u;
for(int i=h[u];i;i=e[i].ne){
int v=e[i].v;
if(!dfn[v]){
dfs(v);
low[u]=min(low[u],low[v]);
}else if(!belong[v])
low[u]=min(low[u],dfn[v]);
}
if(dfn[u]==low[u]){
scc++;
while(true){
int x=st[top--];
belong[x]=scc;
if(x==u) break;
}
}
}
bool judge(){
for(int i=;i<=n;i++) if(belong[i<<]==belong[(i<<)-]) return false;
return true;
}
int ind[N];
void rebuildGraph(){
int _=n<<;
cnt=;
for(int u=;u<=_;u++)
for(int i=h[u];i;i=e[i].ne)
if(belong[u]!=belong[e[i].v])
ins2(belong[e[i].v],belong[u]),ind[belong[u]]++;
}
int color[N];
void dfsCol(int u){
if(color[u]) return;
color[u]=;
for(int i=h2[u];i;i=e2[i].ne) dfsCol(e2[i].v);
}
int opp[N];
void topoSort(){
top=;
for(int i=;i<=scc;i++) if(!ind[i]) st[++top]=i;
while(top){
int u=st[top--];
if(color[u]) continue;
color[u]=;dfsCol(opp[u]);
for(int i=h2[u];i;i=e2[i].ne){
int v=e2[i].v;
ind[v]--;
if(!ind[v]) st[++top]=v;
}
}
}
void print(int a,int b){
printf("%.2d:%.2d ",a/,a%);
printf("%.2d:%.2d ",b/,b%);
puts("");
}
int main(){
freopen("in","r",stdin);
n=read();
for(int i=;i<=n;i++){
int x=i<<,t;
t=read();
p[x-].a=t*+read();
t=read();
p[x].b=t*+read();
t=read();
p[x-].b=p[x-].a+t;
p[x].a=p[x].b-t;
}
buildGraph();
int _=n<<;
for(int i=;i<=_;i++) if(!dfn[i]) dfs(i);
if(!judge()) puts("NO");
else{
puts("YES");
rebuildGraph();
for(int i=;i<=n;i++){
int x=i<<;
opp[belong[x]]=belong[x-];
opp[belong[x-]]=belong[x];
}
topoSort();
for(int i=;i<=n;i++){
int x=i<<;
if(color[belong[x]]==) print(p[x].a,p[x].b);
else print(p[x-].a,p[x-].b);
}
}
}
POJ 3683 Priest John's Busiest Day[2-SAT 构造解]的更多相关文章
- 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 ...
- POJ 3683 Priest John's Busiest Day 【2-Sat】
这是一道裸的2-Sat,只要考虑矛盾条件的判断就好了. 矛盾判断: 对于婚礼现场 x 和 y,x 的第一段可以和 y 的第一段或者第二段矛盾,同理,x 的第二段可以和 y 的第一段或者第二段矛盾,条件 ...
随机推荐
- ubuntu sendmail配置发送邮件
ubuntu中sendmail函数可以很方便的发送邮件,ubuntu sendmail先要安装两个包. 必需安装的两个包: 代码 sudo apt-get install sendmail sudo ...
- python 元组学习
元组用tuple表示,用,分割开,和列表类似,但是没有排序修改等高级操作.简单地说就是终态的...... >>> tuple1 = (1,2,3)>>> type( ...
- timeit模块
算法是计算机处理信息的本质,因为计算机程序本质上是一个算法来告诉计算机确切的步骤来执行一个指定的任务.一般地,当算法在处理信息时,会从输入设备或数据的存储地址读取数据,把结果写入输出设备或某个存储地址 ...
- UE4 字符串的转换
创建Fstring: FString TestHUDString = FString(TEXT("This is my test FString.")); FString,FNam ...
- 【ELK_Log4net】.net Core重写一个TcpAppender
最近再搞ELK,三个工具部署完毕,想再继承上log4net.没想到.net core版Log4net竟然没有直接Tcp发送消息的appender.醉了.log4net 1.RemotingAppend ...
- window.history.go(-1)返回且刷新页面
windows窗口对象(历史)history.go(),history.back(),history.forward(). 因为windows对象引用不是必须的.所以windows.history.g ...
- 阿里云部署SSL证书详解
http://mp.weixin.qq.com/s/NV7Zad4DVEgzG2GCHYJVLw 查找中间证书 为了确保兼容到所有浏览器,我们必须在阿里云上部署中间证书,如果不部署证书,虽然安装过程可 ...
- DEDECMS万能标签{dede:sql}使用教程详解
http://www.dede58.com/a/dedebq/2015/0226/1737.html 1.首页在后台单页文档管理里添加一个单页文档,内容编辑框输入你要的内容生成. 2.在需要调用单页文 ...
- Dede 删除文档同时文章中的图片的方法
首先,在"/include"目录下建立"extend.func.php"文件. 然后,将以下内容保存在"extend.func.php"文件 ...
- C#编写影院售票系统(A project with a higher amount of gold )(2:相关代码)
此篇文章为项目代码,,,需要项目需求 ,思路分析与窗体效果请访问:http://www.cnblogs.com/lsy131479/p/8367304.html 项目类图: 影院类: using Sy ...