Priest John's Busiest Day
Time Limit: 2000MS   Memory Limit: 65536K
Total Submissions: 8872   Accepted: 3027   Special Judge

Description

John is the only priest in his town. September 1st is the John's busiest day in a year because there is an old legend

in the town that the couple who get married on that day will be forever blessed by the God of Love.

This year N couples plan to get married on the blessed day. The i-th couple plan to hold their wedding from time Si to time Ti.

According to the traditions in the town, there must be a special ceremony on which the couple stand before the priest and accept blessings.

The i-th couple need Di minutes to finish this ceremony. Moreover, this ceremony must be either at the beginning

or the ending of the wedding (i.e. it must be either from Si to Si + Di, or from Ti - Di to Ti).

Could you tell John how to arrange his schedule so that he can present at every special ceremonies of the weddings.

Note that John can not be present at two weddings simultaneously.

Input

The first line contains a integer N ( 1 ≤ N ≤ 1000). 
The next N lines contain the SiTi and DiSi and Ti are in the format of hh:mm.

Output

The first line of output contains "YES" or "NO" indicating whether John can be present at every special ceremony.

If it is "YES", output another N lines describing the staring time and finishing time of all the ceremonies.

Sample Input

2
08:00 09:00 30
08:15 09:00 20

Sample Output

YES
08:00 08:30
08:40 09:00

Source

 

题意:有n对人要结婚,牧师分别去主持每个婚礼中的仪式,给出每对新人的开始和结束时间,还有仪式需要的时间,仪式必须要在开始或结束的时间举办,问是否能给出一种方案,使得牧师可以参加所有仪式。

题解:

2-sat。。。

终于把这道题A了。。。

首先,先把每个婚礼的每一段开始时间标记为i*2,结束时间标记为i*2-1。然后缩点,逆向用强联通分量建图。然后用拓扑排序做一遍,输出即可。

安利一个2-sat讲解:2-sat总结

 #include<bits/stdc++.h>
using namespace std;
#define MAXN 2010
struct node
{
int begin,end,next;
}edge[*MAXN*MAXN];
struct NODE
{
int begin,end,next;
}edge1[*MAXN*MAXN];
int cnt,Head[MAXN],cnt1,Head1[MAXN],n,s1[MAXN],t1[MAXN],LOW[MAXN],DFN[MAXN],STACK[MAXN],color[MAXN],q[MAXN],op[MAXN],belong[MAXN],SCC,Id[MAXN],SIZE,top,N;
bool INSTACK[MAXN];
void addedge(int bb,int ee)
{
edge[++cnt].begin=bb;edge[cnt].end=ee;edge[cnt].next=Head[bb];Head[bb]=cnt;
}
void addedge1(int bb,int ee)
{
edge1[++cnt1].begin=bb;edge1[cnt1].end=ee;edge1[cnt1].next=Head1[bb];Head1[bb]=cnt1;
}
int read()
{
int s=,fh=;char ch=getchar();
while(ch<''||ch>''){if(ch=='-')fh=-;ch=getchar();}
while(ch>=''&&ch<=''){s=s*+(ch-'');ch=getchar();}
return s*fh;
}
int Judge(int ii,int jj)
{
if(s1[jj]>=t1[ii]||s1[ii]>=t1[jj])return ;
return ;
}
void Build()
{
int i,j;
memset(Head,-,sizeof(Head));cnt=;
for(i=;i<=n;i++)
{
for(j=i+;j<=n;j++)
{
if(Judge(*i,*j)==)
{
addedge(*i,*j-);
addedge(*j,*i-);
}
if(Judge(*i,*j-)==)
{
addedge(*i,*j);
addedge(*j-,*i-);
}
if(Judge(*i-,*j)==)
{
addedge(*i-,*j-);
addedge(*j,*i);
}
if(Judge(*i-,*j-)==)
{
addedge(*i-,*j);
addedge(*j-,*i);
}
}
}
}
void Tarjan(int u)
{
int i,v;
LOW[u]=DFN[u]=++SIZE;
STACK[top++]=u;INSTACK[u]=true;
for(i=Head[u];i!=-;i=edge[i].next)
{
v=edge[i].end;
if(!DFN[v])
{
Tarjan(v);
LOW[u]=min(LOW[u],LOW[v]);
}
else if(INSTACK[v]==true)LOW[u]=min(LOW[u],DFN[v]);
}
if(LOW[u]==DFN[u])
{
SCC++;
do
{
v=STACK[--top];
INSTACK[v]=false;
belong[v]=SCC;
}while(u!=v);
}
}
void solve()
{
SIZE=;
for(int i=;i<=N;i++)if(!DFN[i])Tarjan(i);
}
void dfs(int u)
{
if(color[u]!=)return;
color[u]=-;
for(int i=Head1[u];i!=-;i=edge1[i].next)dfs(edge1[i].end);
}
void Toposort()
{
int head=,tail=,i,u,v;
for(i=;i<=SCC;i++)if(Id[i]==)q[++tail]=i;
while(head<=tail)
{
u=q[++head];
if(color[u]!=)continue;//已经被标记过就不用搜索了.
color[u]=;/*若将color[]标记为,代表被标记为选择(即1).*/dfs(op[u]);/*将和u对立的点标记为选择u的对立点(即-1).*/
for(i=Head1[u];i!=-;i=edge1[i].next)
{
v=edge1[i].end;
Id[v]--;if(Id[v]==)q[++tail]=v;
}
}
}
void write(int k)
{
printf("%.2d:",k/);
printf("%.2d",k%);
}
int main()
{
int S1,S2,T1,T2,D,i;
n=read();
N=;
for(i=;i<=n;i++)
{
S1=read();S2=read();T1=read();T2=read();D=read();
s1[++N]=S1*+S2;t1[N]=s1[N]+D;
t1[++N]=T1*+T2;s1[N]=t1[N]-D;
}
Build();
solve();
for(i=;i<=n;i++)if(belong[*i-]==belong[*i]){printf("NO");return ;}
memset(Head1,-,sizeof(Head1));cnt1=;
for(i=;i<=cnt;i++)
{
if(belong[edge[i].begin]!=belong[edge[i].end]){addedge1(belong[edge[i].end],belong[edge[i].begin]);Id[belong[edge[i].begin]]++;}
}
memset(color,,sizeof(color));
for(i=;i<=n;i++)
{
op[belong[*i]]=belong[*i-];
op[belong[*i-]]=belong[*i];
}
Toposort();
printf("YES");
for(i=;i<=n;i++)
{
printf("\n");
if(color[belong[i*]]==){write(s1[i*]);printf(" ");write(t1[i*]);}
else {write(s1[i*-]);printf(" ");write(t1[i*-]);}
}
fclose(stdin);
fclose(stdout);
return ;
}

