图论(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 ...
随机推荐
- poj 1821 Fence 单调队列优化dp
/* poj 1821 n*n*m 暴力*/ #include<iostream> #include<cstdio> #include<cstring> #incl ...
- 编程基础-msdn编程指南笔记
此博仅为笔记,摘自msdn编程指南文档,链接地址:http://msdn.microsoft.com/zh-cn/library/67ef8sbd.aspx 注释:// 单行注释 /* 多行注释*/ ...
- iOS9适配中出现的一些常见问题
本文主要是说一些iOS9适配中出现的坑,如果只是要单纯的了解iOS9新特性可以看瞄神的开发者所需要知道的 iOS 9 SDK 新特性.9月17日凌晨,苹果给用户推送了iOS9正式版,随着有用户陆续升级 ...
- angular.js 字符串1
<!DOCTYPE html> <html> <head> <meta charset="utf-8"> <script sr ...
- css布局小技巧 2016.03.06
偶遇一个可爱的css布局学习网站,立刻学起来哟- max-width: 当页面左右宽度缩小时,为了避免出现左右滚动条的糟糕体验,就可以用到max-width啦!页面比宽度小时,会自动缩小哦- max- ...
- hdoj 2049 错排
代码: #include <stdio.h> int main(){ int n,a,b,i,j; __int64 s[22],h[22]; s[1]=0; s[2]=1; s[3]=2; ...
- iOS 视频播放横屏,隐藏状态栏
MPMoviePlayerViewController *moviePlayerViewController = [[MPMoviePlayerViewController alloc] init]; ...
- URPF 简单流程
主要功能是防止基于源地址欺骗的网络攻击. 路由器接口一旦使能URPF功能,当该接口收到数据报文时,首先会对数据报文的源地址进行合法性检查,对于源地址合法性检查通过的报文,才会进一步查找去往目的地址的转 ...
- Iis 日志文件默认路径
Iis 日志文件默认路径: C:\WINDOWS\system32\LogFiles
- yii2源码学习笔记(七)
今天继续了解model类 /** 2 * Returns the form name that this model class should use. 3 * 4 * 返回表单的名称,就是这个 mo ...