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

在一个小镇上,John是唯一的神父,每年的9月1日是他最忙碌的一天,因为镇上有一个古老的传说,当天结婚的夫妇将永远得到爱神的祝福。今年有N对夫妇计划在这天结婚,每对夫妇的婚礼都有特定的时间段,且必须在婚礼开始或结束时进行特别的仪式。本篇探讨如何为John安排日程,确保他能出席所有特别仪式。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

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 Diminutes 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

解题思路:

#include <iostream>
#include <algorithm>
#include <string.h>
#include <vector>
#include <stdio.h>
using namespace std;
const int MAXN = 3010;
int sS[MAXN], sT[MAXN], sD[MAXN];  //读取数据
int N,V;
int k = 0;

//强连通分量部分
vector<int> G[MAXN];
vector<int> rG[MAXN];
vector<int> vs;   //序列
bool svis[MAXN];  //结点被访问?
int sbcmp[MAXN];   //连通分量域

void sAdd_Edge(int from, int to) {
	G[from].push_back(to);     //连通分量
	rG[to].push_back(from);
}

void sDfss(int x) {
	svis[x] = true;
	for (int v = 0; v < G[x].size(); ++v) {   //加到边中
		if (!svis[G[x][v]]) sDfss(G[x][v]);
	}
	vs.push_back(x);   //后序遍历
}

void srDfss(int x, int k) {
	svis[x] = true;
	sbcmp[x] = k;
	for (int v = 0; v < rG[x].size(); ++v) {
		if (!svis[rG[x][v]]) srDfss(rG[x][v], k);
	}
}


int main() {
	scanf("%d", &N);
	int h1, m1, h2, m2, d;
	for (int i = 0; i < N; ++i) {   
		scanf("%d:%d %d:%d %d", &h1, &m1, &h2, &m2, &d);  //读取换算成分钟后的数据
		sS[i] = h1 * 60 + m1;
		sT[i] = h2 * 60 + m2;
		sD[i] = d;
	}
	V = N * 2;  
	for (int i = 0; i < N; ++i) {
		for (int j = 0; j < i; ++j) {
			if (min(sS[i] + sD[i], sS[j] + sD[j]) > max(sS[i], sS[j])) {
				sAdd_Edge(i, N + j);
				sAdd_Edge(j, N + i);
			}
			if (min(sS[i] + sD[i], sT[j]) > max(sS[i],sT[j]-sD[j])) {
				sAdd_Edge(i, j);
				sAdd_Edge(N + j, N + i);
			}
			if (min(sT[i], sS[j] + sD[j]) > max(sT[i]-sD[i],sS[j])) {
				sAdd_Edge(N + i, N + j);
				sAdd_Edge(j, i);
			}
			if (min(sT[i], sT[j]) > max(sT[i] - sD[i], sT[j] - sD[j])) {
				sAdd_Edge(N + i, j);
				sAdd_Edge(N + j, i);
			}
		}
	}
	//求强连通分量
	memset(svis, false, sizeof(svis));
	for (int i = 0; i < V; ++i) {
		if (!svis[i]) sDfss(i);
	}

	memset(svis, false, sizeof(svis));
	for (int i = vs.size() - 1; i >= 0; --i) {
		if (!svis[vs[i]]) srDfss(vs[i], k++);   //继续向下搜索
	}


	for (int i = 0; i < N; ++i) {
		if (sbcmp[i] == sbcmp[N + i]) {
			printf("NO\n");   //存在同一个连通分量中,直接输出NO
			system("PAUSE");
			return 0;
		}
	}
	//输出正确,给出一组解
	printf("YES\n");
	for (int i = 0; i < N; ++i) {
		if (sbcmp[i] > sbcmp[N + i]) {   //在结婚开始时举行
			printf("%02d:%02d %02d:%02d\n", sS[i] / 60, sS[i] % 60, (sS[i] + sD[i]) / 60, (sS[i] + sD[i]) % 60);
		}
		else {   //在结婚结束时举行
			printf("%02d:%02d %02d:%02d\n", (sT[i] - sD[i]) / 60, (sT[i] - sD[i]) % 60, sT[i] / 60, sT[i] % 60);
		}
	}

	system("PAUSE");
	return 0;
}

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值