Priest John's Busiest Day
 

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 Si, Ti and Di. Si 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

  闹半天结果是数组开小了。

  这里是2-sat O(n+m)输出任意解的模板。

 #include <iostream>
#include <cstring>
#include <cstdio>
using namespace std;
const int maxn=;
const int maxm=;
int n;
struct E{
int cnt,fir[maxn],to[maxm],nxt[maxm];
void addedge(int a,int b){
nxt[++cnt]=fir[a];
fir[a]=cnt;
to[cnt]=b;
}
}e,g; struct Time{
int h,s;
Time(int h_=,int s_=){
h=h_;s=s_;
}
friend bool operator <=(Time x,Time y){
return x.h!=y.h?x.h<y.h:x.s<=y.s;
}
friend Time operator +(Time x,Time y){
Time ret(,);
ret.s=(x.s+y.s)%;
ret.h=x.h+y.h+(x.s+y.s)/;
return ret;
}
friend Time operator -(Time x,Time y){
Time ret(,);
ret.s=x.s-y.s+(x.s<y.s?:)*;
ret.h=x.h-y.h-(x.s<y.s?:);
return ret;
}
void Print(){
printf("%02d:%02d",h,s);
}
}p1[maxn],p2[maxn],p3[maxn],p4[maxn]; bool Judge(Time pl,Time pr,Time ql,Time qr){
if(pr<=ql||qr<=pl)return false;
return true;
} int tot,ID[maxn],low[maxn],scnt;
int st[maxn],top,scc[maxn]; void Tarjan(int x){
ID[x]=low[x]=++tot;st[++top]=x;
for(int i=e.fir[x];i;i=e.nxt[i])
if(!ID[e.to[i]]){
Tarjan(e.to[i]);
low[x]=min(low[x],low[e.to[i]]);
}
else if(!scc[e.to[i]])
low[x]=min(low[x],ID[e.to[i]]);
if(low[x]==ID[x]){
++scnt;
while(true){
int y=st[top--];
scc[y]=scnt;
if(x==y)break;
}
}
} bool Solve(){
for(int i=;i<n*;i++)
if(!ID[i])Tarjan(i); for(int i=;i<n;i++)
if(scc[i*]==scc[i*+])
return false;
return true;
} int rev[maxn];
int match[maxn],in[maxn];
int q[maxn],front,back;
void Topo(){
for(int i=;i<n;i++){
rev[scc[i*]]=scc[i*+];
rev[scc[i*+]]=scc[i*];
}
for(int x=;x<n*;x++)
for(int i=e.fir[x];i;i=e.nxt[i])
if(scc[x]!=scc[e.to[i]])
g.addedge(scc[e.to[i]],scc[x]),in[scc[x]]+=; for(int i=;i<=scnt;i++)
if(!in[i])q[back++]=i; while(front<back){
int x=q[front++];
if(match[x]==){
match[x]=;
match[rev[x]]=;
}
for(int i=g.fir[x];i;i=g.nxt[i])
if(--in[g.to[i]]==)q[back++]=g.to[i];
}
} void Print(){
for(int i=;i<n;i++){
if(match[scc[i*]]==){
p1[i].Print();printf(" ");
p2[i].Print();printf("\n");
}
else{
p3[i].Print();printf(" ");
p4[i].Print();printf("\n");
}
}
} int main(){
scanf("%d",&n);
for(int i=,d;i<n;i++){
scanf("%d:%d",&p1[i].h,&p1[i].s);
scanf("%d:%d",&p4[i].h,&p4[i].s);
scanf("%d",&d);
p2[i]=p1[i]+Time(d/,d%);
p3[i]=p4[i]-Time(d/,d%);
} for(int i=;i<n;i++)
for(int j=i+;j<n;j++){
if(Judge(p1[i],p2[i],p1[j],p2[j])){
e.addedge(i*,j*+);
e.addedge(j*,i*+);
}
if(Judge(p1[i],p2[i],p3[j],p4[j])){
e.addedge(i*,j*);
e.addedge(j*+,i*+);
}
if(Judge(p3[i],p4[i],p1[j],p2[j])){
e.addedge(i*+,j*+);
e.addedge(j*,i*);
}
if(Judge(p3[i],p4[i],p3[j],p4[j])){
e.addedge(i*+,j*);
e.addedge(j*+,i*);
}
} if(!Solve())
printf("NO\n");
else{
printf("YES\n");
Topo();Print();
}
return ;
}

