Time Limit: 2000MS   Memory Limit: 65536K
Total Submissions: 11049   Accepted: 3767   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场婚礼,告诉你每一场的开始和结束时间,以及举行仪式所需要的时间。问牧师能否参加所有的婚礼,如果能则输出一种方案。
 
对于每一场婚礼,我们可以把它抽象成一个点对
对于冲突的点,我们可以看做是利用选A不能选B的关系来进行限制
这样这道题就变成了一道2-SAT问题
然后按照套路用tarjan缩点,暴力建反向图,拓扑排序
 
#include<cstdio>
#include<cstring>
#include<algorithm>
#include<stack>
#include<vector>
#include<queue>
#define Pair pair<int,int>
#define F first
#define S second
using namespace std;
const int MAXN=1e6+;
//#define getchar() (p1==p2&&(p2=(p1=buf)+fread(buf,1,1<<20,stdin),p1==p2)?EOF:*p1++)
char buf[<<],*p1=buf,*p2=buf;
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;
}
Pair P[MAXN];
bool check(int x,int y)
{
if((P[x].S<=P[y].F)||(P[x].F>=P[y].S)) return ;
else return ;
}
struct node
{
int u,v,nxt;
}edge[MAXN];
int head[MAXN],num=;
inline void AddEdge(int x,int y)
{
edge[num].u=x;
edge[num].v=y;
edge[num].nxt=head[x];
head[x]=num++;
}
int dfn[MAXN],low[MAXN],vis[MAXN],color[MAXN],colornum=,tot;
stack<int>s;
void tarjan(int now)
{
dfn[now]=low[now]=++tot;
vis[now]=;
s.push(now);
for(int i=head[now];i!=-;i=edge[i].nxt)
{
if(!dfn[edge[i].v])
tarjan(edge[i].v),low[now]=min(low[now],low[edge[i].v]);
else if(vis[edge[i].v])
low[now]=min(low[now],dfn[edge[i].v]);
}
if(dfn[now]==low[now])
{
int h;colornum++;
do
{
h=s.top();s.pop();
color[h]=colornum;
vis[h]=;
}while(h!=now);
}
}
vector<int>E[MAXN];
int enemy[MAXN],inder[MAXN],ans[MAXN];
void Topsort()
{
queue<int>q;
for(int i=;i<=colornum;i++)
if(inder[i]==)
q.push(i);
while(q.size()!=)
{
int p=q.front();q.pop();
if(!ans[p]) ans[p]=,ans[enemy[p]]=-;
for(int i=;i<E[p].size();i++)
{
inder[E[p][i]]--;
if(inder[E[p][i]]==) q.push(E[p][i]);
}
}
}
int main()
{
#ifdef WIN32
freopen("a.in","r",stdin);
#else
#endif
memset(head,-,sizeof(head));
int N=read();
for(int i=;i<=N;i++)
{
int a,b,c,d,len;
scanf("%d:%d %d:%d %d",&a,&b,&c,&d,&len);
P[i].F=a*+b;
P[i].S=a*+b+len;
P[i+N].F=c*+d-len;
P[i+N].S=c*+d;
}
for(int i=;i<=N;i++)
{
for(int j=;j<=N;j++)
{
if(i==j) continue;
if(check(i,j)) AddEdge(i,j+N);
if(check(i,j+N)) AddEdge(i,j);
if(check(i+N,j)) AddEdge(i+N,j+N);
if(check(i+N,j+N)) AddEdge(i+N,j);
}
}
for(int i=;i<=N;i++)
if(!dfn[i])
tarjan(i);
for(int i=;i<=N;i++)
if(color[i]==color[i+N])
{printf("NO\n");return ;}
printf("YES\n");
for(int i=;i<=N;i++)
enemy[color[i]]=color[i+N],
enemy[color[i+N]]=color[i];
for(int i=;i<=N<<;i++)
{
for(int j=head[i];j!=-;j=edge[j].nxt)
{
if(color[i]!=color[edge[j].v])
{
E[color[edge[j].v]].push_back(color[i]);
inder[color[i]]++;
}
}
}
Topsort();
for(int i=;i<=N;i++)
{
if(ans[color[i]]==)
printf("%.2d:%.2d %.2d:%.2d\n",P[i].F/,P[i].F%,P[i].S/,P[i].S%);
else
printf("%.2d:%.2d %.2d:%.2d\n",P[i+N].F/,P[i+N].F%,P[i+N].S/,P[i+N].S%);
}
return ; }
 
 

