Priest John's Busiest Day
Time Limit: 2000MS   Memory Limit: 65536K
Total Submissions: 10010   Accepted: 3425   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 SiTi and DiSi 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

题目链接:POJ 3683

挑战程序书上的例题,重新学习了一下2-SAT,一般的2-SAT是给出一些矛盾的条件,然后根据这些矛盾的条件用强连通分量算法来做。

首先要知道把什么东西拆点,一般是去拆具有两个对立面的事物,比如这题就是神父出现在开头和神父出现在结尾,两者就是不能同时发生的对立面,然后记这两个对立面为$x_i$与$\lnot x_i$,显然一般情况下如果a与b矛盾那么就是说“a与b不能同时发生”,转换成标记符号就是$\lnot (a \land b)$,然后把这个式子拆开得到$\lnot a\lor\lnot b$,那么得到这样一个析取范式,我们可以将他转换成蕴含式子,即$(a \to \lnot b)\land(b \to \lnot a)$,这显然可以转换成两条有向边$<a, \lnot b>$与$<b, \lnot a>$,然后对图进行DFS得到强连通分量,然后看$\lnot x_i$与$x_i$是否在同一个强连通分量里,如果在同一个scc中显然是矛盾的,这题的话就第i个时间段和第j个时间段的开头和结束位置共4种关心进行判断,冲突就按上面的方法连边再scc处理即可,如果存在方案,只要判断$x_i$所在的连通分量编号与$\lnot x_i$所在的连通分量编号即可

代码:

#include <stdio.h>
#include <iostream>
#include <algorithm>
#include <cstdlib>
#include <sstream>
#include <numeric>
#include <cstring>
#include <bitset>
#include <string>
#include <deque>
#include <stack>
#include <cmath>
#include <queue>
#include <set>
#include <map>
using namespace std;
#define INF 0x3f3f3f3f
#define LC(x) (x<<1)
#define RC(x) ((x<<1)+1)
#define MID(x,y) ((x+y)>>1)
#define CLR(arr,val) memset(arr,val,sizeof(arr))
#define FAST_IO ios::sync_with_stdio(false);cin.tie(0);
typedef pair<int, int> pii;
typedef long long LL;
const double PI = acos(-1.0);
const int N = 1010;
const int MAXV = N * 2;
const int MAXE = N * N * 4 * 2;
struct edge
{
int to, nxt;
edge() {}
edge(int _to, int _nxt): to(_to), nxt(_nxt) {}
};
edge E[MAXE];
int head[MAXV], tot;
int dfn[MAXV], low[MAXV], belong[MAXV], st[MAXV], top, ts, ins[MAXV], sc;
int s[N], t[N], d[N]; void init()
{
CLR(head, -1);
tot = 0;
CLR(dfn, 0);
CLR(low, 0);
top = sc = 0;
CLR(ins, 0);
sc = 0;
}
inline void add(int s, int t)
{
E[tot] = edge(t, head[s]);
head[s] = tot++;
}
void Tarjan(int u)
{
low[u] = dfn[u] = ++ts;
st[top++] = u;
ins[u] = 1;
for (int i = head[u]; ~i; i = E[i].nxt)
{
int v = E[i].to;
if (!dfn[v])
{
Tarjan(v);
low[u] = min(low[u], low[v]);
}
else if (ins[v])
low[u] = min(low[u], dfn[v]);
}
if (low[u] == dfn[u])
{
++sc;
int v;
do
{
v = st[--top];
ins[v] = 0;
belong[v] = sc;
} while (u != v);
}
}
int main(void)
{
int n, i, j;
while (~scanf("%d", &n))
{
init();
for (i = 1; i <= n; ++i)
{
int h1, m1, h2, m2;
scanf(" %d:%d %d:%d %d", &h1, &m1, &h2, &m2, &d[i]);
s[i] = h1 * 60 + m1;
t[i] = h2 * 60 + m2;
}
for (i = 1; i <= n; ++i) //n*n*4*2
{
for (j = 1; j <= n; ++j)
{
if (i == j)
continue;
if (min(s[i] + d[i], s[j] + d[j]) > max(s[i], s[j])) //s-s
{
add(i, j + n);
add(j, i + n);
}
if (min(s[i] + d[i], t[j]) > max(s[i], t[j] - d[j])) //s-t
{
add(i, j);
add(j + n, i + n);
}
if (min(t[i], s[j] + d[j]) > max(t[i] - d[i], s[j])) //t-s
{
add(i + n, j + n);
add(j, i);
}
if (min(t[i], t[j]) > max(t[i] - d[i], t[j] - d[j])) //t-t
{
add(i + n, j);
add(j + n, i);
}
}
}
for (i = 1; i <= (n << 1); ++i)
if (!dfn[i])
Tarjan(i);
int flag = 1;
for (i = 1; i <= n; ++i)
if (belong[i] == belong[i + n])
flag = 0;
if (!flag)
puts("NO");
else
{
puts("YES");
for (i = 1; i <= n; ++i)
{
if (belong[i] < belong[i + n])
printf("%02d:%02d %02d:%02d\n", s[i] / 60, s[i] % 60, (s[i] + d[i]) / 60, (s[i] + d[i]) % 60);
else
printf("%02d:%02d %02d:%02d\n", (t[i] - d[i]) / 60, (t[i] - d[i]) % 60, (t[i]) / 60, (t[i]) % 60);
}
}
}
return 0;
}

