POJ 3683 (2-SAT)

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

 

差不多就是,模板题了把。。。

2-sat的原理我也讲不清楚。。。有兴趣的去看其他大佬的博客把,个人觉得白书讲的不错

#include <iostream>
#include <cstring>
#include <cstdio>
#include <queue>
#include <map>
using namespace std;
const int MAX = 2050;
int s[MAX], e[MAX], d[MAX];
int n;
struct Edge
{
	int to, next;
} edge[MAX * MAX], redge[MAX * MAX];

int k, rk, head[MAX], rhead[MAX];

void add(int a, int b){
	edge[k].to = b;
	edge[k].next = head[a];
	head[a] = k++;

	redge[rk].to = a;
	redge[rk].next = rhead[b];
	rhead[b] = rk++;
}


int judge(int t1, int t2, int t3, int t4){
	if(t2 <= t3 || t4 <= t1){
		return 1; 
	}
	return 0;
}

vector<int> vec;
int vis[MAX];
void dfs(int u){
	vis[u] = 1;
	for(int i = head[u]; i != -1; i = edge[i].next){
		int to = edge[i].to;
		if(vis[to]){
			continue;
		}
		dfs(to);
	}
	vec.push_back(u);
}

int cmp[MAX];
void redfs(int u, int x){
	vis[u] = 1;
	cmp[u] = x;
	for(int i = rhead[u]; i != -1; i = redge[i].next){
		int to = redge[i].to;
		if(!vis[to]){
			redfs(to, x);
		}
	}
}
int scc(){
	memset(vis, 0, sizeof(vis));
	vec.clear();
	for(int i = 1; i <= 2 * n; i++){
		if(!vis[i]){
			dfs(i);
		}
	}

	memset(vis, 0, sizeof(vis));
	int num = 0;
	int len = vec.size();
	for(int i = len - 1; i >= 0; i--){
		if(!vis[vec[i]]){  //这里 vec[i]之前写成了i,一直wa,哭死。。。QAQ
			redfs(vec[i], ++num);
		}
	}
	return num;
}

void print(int t){
	int h = t / 60;
	printf("%02d:", h);
	printf("%02d", t % 60);
}
int main() {
	scanf("%d", &n);
	memset(head, -1, sizeof(head));
	memset(rhead, -1, sizeof(rhead));
	for(int i = 1; i <= n; i++){
		int h, m;
		scanf("%d:%d", &h, &m);
		s[i] = h * 60 + m;
		scanf("%d:%d", &h, &m);
		e[i] = h * 60 + m;
		scanf("%d", &d[i]);
	}

	for(int i = 1; i <= n; i++){
		for(int j = i + 1; j <= n; j++){
			if(!judge(s[i], s[i] + d[i], s[j], s[j] + d[j])){
				add(i, j + n);
				add(j, i + n);
			}
			if(!judge(s[i], s[i] + d[i], e[j] - d[j], e[j])){
				add(i, j);
				add(j + n, i + n);
			}
			if(!judge(e[i] - d[i], e[i], s[j], s[j] + d[j])){
				add(i + n, j + n);
				add(j, i);
			}
			if(!judge(e[i] - d[i], e[i], e[j] - d[j], e[j])){
				add(i + n, j);
				add(j + n, i);
			}
		}
	}

	scc();
	int flag = 1;
	for(int i = 1; i <= n; i++){
		if(cmp[i] == cmp[i + n]){
			flag = 0;
			break;
		}
	}

	if(flag == 0){
		printf("NO\n");
		return 0;
	}
	printf("YES\n");
	for(int i = 1; i <= n; i++){
		//printf("i = %d n = %d\n", i, n);
		if(cmp[i] > cmp[i + n]){
			print(s[i]);
			printf(" ");
			print(s[i] + d[i]);
			printf("\n");
		} else{
			print(e[i] - d[i]);
			printf(" ");
			print(e[i]);
			printf("\n");
		}
	}
    return 0;
}

 

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值