# poj 3683 Priest John's Busiest Day

// poj 3683 Priest John's Busiest Day
// re re ac 12196K 125MS
#include <iostream>
#include <string>
#include <queue>
#include <stack>
#include <algorithm>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <cstring>
using namespace std;

#define FOR(i,a,b) for(i = (a); i < (b); ++i)
#define FORE(i,a,b) for(i = (a); i <= (b); ++i)
#define FORD(i,a,b) for(i = (a); i > (b); --i)
#define FORDE(i,a,b) for(i = (a); i >= (b); --i)
#define CLR(a,b) memset(a,b,sizeof(a))
#define PB(x) push_back(x)

const int N = 5000;
const int M = 4000010;

int low[N], dfn[N];
int belong[N];
bool inStack[N], vis[N];
stack<int> st;
int n, m;
int step, t;
int conflict[N];//缩点后有矛盾的点
int du[N];//入度
int color[N];//1为红色，-1为蓝色，红色输出
int cnt1, cnt2;
int top[N], cnt;
struct node {
int s, e;
int len;
}c[N];
struct edge1 {
int v, nxt;
}E1[M];
struct edge2 {
int v, nxt;
}E2[M];

void addedge1(int u, int v) {
E1[cnt1].v = v;
}

void addedge2(int u, int v) {
E2[cnt2].v = v;
}

void tarjan(int u) {
int i;
step++;
st.push(u);
low[u] = dfn[u] = step;
vis[u] = 1;
inStack[u] = 1;
for(i = head1[u]; i != -1; i = E1[i].nxt) {
int x = E1[i].v;
if(!vis[x]) {
tarjan(x);
low[u] = min(low[u], low[x]);
}
else if(inStack[x])
low[u]=min(low[u], dfn[x]);
}
if(low[u] == dfn[u]) {
t++;
while(1) {
int x = st.top();
st.pop();
belong[x] = t;
inStack[x] = 0;
if(x == u)
break;
}
}
}

void init() {
cnt1 = cnt2 = step = t = 0;
CLR(du, 0);
CLR(color, 0);
CLR(vis, 0);
CLR(inStack, 0);
}

bool isConflict(int a, int b, int c, int d) {
if(a >= d || b <= c)
return false;
return true;
}

void Build() {
int i,j;
init();
FORE(i, 1, n - 1)
FORE(j, i + 1, n) {
if(isConflict(c[i].s, c[i].s + c[i].len, c[j].s, c[j].s + c[j].len)) {
}
if(isConflict(c[i].s, c[i].s + c[i].len, c[j].e - c[j].len, c[j].e)) {
addedge1(j + n, i + n);
}
if(isConflict(c[i].e - c[i].len, c[i].e, c[j].e - c[j].len, c[j].e)) {
}
if(isConflict(c[i].e - c[i].len, c[i].e, c[j].s, c[j].s + c[j].len)) {
addedge1(i + n, j + n);
}
}
}

void Rebuild(){//逆图
int i, j;

FORE(i, 1, 2 * n) {
for(j = head1[i]; j != -1; j = E1[j].nxt) {
int a = belong[i], b = belong[E1[j].v];
if(a != b) {
du[a]++;
}
}
}
FORE(i, 1, n) {
int a = belong[i], b = belong[i + n];
conflict[a] = b;//缩点后有矛盾的点
conflict[b] = a;
}
}

void topsort() {
int i,j;

queue<int> q;
FORE(i, 1, t)
if(du[i] == 0)
q.push(i);
cnt = 0;
while(!q.empty()) {
int x = q.front();
top[++cnt] = x;
q.pop();
for(i = head2[x]; i != -1; i = E2[i].nxt) {
int tmp = E2[i].v;
du[tmp]--;
if(du[tmp] == 0)
q.push(tmp);
}
}
}

void dfs_Blue(int u) {
int i;
color[u] = -1;
for(i = head2[u]; i != -1; i = E2[i].nxt) {
int x = E2[i].v;
if(color[x] == 0)
dfs_Blue(x);
}
}

void dfs_Red() {
int i, j;

FORE(i, 1, cnt) {
int x = top[i];
if(color[x] == 0) {
color[x] = 1;//Red
dfs_Blue(conflict[x]);//把所有与x有矛盾的点及其子孙染蓝色
}
}
}

void output() {
int i, j, t1, t2;

printf("YES\n");
FORE(i, 1, n) {
int x = belong[i], y = belong[i + n];
if(color[x] == 1) {
t1 = c[i].s;
t2 = c[i].s + c[i].len;
}
if(color[y] == 1) {
t1 = c[i].e - c[i].len;
t2 = c[i].e;
}
printf("%02d:%02d %02d:%02d\n", t1 / 60, t1 % 60, t2 / 60, t2 % 60);
}
}

void solve() {
int i, j;
FORE(i, 1, 2 * n)
if(!vis[i])
tarjan(i);
FORE(i, 1, n)
if(belong[i] == belong[i + n]) {
printf("NO\n");
return ;
}
Rebuild();
topsort();
dfs_Red();
output();
}

int main() {
while(scanf("%d", &n) != EOF) {
int i, j;
FORE(i, 1, n) {
int h1, m1, h2, m2;
scanf("%d:%d %d:%d %d", &h1, &m1, &h2, &m2, &c[i].len);
c[i].s = h1 * 60 + m1;
c[i].e = h2 * 60 + m2;
}
Build();
solve();
}
return 0;
}


• 本文已收录于以下专栏：

## POJ3683——Priest John's Busiest Day

Description John is the only priest in his town. September 1st is the John's busiest day in a y...

## 【POJ 3683】Priest John's Busiest Day

【POJ 3683】Priest John's Busiest Day 2-SAT可行性判断+方案输出模板题~ UPD:对2—SAT的理解以及代码完善~...

## poj 3683 Priest John's Busiest Day 【2-sat 经典建图输出一组解】【有向图tarjan + 反向拓扑 + 染色】

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

## 2-SAT——5.0（poj3683 Priest John's Busiest Day）

poj3683 Priest John's Busiest Day 题意：一个小镇里面只有一个牧师，现在有些新人要结婚，需要牧师分别去主持一个仪式，给出每对新人婚礼的开始时间 s 和结束时间 t ，...

## poj3683Priest 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 y...

## POJ3683 Priest John's Busiest Day(神父约翰的忙日)题解(2-SAT及布尔方程运用)

• DareXK
• 2017年03月11日 00:11
• 329

举报原因： 您举报文章：poj 3683 Priest John's Busiest Day 色情 政治 抄袭 广告 招聘 骂人 其他 (最多只允许输入30个字)