2-sat->poj 3683 Priest John's Busiest Day

Priest John's Busiest Day
Time Limit: 2000MS Memory Limit: 65536K
Total Submissions: 7323 Accepted: 2498 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 Sito 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 toSi + 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


2-sat, 构图, 求强连通分量, 缩点, 拓扑排序, 输出。

#include <iostream>
#include <algorithm>
#include <string.h>
#include <cstdio>
#include <string>
#include <queue>
#include <stack>

using namespace std;

#define MAXN 2000 + 10

string str1, str2;
int len;

struct Node
{
	int start, end;
}temp[MAXN];

struct Edge
{
	int u, v, next;
}edge[2000 * MAXN], edgetwo[2000 * MAXN];

int head[MAXN], e, cnt, ind, n;
int headtwo[MAXN], etwo;
int dfn[MAXN], low[MAXN], belong[MAXN], cf[MAXN], indegree[MAXN], col[MAXN];
bool ins[MAXN];

void add(int u, int v)
{
	edge[e].u = u;
	edge[e].v = v;
	edge[e].next = head[u];
	head[u] = e++;
}

void insert(int u, int v)
{
	edgetwo[etwo].u = u;
	edgetwo[etwo].v = v;
	edgetwo[etwo].next = headtwo[u];
	headtwo[u] = etwo++;
}

void init()
{
	e = etwo = cnt = ind = 0;
	memset(head, -1, sizeof(head));
	memset(headtwo, -1, sizeof(headtwo));
	memset(indegree, 0, sizeof(indegree));
	memset(col, 0, sizeof(col));
}

int change(int methord)
{
	int res = 0;
	
	if (methord == 1)
	{
		res = ((str1[0] - '0') * 10 + str1[1] - '0') * 60 + (str1[3] - '0') * 10 + str1[4] - '0';
	}
	else
	{
		res = ((str2[0] - '0') * 10 + str2[1] - '0') * 60 + (str2[3] - '0') * 10 + str2[4] - '0';
	}
	
	return res;
}

string changetwo(int val)
{
	string str = "";
	
	str += val / 60 / 10 + '0';
	str += val / 60 % 10 + '0';
	str += ":";
	str += val % 60 / 10 + '0';
	str += val % 60 % 10 + '0';
	
	return str;
}

stack <int> s;

void tarjan(int u)
{
	low[u] = dfn[u] = ++ind;
	
	ins[u] = true;
	
	s.push(u);
	
	for (int i = head[u]; i != -1; i = edge[i].next)
	{
		int v = edge[i].v;
		
		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])
	{
		cnt++;
		
		int x;
		
		do
		{
			x = s.top();
			s.pop();
			ins[x] = false;
			belong[x] = cnt;
		}while (x != u);
	}
}

queue <int> q;

void torpu()
{
	for (int i = 1; i <= cnt; i++)
	{
		if (indegree[i] == 0)
		{
			q.push(i);
		}
	}
	
	while (!q.empty())
	{
		int cur = q.front();
		q.pop();
		
		if (col[cur] == 0)
		{
			col[cur] = 1;
			col[cf[cur]] = -1;
		}
		
		for (int i = headtwo[cur]; i != -1; i = edgetwo[i].next)
		{
			int v = edgetwo[i].v;
			
			if (--indegree[v] == 0)
			{
				q.push(v);
			}
		}
	}
}

void solve()
{
	for (int i = 0; i < 2 * n; i++)
	{
		if (!dfn[i])
		{
			tarjan(i);
		}
	}
	
	bool flag = true;
	
	for (int i = 0; i < n; i++)
	{
		if (belong[2 * i] == belong[i * 2 + 1])
		{
			flag = false;
		}
		
		cf[belong[2 * i]] = belong[2 * i + 1];
		cf[belong[2 * i + 1]] = belong[2 * i];
	}
	
	if (!flag)
	{
		cout << "NO" << endl;
	}
	else
	{
		cout << "YES" << endl;
		
		for (int i = 0; i < e; i++)
		{
			if (belong[edge[i].u] != belong[edge[i].v])
			{
				insert(belong[edge[i].v], belong[edge[i].u]);
				indegree[belong[edge[i].u]]++;
			}
		}
		
		torpu();
		
		for (int i = 0; i < 2 * n; i++)
		{
			if (col[belong[i]] == 1)
			{
				cout << changetwo(temp[i].start) << ' ' << changetwo(temp[i].end) << endl;
			}
		}
	}
}

void input()
{
	init();
	
	cin >> n;
	
	for (int i = 0; i < n; i++)
	{
		cin >> str1 >> str2 >> len;
		temp[2 * i].start = change(1);
		temp[2 * i].end = temp[2 * i].start + len;
		temp[2 * i + 1].end = change(2);
		temp[2 * i + 1].start = temp[2 * i + 1].end - len; 
	}
	
	for (int i = 0; i < 2 * n; i++)
	{
		for (int j = i + 1 + !(i & 1); j < 2 * n; j++)
		{
			if (!(temp[i].end <= temp[j].start || temp[i].start >= temp[j].end))
			{
				add(i, j ^ 1);
				add(j, i ^ 1);
			}
		}
	}
	
	solve();
}

int main()
{
	input();
	return 0;
}


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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值