解题思路:大概讲一下建图思路:首先将一个婚礼分成有个点,分别表示在开头神父到,和在末尾神父到。然后判断各个婚礼之间的矛盾情况,按2SAT传统连边。
#include<cstdio>
#include<cstring>
#include<stack>
#include<algorithm>
using namespace std;
const int MAXN=10080;
struct node
{
int st,ed;
}t[2008];
struct edge
{
int u,v,next;
}s[4000008];
int cnt,head[2008],dfn[2008],low[2008],ins[2008],top,color[2008],num,tim,stck[2008],oppo[2008];
int deg[2008],jd[2008],n;
stack<int> q;
bool judge(int i1,int i2)
{
if(t[i1].ed<=t[i2].st||t[i2].ed<=t[i1].st)
return false;
return true;
}
void add_edge(int u,int v)
{
s[cnt].u=u;
s[cnt].v=v;
s[cnt].next=head[u];
head[u]=cnt++;
}
void tarjan(int u)
{
int tp;
//printf("%d\n",u);
ins[u]=1;
stck[top++]=u;
dfn[u]=low[u]=tim++;
for(int i=head[u];i!=-1;i=s[i].next)
{
int v=s[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])
{
num++;
do
{
tp=stck[--top];
ins[tp]=0;
color[tp]=num;
}while(tp!=u);
}
}
void topo()
{
while(!q.empty()) q.pop();
for(int i=1;i<=num;i++)
{
if(deg[i]==0) q.push(i);
}
while(!q.empty())
{
int u=q.top();
q.pop();
if(jd[u]==0) jd[u]=1,jd[oppo[u]]=-1;
for(int i=head[u];i!=-1;i=s[i].next)
{
int v=s[i].v;
deg[v]--;
if(deg[v]==0)
q.push(v);
}
}
}
void solve()
{
memset(dfn,0,sizeof(dfn));
memset(ins,0,sizeof(ins));
top=0;
num=0;
tim=1;
for(int i=0;i<2*n;i++)
{
if(!dfn[i])
tarjan(i);
}
//printf("%d",num);
for(int i=0;i<n;i++)
{
if(color[i]==color[i+n])
{
printf("NO\n");
return;
}
oppo[color[i]]=color[i+n];
oppo[color[i+n]]=color[i];
}
int tp=cnt;
memset(head,-1,sizeof(head));
memset(deg,0,sizeof(deg));
for(int i=0;i<tp;i++)
{
if(color[s[i].v]==color[s[i].u]) continue;
add_edge(color[s[i].v],color[s[i].u]);
deg[color[s[i].u]]++;
}
memset(jd,0,sizeof(jd));
topo();
printf("YES\n");
for(int i=0;i<n;i++)
{
//printf("%d*",jd[color[i]]);
if(jd[color[i]]==1)
{
//printf("*%d*",i);
printf("%02d:%02d %02d:%02d\n",t[i].st/60,t[i].st%60,t[i].ed/60,t[i].ed%60);
}else
{
printf("%02d:%02d %02d:%02d\n",t[i+n].st/60,t[i+n].st%60,t[i+n].ed/60,t[i+n].ed%60);
}
}
}
int main()
{
int a1,a2,b1,b2,c;
// freopen("t.txt","r",stdin);
while(scanf("%d",&n)!=EOF)
{
for(int i=0;i<n;i++)
{
scanf("%d:%d %d:%d %d",&a1,&a2,&b1,&b2,&c);
//printf("%d:%d %d:%d %d\n",a1,a2,b1,b2,c);
t[i].st=a1*60+a2;
t[i+n].ed=b1*60+b2;
t[i].ed=t[i].st+c;
t[i+n].st=t[i+n].ed-c;
}
memset(head,-1,sizeof(head));
cnt=0;
for(int i=0;i<n;i++)
{
for(int j=i+1;j<n;j++)
{
if(judge(i,j))
{
//printf("%d %d\n",i,j);
add_edge(i,j+n);
add_edge(j,i+n);
}
if(judge(i+n,j))
{
//printf("%d %d\n",i+n,j);
add_edge(i+n,j+n);
add_edge(j,i);
}
if(judge(i+n,j+n))
{
//printf("%d %d\n",i+n,j+n);
add_edge(i+n,j);
add_edge(j+n,i);
}
if(judge(i,j+n))
{
//printf("%d %d\n",i,j+n);
add_edge(i,j);
add_edge(j+n,i+n);
}
}
}
solve();
}
return 0;
}