博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
poj 3683: Priest John's Busiest Day(2-sat 输出解)
阅读量:4684 次
发布时间:2019-06-09

本文共 4707 字,大约阅读时间需要 15 分钟。

www.cnblogs.com/shaokele/


Priest John's Busiest Day

  Time Limit: 2 Sec

  Memory Limit: 64 MB

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
  

题目地址:  

题目大意:

  题目已经很简洁了>_<

题解:

  先判是否有解

  然后对于 A 和 'A
  它们所在的联通块是不能同时选的
  重新建图
  拓扑一下搞出顺序
  就完了


AC代码

//poj3683#include 
#include
using namespace std;const int N=1e3+5;int n,cnt,_cnt;int S1[N],T1[N],S2[N],T2[N],last[N<<1],_last[N<<1];int op[N<<1];inline int read(){ int x=0,f=1;char ch=getchar(); while(ch<'0'||ch>'9'){if(ch=='-')f=-1;ch=getchar();} while(ch>='0'&&ch<='9'){x=x*10+ch-'0';ch=getchar();} return x*f;}bool judge(int S1,int T1,int S2,int T2){ if(T1<=S2 || S1>=T2)return 0; return 1;}struct edge{ int to,next;}e[(N*N)<<2],_e[(N*N)<<2];void add_edge(int u,int v){ e[++cnt]=(edge){v,last[u]};last[u]=cnt;}int in[N<<1];void _add_edge(int u,int v){ in[v]++; _e[++_cnt]=(edge){v,_last[u]};_last[u]=_cnt;}int ind,top,scc;int q[N<<1],dfn[N<<1],low[N<<1],col[N<<1];bool inq[N<<1];void Tarjan(int u){ dfn[u]=low[u]=++ind; q[++top]=u;inq[u]=1; for(int i=last[u];i;i=e[i].next){ int v=e[i].to; if(!dfn[v]){ Tarjan(v); low[u]=min(low[u],low[v]); } else if(inq[v]) low[u]=min(low[u],dfn[v]); } if(low[u]==dfn[u]){ int now=0;scc++; while(now!=u){ now=q[top--]; col[now]=scc; inq[now]=0; } }}int mark[N<<1];void dfs(int u){ if(mark[u])return; mark[u]=-1; for(int i=_last[u];i;i=_e[i].next) dfs(_e[i].to);}void topsort(){ top=0; for(int i=1;i<=scc;i++) if(!in[i])q[++top]=i; while(top){ int u=q[top--]; if(mark[u])continue; mark[u]=1;dfs(op[u]); for(int i=_last[u];i;i=_e[i].next){ int v=_e[i].to; in[v]--; if(!in[v])q[++top]=v; } }}void print(int x){ printf("%.2d:",x/60); printf("%.2d ",x%60);}int main(){ n=read(); for(int i=1;i<=n;i++){ S1[i]=read()*60+read(); T2[i]=read()*60+read(); int x=read(); T1[i]=S1[i]+x; S2[i]=T2[i]-x; } for(int i=1;i<=n;i++) for(int j=i+1;j<=n;j++){ if(judge(S1[i],T1[i],S1[j],T1[j])){ add_edge(i*2,j*2-1); add_edge(j*2,i*2-1); } if(judge(S1[i],T1[i],S2[j],T2[j])){ add_edge(i*2,j*2); add_edge(j*2-1,i*2-1); } if(judge(S2[i],T2[i],S1[j],T1[j])){ add_edge(i*2-1,j*2-1); add_edge(j*2,i*2); } if(judge(S2[i],T2[i],S2[j],T2[j])){ add_edge(i*2-1,j*2); add_edge(j*2-1,i*2); } } for(int i=1;i<=n*2;i++) if(!dfn[i])Tarjan(i); for(int i=1;i<=n;i++) if(col[i*2-1]==col[i*2]){ puts("NO"); return 0; } puts("YES"); for(int u=1;u<=2*n;u++) for(int i=last[u];i;i=e[i].next) if(col[u]!=col[e[i].to]) _add_edge(col[e[i].to],col[u]); for(int i=1;i<=n;i++){ op[col[i*2]]=col[i*2-1]; op[col[i*2-1]]=col[i*2]; } topsort(); for(int i=1;i<=n;i++) if(mark[col[i*2]]==1) print(S1[i]),print(T1[i]),puts(""); else print(S2[i]),print(T2[i]),puts(""); return 0;}

转载于:https://www.cnblogs.com/shaokele/p/9455438.html

你可能感兴趣的文章
PHP 在5.1.* 和5.2.*之间 PDO数据库操作中的不同!
查看>>
导入properties时的坑
查看>>
python——网络编程
查看>>
Spark的39个机器学习库
查看>>
Electron学习笔记(一)
查看>>
Java并发编程:CountDownLatch、CyclicBarrier和Semaphore
查看>>
配置NRPE的通讯
查看>>
VS2005编译VTK5.10.1
查看>>
shp系列(一)——利用C++进行shp文件的读(打开)与写(创建)开言
查看>>
总结上海永辉云商高级前端职位面试题集
查看>>
中国计算机学会推荐国际学术会议和期刊目录
查看>>
文本元素
查看>>
各种可以远程
查看>>
对服务器的认识
查看>>
分治法实现1-N的数字按字典序全排列组合 Java语言
查看>>
序列化 与 反序列化
查看>>
购物车
查看>>
矩阵中路径数目问题
查看>>
NSURLRequest POST方式请求服务器示例
查看>>
《精通并发与Netty》学习笔记(07 - 基于Thrift实现Java与Python的RPC调用)
查看>>