题目链接:http://poj.org/problem?id=3683
题目大意:给定每场婚礼的起始时间(s~T),而且每次婚礼必须在开头或结尾(二选一)花D时间用来主持( [S,S+D) 或[T-D,T) ),而且主持不能同时进行,问是否存在一种方案使得所有婚礼均能被主持,能的话把方案输出.
题目思路:2-SAT问题,完整的2-SAT算法(详见http://wenku.baidu.com/view/6a6d38000740be1e650e9a6b.html).
代码:
#include <stdlib.h>
#include <string.h>
#include <stdio.h>
#include <ctype.h>
#include <math.h>
#include <time.h>
#include <stack>
#include <queue>
#include <map>
#include <set>
#include <vector>
#include <string>
#include <iostream>
#include <algorithm>
using namespace std;
#define ull unsigned __int64
//#define ll __int64
//#define ull unsigned long long
#define ll long long
#define son1 New(p.xl,xm,p.yl,ym),(rt<<2)-2
#define son2 New(p.xl,xm,min(ym+1,p.yr),p.yr),(rt<<2)-1
#define son3 New(min(xm+1,p.xr),p.xr,p.yl,ym),rt<<2
#define son4 New(min(xm+1,p.xr),p.xr,min(ym+1,p.yr),p.yr),rt<<2|1
#define lson l,mid,rt<<1
#define rson mid+1,r,rt<<1|1
#define middle (l+r)>>1
#define MOD 1000000007
#define esp (1e-8)
const int INF=0x3F3F3F3F;
const double DINF=10000.00;
//const double pi=acos(-1.0);
const int M=2010;
int min(int x,int y){return x<y? x:y;}
int max(int x,int y){return x>y? x:y;}
void swap(int& x,int& y){int t=x;x=y;y=t;}
int T,cas;
vector<int>init[M],now[M];
int low[M],pre[M],idx[M],ss[M],Hash[M],sym[M];
int n,nn,Index,top,m,N,deg[M];
struct node{int s,t;}pot[M];
void dfs(int u){
low[u]=pre[u]=++Index;
ss[++top]=u;
for(int i=0;i<init[u].size();i++){
int v=init[u][i];
if(pre[v]==-1) dfs(v),low[u]=min(low[u],low[v]);
else if(idx[v]==-1) low[u]=min(low[u],pre[v]);
}
if(low[u]==pre[u]){
int v=-1;
now[++nn].clear();
while(u!=v) idx[v=ss[top--]]=nn;
}
}
void Tarjan(){
memset(idx,-1,sizeof(int)*(n+1));
memset(pre,-1,sizeof(int)*(n+1));
Index=top=nn=0,ss[0]=-1;
for(int u=1;u<=n;u++) if(pre[u]==-1) dfs(u);
}
void shrink(){
memset(deg,0,sizeof(int)*(nn+1));
memset(Hash,-1,sizeof(int)*(nn+1));
for(int u=1;u<=nn;u++){
for(int i=0;i<init[u].size();i++){
int v=init[u][i];
if(idx[u]!=idx[v] /*&& Hash[idx[u]]!=idx[v]*/){
//Hash[idx[u]]=idx[v];
now[idx[v]].push_back(idx[u]);
deg[idx[u]]++;
}
}
}
}
bool isCross(int i,int j){
if(pot[i].s<pot[j].t && pot[j].s<pot[i].t) return true;
return false;
}
struct cmp{
bool operator () (int a,int b){
return a > b;
}
};
void _sof(){
Tarjan();
for(int i=1;i<=n;i+=2){
if(idx[i]==idx[i+1]){puts("NO");return;}
sym[idx[i]]=idx[i+1],sym[idx[i+1]]=idx[i];
}
puts("YES");
shrink();
for(int i=1;i<=nn;i++) if(!deg[i]) ss[++top]=i,deg[i]=-1,deg[sym[i]]=-2;
while(top){
int u=ss[top--];
for(int i=0;i<now[u].size();i++){
int v=now[u][i];
if(--deg[v]==0) ss[++top]=v,deg[v]=-1,deg[sym[v]]=-2;
}
}
for(int i=1;i<=n;i++) if(deg[idx[i]]==-1){
int sh,sm,th,tm;
sh=pot[i].s/60,sm=pot[i].s%60;
th=pot[i].t/60,tm=pot[i].t%60;
printf("%02d:%02d %02d:%02d\n",sh,sm,th,tm);
}
}
void _init(){
int i,j,sh,sm,th,tm,d,u,uu,v,vv;
for(i=0;i<N;i++){
scanf("%d:%d %d:%d %d",&sh,&sm,&th,&tm,&d);
u=i<<1|1,uu=u+1;
pot[u].s=sh*60+sm,pot[uu].t=th*60+tm;
pot[u].t=pot[u].s+d,pot[uu].s=pot[uu].t-d;
}
for(u=1,n=N<<1;u<=n;u++) init[u].clear();
for(u=1;u<=n;u++){
uu= u&1? u+1:u-1;
for(v=1;v<=n;v++) if(v!=u && v!=uu){
vv= v&1? v+1:v-1;
if(isCross(u,v)) init[u].push_back(vv);
}
}
_sof();
}
int main(){
//freopen("1.in","r",stdin);
//freopen("1.out","w",stdout);
//_init()
//scanf("%d",&T);for(cas=1;cas<=T;cas++) _init();
while(~scanf("%d",&N)) _init();
return 0;
}