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. CentOS使用sendmail发送邮件

    1.安装sendmail yum -y install sendmail 2.启动sendmail服务 service sendmail start 3.将发件内容写入mail.txt mail -s ...

  2. 重新看php数组

    闲来有空,最近看php手册数组这块,对于array_values() 还是第一次接触,array_values是不保留键名,只有键值的函数,还有一个作用就是  重新索引. unset() 函数,是删除 ...

  3. 用GitHub Pages免费空间搭建Blog

    前言   其实之前就知道可以用GitHub Pages搭建静态博客,不过之前一直忙着爬手册撸代码==,昨天终于把前端各种手册里的入门教程撸的差不多了(CSS布局撸的我要吐了好嘛),于是把代码什么的放一 ...

  4. c读mysql产生乱码问题

    在编写接口API时,发现中文字utf8输入的在linux下采用c读取显示为”??”问号,这是由于编码造成的. 很简单的两个地方做修改就搞定. 1.先找到mysql的my.cnf配置文件/etc/my. ...

  5. 那些年,我们一起学WCF--(6)PerCall实例行为

    当客户端调用服务器端服务后,服务器端就会为客户端生成一个实例,关于服务实例的分配问题,在WCF中有专门的属性进行设置,可以让所有客户端共享一个实例, 也可以让一个客户端可以拥有多个实例,也可以让一个实 ...

  6. shell脚本学习之case例子

    case和select结构在技术上说并不是循环, 因为它们并不对可执行代码块进行迭代. 但是和循环相似的是, 它们也依靠在代码块顶部或底部的条件判断来决定程序的分支.  在代码块中控制程序分支  ca ...

  7. 在万网虚拟主机上部署MVC5

    参考 要想部署mvc,需要把一些mvc用到的全局程序集改为本地部署,通过N次试验,终于搞定. 特写个备忘录,免得以后忘了. 首先更改web.config,在里面加上 <system.web> ...

  8. 找出整数中第k大的数

    一  问题描述: 找出 m 个整数中第 k(0<k<m+1)大的整数. 二  举例: 假设有 12 个整数:data[1, 4, -1, -4, 9, 8, 0, 3, -8, 11, 2 ...

  9. maven 常用插件总结

    maven-javadoc-plugin (1) 说明:该插件生成项目的javadoc.对于构建jar目标,javadoc会首先生成并打包放入jar文件中. (2) 默认用法: pom.xml配置 & ...

  10. 一起来背ABC

    construction 构造,结构 constructor  构造函数,施工员