POJ 3683 Priest John's Busiest Day(2-SAT+方案输出)的更多相关文章

  1. 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 ...

  2. 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 ...

  3. POJ 3683 Priest John's Busiest Day (2-SAT)

    Priest John's Busiest Day Time Limit: 2000MS   Memory Limit: 65536K Total Submissions: 6900   Accept ...

  4. poj - 3683 - Priest John's Busiest Day(2-SAT)

    题意:有N场婚礼,每场婚礼的开始时间为Si,结束时间为Ti,每场婚礼有个仪式,历时Di,这个仪式要么在Si时刻开始,要么在Ti-Di时刻开始,问能否安排每场婚礼举行仪式的时间,使主持人John能参加所 ...

  5. POJ 3683 Priest John's Busiest Day (2-SAT)

    题意:有n对新人要在同一天结婚.结婚时间为Ti到Di,这里有时长为Si的一个仪式需要神父出席.神父可以在Ti-(Ti+Si)这段时间出席也可以在(Di-Si)-Si这段时间.问神父能否出席所有仪式,如 ...

  6. POJ 3683 Priest John's Busiest Day (2-SAT,常规)

    题意: 一些人要在同一天进行婚礼,但是牧师只有1个,每一对夫妻都有一个时间范围[s , e]可供牧师选择,且起码要m分钟才主持完毕,但是要么就在 s 就开始,要么就主持到刚好 e 结束.因为人数太多了 ...

  7. POJ 3683 Priest John's Busiest Day

    2-SAT简单题,判断一下两个开区间是否相交 #include<cstdio> #include<cstring> #include<cmath> #include ...

  8. POJ 3683 Priest John's Busiest Day[2-SAT 构造解]

    题意: $n$对$couple$举行仪式,有两个时间段可以选择,问是否可以不冲突举行完,并求方案 两个时间段选择对应一真一假,对于有时间段冲突冲突的两人按照$2-SAT$的规则连边(把不冲突的时间段连 ...

  9. POJ 3683 Priest John's Busiest Day 【2-Sat】

    这是一道裸的2-Sat,只要考虑矛盾条件的判断就好了. 矛盾判断: 对于婚礼现场 x 和 y,x 的第一段可以和 y 的第一段或者第二段矛盾,同理,x 的第二段可以和 y 的第一段或者第二段矛盾,条件 ...

随机推荐

  1. winform下读取excel文件并绑定datagridview例子

    首先我要读取这个excel文件然后生成Datable 用winform编程的方式 前台界面: 后台的代码 using System; using System.Collections.Generic; ...

  2. angular2新建组件

    1,使用ng g c hello 创建一个新的组件 它创建了4个文件,并更新了app.module.ts 如果想访问这个组件,只需要添加它的路由 成功访问这个组件 Import语句定义了我们需要用到的 ...

  3. LNMP源码安装脚本

    LNMP安装脚本,脚本环境   #LNMP环境搭建centos6.8 2.6.32-696.28.1.el6.x86_64  nginx:1.12.2   mysql:5.6.36  PHP:5.5. ...

  4. TP5数据库操作方法总结

    一.TP5数据库操作方法 1.name()方法        作用 : 指定默认的数据表名(不含前缀)        示例 : Db::name('weiba_post');        返回 : ...

  5. JS:字符串转成json数据,和json转成字符串方法 iframe获取父级传过来的数据

    字符串转成json数据,和json转成字符串方法 //转为JSON adinfo=JSON.parse(adinfo) //转为字符串 adinfo=JSON.stringify(adinfo) 大概 ...

  6. 哦?原来Python 面试题是这样的,Python面试题No19

    本面试题题库,由公号:非本科程序员 整理发布 第1题:是否遇到过python的模块间循环引用的问题,如何避免它? 这是代码结构设计的问题,模块依赖和类依赖 如果老是觉得碰到循环引用可能的原因有几点: ...

  7. 739. Daily Temperatures

    https://leetcode.com/problems/daily-temperatures/description/ class Solution { public: vector<int ...

  8. V4L2学习(三)框架分析

    整个v4l2的框架分为三层: 在应用层,我们可以在 /dev 目录发现 video0 类似的设备节点,上层的摄像头程序打开设备节点进行数据捕获,显示视频画面.设备节点的名字很统一,video0 vid ...

  9. tomcat8+idea远程调试

    window下 setenv.bat增加 set JPDA_OPTS=-Xrunjdwp:transport=dt_socket,address=8787,server=y,suspend=n lin ...

  10. 3 View视图 URLconf

    1.视图 视图接受Web请求并且返回Web响应 视图就是一个python函数,被定义在views.py中 响应可以是一张网页的HTML内容,一个重定向,一个404错误等等 响应处理过程如下图: 2 准 ...