图论(2-sat):Priest John's Busiest Day的更多相关文章

  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. 【POJ3683】Priest John's Busiest Day

    题目 John is the only priest in his town. September 1st is the John's busiest day in a year because th ...

  3. poj 3686 Priest John's Busiest Day

    http://poj.org/problem?id=3683 2-sat 问题判定,输出一组可行解 http://www.cnblogs.com/TheRoadToTheGold/p/8436948. ...

  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+方案输出)

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

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

    原题如下: Priest John's Busiest Day Time Limit: 2000MS   Memory Limit: 65536K Total Submissions: 12162   ...

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

  8. POJ3683 Priest John's Busiest Day(2-SAT)

    Time Limit: 2000MS   Memory Limit: 65536K Total Submissions: 11049   Accepted: 3767   Special Judge ...

  9. HDU 2491 Priest John's Busiest Day(贪心)(2008 Asia Regional Beijing)

    Description John is the only priest in his town. October 26th is the John's busiest day in a year be ...

随机推荐

  1. 数据的动态合并和导出至EXCEL

    最近一段时间都在处理数据的动态合并和导出EXCEL的问题,写个demo记录下,希望和我碰到同样问题的博友可以顺利解决:后面会提供demo下载链接. (VS2012,ASP.NET) 一.主要解决以下问 ...

  2. [转帖]SD卡&FLASH&USB

    来源:http://www.cypress.com Cypress官网,了解任何芯片都应该从它的官网入手,资料一定是最多最原始的,像Ronnie学习. Cypress’s EZ-USB® FX2LP™ ...

  3. 解决vim不能使用方向键和退格键问题

    1.使用vi命令时,不能正常编辑文件,使用方向键时老是出现很多字母,或者退格键却变成方向键的功能 只要重装一下vi的依赖包即可完美解决vi编辑器方向键变字母的问题.rpm -e vim-enhance ...

  4. method=“post/get”

    Form表单中method="post/get'的区别   Form提供了两种数据传输的方式——get和post.虽然它们都是数据的提交方式,但是在实际传输时确有很大的不同,并且可能会对数据 ...

  5. iOS打包ipa安装包的流程

    应用的发布也分两种 一种是.打包成ipa上传到国内第3方软件市场,当用户的手机已经JailBreak时,双击下载的ipa文件就可以安装软件 (ipa同android的apk包一样,实质是一个压缩包) ...

  6. 用JS实现版面拖拽效果

    类似于这样的一个版面,点击标题栏,实现拖拽效果. 添加onmousedown事件 通过获取鼠标的坐标(clientX,clientY)来改变面板的位置 注意:面板使用绝对定位方式,是以左上角为参考点, ...

  7. Java反射与代理

    Java反射机制与动态代理,使得Java更加强大,Spring核心概念IoC.AOP就是通过反射机制与动态代理实现的. 1       Java反射 示例: User user = new User( ...

  8. SGU 171.Sarov zones

    简单的贪心.优先weight最大的,优先匹配Q值大的地区 code #include <iostream> #include <algorithm> #include < ...

  9. Codeforces 474E - Pillars

    一眼看上去非常像最长不下降子序列. 然后比赛的时候对每个答案长度为k的序列,维护最后一个数的最大值和最小值. 当时不知道为什么认为从长度最长倒推至前面不会太长,于是心满意足地敲了个O(n^2).结果T ...

  10. asp.net Handler中的IsReusable属性及在Handler中使用Session

    大家在用HttpHandler的时候,一般都会有两个大的疑问(当然,前提是你有钻研精神的话,呵呵) 1. IsReusable到底什么意思? 老实说,这个属性很多人都感兴趣,但搞懂的人确实不多.MSD ...