poj3683 Priest John's Busiest Day

不用topsort的,我也不知道为啥。

#include <iostream>
#include <cstring>
#include <cstdio>
using namespace std;
struct Node{
	int fro, too;
}nd[2005];
struct Edge{
	int too, nxt, val;
}edge[4000005];
int uu, vv, ww, n, hea[2005], cnt, dfn[2005], loo[2005], scc, bel[2005];
int sta[2005], din, ind;
bool ins[2005];
bool pd(int x, int y){
	if(nd[x].fro<=nd[y].fro && nd[x].too>nd[y].fro)	return true;
	swap(x, y);
	if(nd[x].fro<=nd[y].fro && nd[x].too>nd[y].fro)	return true;
	return false;
}
void add_edge(int fro, int too){
	edge[++cnt].nxt = hea[fro];
	edge[cnt].too = too;
	hea[fro] = cnt;
}
void tarjan(int u){
	dfn[u] = loo[u] = ++ind;
	sta[++din] = u;
	ins[u] = true;
	for(int i=hea[u]; i; i=edge[i].nxt){
		int t=edge[i].too;
		if(!dfn[t]){
			tarjan(t);
			loo[u] = min(loo[u], loo[t]);
		}
		else if(ins[t])	loo[u] = min(loo[u], dfn[t]);
	}
	int j;
	if(dfn[u]==loo[u]){
		scc++;
		do{
			j = sta[din--];
			ins[j] = false;
			bel[j] = scc;
		}while(dfn[j]!=loo[j]);
	}
}
int main(){
	cin>>n;
	for(int i=0; i<n; i++){
		scanf("%d:%d", &uu, &vv);
		uu = uu * 60 + vv;
		scanf("%d:%d", &vv, &ww);
		vv = vv * 60 + ww;
		scanf("%d", &ww);
		nd[2*i].fro = uu, nd[2*i].too = uu + ww;
		nd[2*i+1].fro = vv - ww, nd[2*i+1].too = vv;
	}
	for(int i=0; i<n+n; i++)
		for(int j=i+1; j<n+n; j++)
			if(pd(i, j)){
				add_edge(i, j^1);
				add_edge(j, i^1);
			}
	for(int i=0; i<n+n; i++)
		if(!dfn[i])
			tarjan(i);
	for(int i=0; i<n+n; i+=2)
		if(bel[i]==bel[i^1]){//说明以前有要求既不能选i又不能选i^1
			printf("NO
");
			return 0;
		}
	printf("YES
");
	for(int i=0; i<n+n; i+=2){
		int id=bel[i]<bel[i^1]?i:i+1;//为什么要这样做?id小说明先求出来,在图里是靠近下游的,影响小
		printf("%02d:%02d %02d:%02d
", nd[id].fro/60, nd[id].fro%60, nd[id].too/60, nd[id].too%60);
	}
	return 0;
}
原文地址:https://www.cnblogs.com/poorpool/p/8006997.html