图论(2-sat):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的更多相关文章
- 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 ...
- 【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 ...
- poj 3686 Priest John's Busiest Day
http://poj.org/problem?id=3683 2-sat 问题判定,输出一组可行解 http://www.cnblogs.com/TheRoadToTheGold/p/8436948. ...
- 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+方案输出)
Priest John's Busiest Day Time Limit: 2000MS Memory Limit: 65536K Total Submissions: 10010 Accep ...
- Priest John's Busiest Day(POJ 3683)
原题如下: Priest John's Busiest Day Time Limit: 2000MS Memory Limit: 65536K Total Submissions: 12162 ...
- 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 ...
- POJ3683 Priest John's Busiest Day(2-SAT)
Time Limit: 2000MS Memory Limit: 65536K Total Submissions: 11049 Accepted: 3767 Special Judge ...
- 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 ...
随机推荐
- Python的Ftplib问题:UnicodeEncodeError: 'latin-1' codec can't encode characters的解决方法
ftplib中有一个方法是cwd,用来切换目录,需要传入一个dirname,经过个人测试,该dirname不能含有汉字,会抛出:UnicodeEncodeError: 'latin-1' codec ...
- 样式单位之px、em、rem
最近在看bootstrap.css的时候看到很多单位都用到rem而不是熟系的px.经学习得知: 1.px精确的单位: 2.em为相对单位(相对父级元素) 3.rem为相对单位(相对根元素 html)
- HTML设置固定页脚飘浮
Css /* 页脚 */.footSty{bottom: 0pt; margin: 0pt; position: fixed; width: 100%; z-index: 10 ! important ...
- 【转】纯 CSS 实现高度与宽度成比例的效果
先来演示页面:Demo; 转的内容: 最近在做一个产品列表页面,布局如右图所示.页面中有若干个 item,其中每个 item 都向左浮动,并包含在自适应浏览器窗口宽度的父元素中. item 元素的 C ...
- selenium+eclipse+python环境
1.下载并安装jdk,配置环境变量: 2.下载并安装python,配置path系统环境变量:D:\Program Files\python34: 3.安装selenium,在安装好的python路径D ...
- POJ 1236.Network of Schools (强连通)
首先要强连通缩点,统计新的图的各点的出度和入度. 第一问直接输出入度为0的点的个数 第二问是要是新的图变成一个强连通图,那么每一个点至少要有一条出边和一条入边,输出出度和入度为0的点数大的那一个 注意 ...
- 总结Widows 7 Start->Run 命令
Widows + R基本上成为很常用的方式,那么通过Windows + R我们可以在运行中做什么手脚呢. 下面从最基本的系统命令说起 notepad--------打开记事本 services. ...
- 《find技巧》-“linux命令五分系列”之一
一天一个命令,做个记录, 我要成大神,哈哈哈 本原创文章属于<Linux大棚>博客. 博客地址为http://roclinux.cn. 文章作者为roc 希望您能通过捐款的方式支持Linu ...
- underscorejs-countBy学习
2.20 countBy 2.20.1 语法 _.countBy(list, iteratee, [context]) 2.20.2 说明 排序一个列表组成一个组,并且返回各组中的对象的数量的计数.类 ...
- 小心DriveInfo类IsReady属性的较大延迟问题
当某些驱动器调用IsReady属性来判断是否准备好时,会有性能问题,会非常慢,特别是网络驱动器断开的时候,这个属性会有30秒左右的延迟,这对程序执行是非常大的开销,请慎重调用