图论(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 ...
随机推荐
- CentOS 6.7增加SWAP交换分区
任务:新增一个1GB的SWAP分区,并开机自动挂载 1.在/var目录下新增SWAPFILE交换区文件 2.生成SWAP分区 mkswap /var/SWAPFILE 3.激活SWAP分区 swapo ...
- Linux - CentOS6.5服务器搭建与初始化配置详解(上)
1.新建一个虚拟机 选择典型 单机下一步 p.MsoNormal,li.MsoNormal,div.MsoNormal { margin: 0cm 0cm 0.0001pt; text-align: ...
- python版本简历
- PHP操作Oracle数据库
原文出处 (这是来自“百度文库”中的文章写得很不错) PHP操作Oracle数据库(OCI数据抽象层)OCI(Oracle 8 Call-Interface)是PHP中内置的数据库抽象层函数.下面针对 ...
- html px em pt长度单位(像素 相对长度 点)知识(转)
html px em pt单位区 一.PX\EM\PT单位介绍 px单位名称为像素,相对长度单位,像素(px)是相对于显示器屏幕分辨率而言的国内推荐:em单位名称为相对长度单位.相对于当前对象内文本的 ...
- cocos2dx Hello world 创建
环境搭建好后,就要开始创建自己的第一个hello world项目了 因为没有安装其他的插件,所以最开始只能手动创建 首先通过cmd 进入你的cocos2dx的路径下: D:\soft\cocos2d- ...
- angularjs 将带标签的内容解析后返回
参考地址:http://okashii.lofter.com/post/1cba87e8_29e0fab 引入angular-sanitize.js文件 注入ngSanitize 页面数据绑定 ng- ...
- How to customize authentication to my own set of tables in asp.net web api 2?
ssuming your table is called AppUser, convert your own AppUser domain object to IUser(using Microsof ...
- Jmeter软件测试1--webservice测试
写在前言 程序猿一枚,原本就是负责安安静静的撸代码,后来公司让兼任下测试的工作,还得照顾下面的几个测试兄弟,无奈本人毫无软件测试理论知识,下面的测试兄弟也是初级水平,又面临公司要求做webservic ...
- Oracle IN 传递字符串参数查询失效
在写存储过程中有如下代码: FOR a IN ( SELECT a.svo_no,a.AUDIT_NO,a.order_id FROM TT_PI_MODEL_REL a ) LOOP SELECT ...