[POJ 3683] Priest John‘s Busiest Day | 2-SAT +Tarjan缩点跑拓扑排序

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

Source
题意:
给出n个婚礼,每个婚礼有个开始的时间和结束的时间,在婚礼期间,要举行持续时间为D的活动,这个活动只能在婚礼期间的前D时间内举行,或者是在婚礼期间的后D时间内举行,问能否安排一种方式使得能够参与n个婚礼的活动
思路:
每个活动有两个选择,而且还要满足一定的条件,这显然是一个2-SAT问题
方法:
2-SAT + Tarjan

首先根据两种选择是否首尾冲突进行连边,如果冲突,则连对立的边

for(int i=1; i<=n; i++) {
		for(int j=1; j<=n; j++) {
			if(i == j) continue;
			if(notFit(i,j)) add(i,j+n);
			if(notFit(i,j+n)) add(i,j);
			if(notFit(i+n,j)) add(i+n,j+n);
			if(notFit(i+n,j+n)) add(i+n,j);
		}
	}

根据建立的图,进行缩点,缩点后可以得到强连通分量的数量以及每个点属于哪一个强连通分量
求完SCC之后,判断两种情况是否冲突(在同一个SCC之内),如果冲突就直接输出“NO”即可
在判断的过程中同时记录对立的点所在的联通块的编号
其余的均为有解(“YES”)的情况

for(int i=1; i<=n; i++) {
		if(pos[i] == pos[i+n]) {
			puts("NO");
			return 0;
		}
		op[pos[i]] = pos[i+n];
		op[pos[i+n]] = pos[i];
	}

然后建立反图统计入度,跑拓扑排序,在拓扑排序的过程当中,如果对于一个没有被标记的点就染色为颜色1,将其对立的点染色为0

void topoSort() {
	int lim = 2*n;
	for(int i=1; i<=lim; i++) {
		for(int j=head[i]; ~j; j=e[j].nex) {
			int u = i,v = e[j].to;
			if(pos[u] == pos[v]) continue;
			edg[pos[v]].push_back(pos[u]);
			++ indeg[pos[u]];///indeg
		}
	}
	for(int i=1; i<=cntSCC; i++) {
		if(indeg[i] == 0) que.push(i);
	}

	while(que.size()) {
		int u = que.front();
		que.pop();
		if(col[u] == -1) {
			col[u] = 1;
			col[op[u]] = 0;
		}
		for(int i=edg[u].size()-1; i>=0; i--) {
			int v = edg[u][i];
			indeg[v] --;
			if(indeg[v] == 0) {
				que.push(v);
			}
		}
	}
}

最后遍历n个点的染色信息,给出对应的输出即可:

ac_code:

int n,m;
int cnt,head[maxn << 1];
struct node {
	int to,nex;
} e[2000007];
void init() {
	cnt = 0;
	Clear(head,-1);
}
void add(int u,int v) {
	e[cnt].to = v;
	e[cnt].nex = head[u];
	head[u] = cnt ++;
}
int dfn[maxn<<1],low[maxn<<1],dfc,cntSCC,pos[maxn<<1];
stack<int> stk;
bool inStk[maxn<<1];
void Tarjan(int u) {
	dfn[u] = low[u] = ++dfc;
	stk.push(u);
	inStk[u] = true;
	for(int i=head[u]; ~i; i=e[i].nex) {
		int to = e[i].to;
		if(!dfn[to]) {
			Tarjan(to);
			low[u] = min(low[u],low[to]);
		} else if(inStk[to]) {
			low[u] = min(low[u],dfn[to]);
		}
	}
	if(low[u] == dfn[u]) {
		int tp;
		++ cntSCC;
		do {
			tp = stk.top();
			stk.pop();
			pos[tp] = cntSCC;
			inStk[tp] = false;
		} while(tp != u);
	}
}
int a[maxn<<1],b[maxn<<1],op[maxn<<1];
int sth,stm,edh,edm,l;
bool notFit(int x,int y) {
	if(b[x] <= a[y] || b[y] <= a[x]) return false;
	return true;
}
queue<int> que;
vector<int> edg[maxn<<1];
int indeg[maxn<<1],col[maxn<<1];
void topoSort() {
	int lim = 2*n;
	for(int i=1; i<=lim; i++) {
		for(int j=head[i]; ~j; j=e[j].nex) {
			int u = i,v = e[j].to;
			if(pos[u] == pos[v]) continue;
			edg[pos[v]].push_back(pos[u]);
			++ indeg[pos[u]];///indeg
		}
	}
	for(int i=1; i<=cntSCC; i++) {
		if(indeg[i] == 0) que.push(i);
	}

	while(que.size()) {
		int u = que.front();
		que.pop();
		if(col[u] == -1) {
			col[u] = 1;
			col[op[u]] = 0;
		}
		for(int i=edg[u].size()-1; i>=0; i--) {
			int v = edg[u][i];
			indeg[v] --;
			if(indeg[v] == 0) {
				que.push(v);
			}
		}
	}
}
int main() {
	init();
	n = read;
	for(int i=1; i<=n; i++) {
		scanf("%d:%d %d:%d %d",&sth,&stm,&edh,&edm,&l);
		a[i] = sth * 60 + stm;
		b[i] = a[i] + l;
		b[i+n] = edh * 60 + edm;
		a[i+n] = b[i+n] - l;
	}
	for(int i=1; i<=n; i++) {
		for(int j=1; j<=n; j++) {
			if(i == j) continue;
			if(notFit(i,j)) add(i,j+n);
			if(notFit(i,j+n)) add(i,j);
			if(notFit(i+n,j)) add(i+n,j+n);
			if(notFit(i+n,j+n)) add(i+n,j);
		}
	}
	Clear(dfn,0);
	Clear(low,0);
	for(int i=1; i<=2*n; i++) {
		if(!dfn[i]) Tarjan(i);
	}///
	for(int i=1; i<=n; i++) {
		if(pos[i] == pos[i+n]) {
			puts("NO");
			return 0;
		}
		op[pos[i]] = pos[i+n];
		op[pos[i+n]] = pos[i];
	}
	Clear(col,-1);
	Clear(indeg,0);
	topoSort();
	puts("YES");
	for(int i=1; i<=n; i++) {
		if(col[pos[i]]) {
			printf("%02d:%02d %02d:%02d\n",a[i]/60,a[i]%60,b[i]/60,b[i]%60);
		} else printf("%02d:%02d %02d:%02d\n",a[i+n]/60,a[i+n]%60,b[i+n]/60,b[i+n]%60);
	}
	return 0;
}
/**
2
08:00 09:00 30
08:15 09:00 20

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值