POJ3683 Priest John's Busiest Day(2-SAT)的更多相关文章

  1. POJ3683 Priest John's Busiest Day 【2-sat】

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

  2. poj3683 Priest John's Busiest Day

    2-SAT 输出可行解 找可行解的方案就是: 根据第一次建的图建一个反图..然后求逆拓扑排序,建反图的原因是保持冲突的两个事件肯定会被染成不同的颜色 求逆拓扑排序的原因也是为了对图染的色不会发生冲突, ...

  3. poj3683 Priest John's Busiest Day

    2-SAT. 读入用了黄学长的快速读入,在此膜拜感谢. 把每对时间当作俩个点.如果有交叉代表相互矛盾. 然后tarjan缩点,这样就能得出当前的2-SAT问题是否有解. 如果有解,跑拓扑排序就能找出一 ...

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

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

  6. 图论(2-sat):Priest John's Busiest Day

    Priest John's Busiest Day   Description John is the only priest in his town. September 1st is the Jo ...

  7. poj 3686 Priest John's Busiest Day

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

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

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

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

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

随机推荐

  1. Oracle创建表空间创建用户和用户授权

    今天要创建一个Oracle用户,然后发现sql不太记得了,然后只能再去找找资料,发现这样效率并不是很高,所以记录成博客,有需要就直接从博客复制. 下面是我简单整理的,有需要可以参考. --创建表空间 ...

  2. 关于常用的编码工具如何引入jar包

    myeclipse和eclipse(差不多)引入jar包: 普通项目: 1.对准你的项目创建一个文件夹名字尽量命名成lib(注意要和src平级,不要在src下创建文件夹). 2.将下载好的依赖放到li ...

  3. HTML百宝箱(1从0开始)

    标准格式(XHTML) l   元素必须正确嵌套 l   元素必须始终关闭 l   元素名和属性名必须小写 l   文档必须有且仅有一个根元素 l   属性值必须使用双引号括起来 l   声明文档为标 ...

  4. Redis 再牛逼,也得设置密码!!

    Redis 你再牛逼也得设置密码啊,不然会有安全漏洞,造成一些隐患. 还有,比如像出现下面这样的错,需要设置密码,或者关闭保护模式,所以还是设置密码比较安全.不然只能本地操作,不能远程连接. DENI ...

  5. [源码]Python简易http服务器(内网渗透大文件传输含下载命令)

    Python简易http服务器源码 import SimpleHTTPServerimport SocketServerimport sysPORT = 80if len(sys.argv) != 2 ...

  6. git push 时发生 error: failed to push some refs to 错误 (解决办法)

    出现问题的原因:在github上更新了README.md,没有更新到本地仓库.而在本地git仓库又修改了文件,这时使用 git push origin master 推送到远程仓库后就出现了下面的问题 ...

  7. 《CLR Via C#》读书笔记:26.线程基础

    一.线程开销 操作系统创建线程是有代价的,其主要开销在下面列举出来了. 内存开销 线程内核对象 拥有线程描述属性与线程上下文,线程上下文占用的内存空间为 x86 架构 占用 700 字节.x64 架构 ...

  8. mybatis框架(1)---mybatis入门

    mybatis入门   MyBatis是什么? MyBatis 本是apache的一个开源项目iBatis, 2010年这个项目由apache software foundation 迁移到了goog ...

  9. Java的运行原理(转载)

    在Java中引入了虚拟机的概念,即在机器和编译程序之间加入了一层抽象的虚拟的机器.这台虚拟的机器在任何平台上都提供给编译程序一个的共同的接口.编译程序只需要面向虚拟机,生成虚拟机能够理解的代码,然后由 ...

  10. 项目ITP(二) 二维码 拿起你的手机装一装,扫一扫 【每日一搏】

    前言 系列文章:[传送门] 五一,期待的两天假期.我的生日,happy. [吐槽] 学校真恶心,半月前让我给他搞个东西,md,课题不加人.后来又来求,说钱(钱,咱不需要:我猜也不多).到现在,又来了, ...