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 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
解题思路:
#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;
}