Poj 3683-Priest John's Busiest Day 2-sat,拓扑排序的更多相关文章

  1. 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 ...

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

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

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

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

  4. 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 ...

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

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

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

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

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

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

  8. POJ 3683 Priest John's Busiest Day

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

  9. POJ 3683 Priest John's Busiest Day[2-SAT 构造解]

    题意: $n$对$couple$举行仪式,有两个时间段可以选择,问是否可以不冲突举行完,并求方案 两个时间段选择对应一真一假,对于有时间段冲突冲突的两人按照$2-SAT$的规则连边(把不冲突的时间段连 ...

  10. POJ 3683 Priest John's Busiest Day 【2-Sat】

    这是一道裸的2-Sat,只要考虑矛盾条件的判断就好了. 矛盾判断: 对于婚礼现场 x 和 y,x 的第一段可以和 y 的第一段或者第二段矛盾,同理,x 的第二段可以和 y 的第一段或者第二段矛盾,条件 ...

随机推荐

  1. 编程语言的发展趋势by Anders Hejlsberg

    这是Anders Hejlsberg在比利时TechDays 2010所做的开场演讲. 编程语言的发展非常缓慢,期间也当然出现了一些东西,例如面向对象等等,你可能会想,那么我么这么多年的努力都到哪里去 ...

  2. CentOS系统Apache服务器优化详解

    1.Apache优化 Apache能够在CentOS系统正常运行.但是,对于访问量稍大的站点,Apache的这些默认配置是无法满足需求的,我们仍需调整Apache的一些参数,使Apache能够在大访问 ...

  3. Unix环境高级编程学习笔记——fcntl

    写这篇文正主要是为了介绍下fcntl,并将我自己在学习过程中的一些理解写下来,不一定那么官方,也有错误,希望指正,共同进步- fcntl: 一个修改一打开文件的性质的函数.基本的格式是 int fcn ...

  4. Linux 进程编程

    Linux通过维护者五个状态来调度进程的运行.这五个状态分别为:运行.可中断.不可中断.僵死.停止 . PID来标识不同的进程的,Linux中每一个进程都有一个唯一的进程号 . PCB块就是一个进程资 ...

  5. 『奇葩问题集锦』Cannot find module 'webpack/lib/node/NodeTemplatePlugin'

    第一步:npm config get prefix ,获取输出path“C:\Users\jaxGu\AppData\Roaming\npm”加上"\node_modules"用于 ...

  6. Win7 + VS2015 + CMake3.6.1-GUI编译库

    CMake生成Unicode版本VC工程 Just add this line in your top CMakeLists.txt file:     add_definitions(-DUNICO ...

  7. C#中的委托事件的分析

    推荐:http://www.cnblogs.com/SkySoot/archive/2012/04/05/2433639.html 委托和事件在 .NET Framework 中的应用非常广泛,然而, ...

  8. Hibernate学习笔记--环境搭建及运行

    1.hibernate开发包下载 http://sourceforge.net/projects/hibernate/files/ 如果不能访问请用代理http://dongtaiwang.com/l ...

  9. activemq启动不起来,报错Address already in use: JVM_Bind

    之前莫名其妙的activemq怎么都启动不起来后来多方查询是因为widows 的ICS服务. 解决方案是,我的电脑上邮件,选择服务,然后在服务中找到Internet Connection Sharin ...

  10. [原博客] POI系列(1)

    正规.严谨.精妙. -POI 发现POI(波兰信息学奥赛)的题都很有意思.于是开刷bzoj上的poi题目(按ac人数降序..).顺手写一写题解,加深印象. 为了防止一篇文章过于长,打算每五道题另起一篇 ...