[2-SAT] 输出路径 POJ3683 Priest John's Busiest Day

Priest John's Busiest Day

Time Limit: 2000MS Memory Limit: 65536K
Total Submissions: 13255 Accepted: 4495 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
#include <iostream>
#include <cstdio>
#include <stack>
#include <queue>
#include <cstring>
using namespace std;

const int mn = 2010, mm = 1010 * 1010 * 2;

int s[mn], e[mn];
bool check(int i, int j)
{
	if (e[j] <= s[i]) return 0;
	if (e[i] <= s[j]) return 0;
	return 1;
}
int ecnt, to[mm], nx[mm], fr[mn];
void addedge(int u, int v)
{
	to[++ecnt] = v;
	nx[ecnt] = fr[u];
	fr[u] = ecnt;
}

int idx, low[mn], dfn[mn];
bool insta[mn];
stack<int> sta;
int Belong, belong[mn];
void tarjan(int u)	/// Tarjan 缩点
{
	low[u] = dfn[u] = ++idx;
	sta.push(u);
	insta[u] = 1;
	for (int i = fr[u]; i != -1; i = nx[i])
	{
		int v = to[i];
		if (!dfn[v])
		{
			tarjan(v);
			low[u] = min(low[u], low[v]);
		}
		if (dfn[v] && insta[v]) low[u] = min(low[u], dfn[v]);
	}
	if (low[u] == dfn[u])
	{
		Belong++;
		int v;
		do
		{
			v = sta.top();
			insta[v] = 0;
			belong[v] = Belong; // 点 v 属于的联通块
			sta.pop();
		}
		while (v != u);
	}
}

int indeg[mn], cf[mn], col[mn];
vector<int> dag[mn];
queue<int> que;
void tuopu(int n)	/// 拓扑排序并染色 
{
	// 多组 初始化
	memset(indeg, 0, sizeof indeg);
	memset(col, 0, sizeof col);
	for (int i = 0; i < n; i++) dag[i].clear();
	while (!que.empty()) que.pop();
	//
	
	for (int u = 0; u < n; u++)
	{
		for (int i = fr[u]; i != -1; i = nx[i])
		{
			int a = belong[u], b = belong[to[i]];
			if (a == b) continue;
			dag[b].push_back(a); /// 反向建边
			indeg[a]++;
		}
	}
	for (int i = 1; i <= Belong; i++) if (!indeg[i]) que.push(i);
	while (!que.empty())
	{
		int u = que.front();
		que.pop();
		if (col[u] == 0) // 当前点未染色 (已保证可行)
		{
			col[u] = 1; // 当前点可选
			col[cf[u]] = 2; // 相反点不可选
		}
		for (int i = dag[u].size() - 1; i >= 0; i--)
		{
			int v = dag[u][i];
			indeg[v]--;
			if (indeg[v] == 0) que.push(v);
		}
	}
}

int main()
{
	int n;
	while (~scanf("%d", &n))
	{
		for (int i = 0; i < n; i++)
		{
			int h1, m1, h2, m2;
			scanf("%d:%d %d:%d", &h1, &m1, &h2, &m2);
			int temp;
			scanf("%d", &temp);
			s[2 * i + 0] = h1 * 60 + m1, e[2 * i + 0] = h1 * 60 + m1 + temp;
			s[2 * i + 1] = h2 * 60 + m2 - temp, e[2 * i + 1] = h2 * 60 + m2;
		}

		ecnt = 0; memset(fr, -1, sizeof fr);
		for (int i = 0; i < n; i++)
		{
			for (int j = i + 1; j < n; j++)
			{
				if (check(2 * i + 0, 2 * j + 0)) // 每场活动的冲突检测
				{
					addedge(2 * i + 0, 2 * j + 1);
					addedge(2 * j + 0, 2 * i + 1);
				}
				if (check(2 * i + 0, 2 * j + 1))
				{
					addedge(2 * i + 0, 2 * j + 0);
					addedge(2 * j + 1, 2 * i + 1);
				}
				if (check(2 * i + 1, 2 * j + 0))
				{
					addedge(2 * i + 1, 2 * j + 1);
					addedge(2 * j + 0, 2 * i + 0);
				}
				if (check(2 * i + 1, 2 * j + 1))
				{
					addedge(2 * i + 1, 2 * j + 0);
					addedge(2 * j + 1, 2 * i + 0);
				}
			}
		}

		idx = 0, Belong = 0;
		memset(low, 0, sizeof low);
		memset(dfn, 0, sizeof dfn);
		memset(insta, 0, sizeof insta);
		while(!sta.empty()) sta.pop();
		
		for (int i = 0; i < 2 * n; i++)
			if (!dfn[i]) tarjan(i);

		bool flag = 1;
		for (int i = 0; i < n; i++)
		{
			cf[belong[2 * i + 0]] = belong[2 * i + 1];	/// 缩点后的同组两个点对立关系
			cf[belong[2 * i + 1]] = belong[2 * i + 0];
			if (belong[2 * i + 0] == belong[2 * i + 1]) flag = 0;
		}
		if (!flag) printf("NO\n");
		else
		{
			printf("YES\n");
			tuopu(2 * n);
			for (int i = 0; i < n; i++)
			{
				if (col[belong[2 * i]] == 1) // 前一个点必选
					printf("%02d:%02d %02d:%02d\n", s[2 * i] / 60, s[2 * i] % 60, e[2 * i] / 60, e[2 * i] % 60);
				else // 后一个点必选
					printf("%02d:%02d %02d:%02d\n", s[2 * i + 1] / 60, s[2 * i + 1] % 60, e[2 * i + 1] / 60, e[2 * i + 1] % 60);
			}
		}
	}
	return 0;
}

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值