POJ3683 Priest John's Busiest Day(2-SAT)
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 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
Source
题意:
#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)的更多相关文章
- 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 ...
- poj3683 Priest John's Busiest Day
2-SAT 输出可行解 找可行解的方案就是: 根据第一次建的图建一个反图..然后求逆拓扑排序,建反图的原因是保持冲突的两个事件肯定会被染成不同的颜色 求逆拓扑排序的原因也是为了对图染的色不会发生冲突, ...
- poj3683 Priest John's Busiest Day
2-SAT. 读入用了黄学长的快速读入,在此膜拜感谢. 把每对时间当作俩个点.如果有交叉代表相互矛盾. 然后tarjan缩点,这样就能得出当前的2-SAT问题是否有解. 如果有解,跑拓扑排序就能找出一 ...
- 【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 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 ...
- 图论(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 ...
- 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 ...
随机推荐
- 第38节:hashCode()与toString()与equals()函数的作用,内部类和匿名内部类
hashCode()和toString() Hash算法是把任意长度的数据通过hash算法成为散列值 hashCode() public int hashCode(){ int result = 10 ...
- Kali学习笔记26:OWASP_ZAP
文章的格式也许不是很好看,也没有什么合理的顺序 完全是想到什么写一些什么,但各个方面都涵盖到了 能耐下心看的朋友欢迎一起学习,大牛和杠精们请绕道 OWASP_ZAP扫描器不同于之前介绍的Web扫描器: ...
- Linux服务器开机自动启动服务或脚本的方法
由于种种原因我们需要重启服务器或断电重启,服务都得手动一个一个启动太过麻烦,所以专门了解开机自启脚本的设置方法. 方式一: 直接在脚本/etc/rc.d/rc.local(和/etc/rc.local ...
- 浅谈final关键字的用法
1.final变量: 常和static一起使用,修饰成员变量或者本地变量.修饰后为常量,不可以再次初始化(再次引用),例如public static final String SUCCESS= &qu ...
- python应用-爬取猫眼电影top100
import requests import re import json import time from requests.exceptions import RequestException d ...
- Java核心技术及面试指南 键值对方面的面试题总结以及答案
3.3.5.1如何遍历HashMap对象?尤其请说明通过Iterator遍历HashMap对象的方法. 建议用这种方式: Set<Entry<String,String>>en ...
- java提高(2)---正则表达式(1)常用符号
正则表达式---常用符号 首先声明,我这里列表的是经常使用的一些符号,如果你想得到全部,那建议你通过API中,搜索Pattern类,会得到所有符号. 字符类 [abc] a.b 或 c(简单类) [^ ...
- 如何正确且高效实现OSSIM中文化的解决方案(图文详解)
前言 对于玩OSSIM的初学者或者中级水平的从业人员来说,都有一定必要性从中文看起,当然,最终还是英文的目标迈进,只是说,为了让自己更快速上手! 虽然系统说明支持中文,实际上,只是台湾的繁体中文而 ...
- java~springboot~h2数据库在单元测试中的使用
单元测试有几点要说的 事实上springboot框架是一个tdd框架,你在进行建立项目时它会同时建立一个单元测试项目,而我们的代码用例可以在这个项目里完成,对于单元测试大叔有以下几点需要说明一下: 单 ...
- flex布局常见用法小结
1,display:flex 这个在父容器中声明: 2,flex-direction:row / column 默认为横向,也在父容器中设置,定义flex布局的主轴方向:一条轴为主轴,那么另一条轴自然 ...