Priest John's Busiest Day
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). 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 Sample Output YES Source
POJ Founder Monthly Contest – 2008.08.31, Dagger and Facer
|
题意:有n对人要结婚,牧师分别去主持每个婚礼中的仪式,给出每对新人的开始和结束时间,还有仪式需要的时间,仪式必须要在开始或结束的时间举办,问是否能给出一种方案,使得牧师可以参加所有仪式。
题解:
2-sat。。。
终于把这道题A了。。。
首先,先把每个婚礼的每一段开始时间标记为i*2,结束时间标记为i*2-1。然后缩点,逆向用强联通分量建图。然后用拓扑排序做一遍,输出即可。
安利一个2-sat讲解:2-sat总结
#include<bits/stdc++.h>
using namespace std;
#define MAXN 2010
struct node
{
int begin,end,next;
}edge[*MAXN*MAXN];
struct NODE
{
int begin,end,next;
}edge1[*MAXN*MAXN];
int cnt,Head[MAXN],cnt1,Head1[MAXN],n,s1[MAXN],t1[MAXN],LOW[MAXN],DFN[MAXN],STACK[MAXN],color[MAXN],q[MAXN],op[MAXN],belong[MAXN],SCC,Id[MAXN],SIZE,top,N;
bool INSTACK[MAXN];
void addedge(int bb,int ee)
{
edge[++cnt].begin=bb;edge[cnt].end=ee;edge[cnt].next=Head[bb];Head[bb]=cnt;
}
void addedge1(int bb,int ee)
{
edge1[++cnt1].begin=bb;edge1[cnt1].end=ee;edge1[cnt1].next=Head1[bb];Head1[bb]=cnt1;
}
int read()
{
int s=,fh=;char ch=getchar();
while(ch<''||ch>''){if(ch=='-')fh=-;ch=getchar();}
while(ch>=''&&ch<=''){s=s*+(ch-'');ch=getchar();}
return s*fh;
}
int Judge(int ii,int jj)
{
if(s1[jj]>=t1[ii]||s1[ii]>=t1[jj])return ;
return ;
}
void Build()
{
int i,j;
memset(Head,-,sizeof(Head));cnt=;
for(i=;i<=n;i++)
{
for(j=i+;j<=n;j++)
{
if(Judge(*i,*j)==)
{
addedge(*i,*j-);
addedge(*j,*i-);
}
if(Judge(*i,*j-)==)
{
addedge(*i,*j);
addedge(*j-,*i-);
}
if(Judge(*i-,*j)==)
{
addedge(*i-,*j-);
addedge(*j,*i);
}
if(Judge(*i-,*j-)==)
{
addedge(*i-,*j);
addedge(*j-,*i);
}
}
}
}
void Tarjan(int u)
{
int i,v;
LOW[u]=DFN[u]=++SIZE;
STACK[top++]=u;INSTACK[u]=true;
for(i=Head[u];i!=-;i=edge[i].next)
{
v=edge[i].end;
if(!DFN[v])
{
Tarjan(v);
LOW[u]=min(LOW[u],LOW[v]);
}
else if(INSTACK[v]==true)LOW[u]=min(LOW[u],DFN[v]);
}
if(LOW[u]==DFN[u])
{
SCC++;
do
{
v=STACK[--top];
INSTACK[v]=false;
belong[v]=SCC;
}while(u!=v);
}
}
void solve()
{
SIZE=;
for(int i=;i<=N;i++)if(!DFN[i])Tarjan(i);
}
void dfs(int u)
{
if(color[u]!=)return;
color[u]=-;
for(int i=Head1[u];i!=-;i=edge1[i].next)dfs(edge1[i].end);
}
void Toposort()
{
int head=,tail=,i,u,v;
for(i=;i<=SCC;i++)if(Id[i]==)q[++tail]=i;
while(head<=tail)
{
u=q[++head];
if(color[u]!=)continue;//已经被标记过就不用搜索了.
color[u]=;/*若将color[]标记为,代表被标记为选择(即1).*/dfs(op[u]);/*将和u对立的点标记为选择u的对立点(即-1).*/
for(i=Head1[u];i!=-;i=edge1[i].next)
{
v=edge1[i].end;
Id[v]--;if(Id[v]==)q[++tail]=v;
}
}
}
void write(int k)
{
printf("%.2d:",k/);
printf("%.2d",k%);
}
int main()
{
int S1,S2,T1,T2,D,i;
n=read();
N=;
for(i=;i<=n;i++)
{
S1=read();S2=read();T1=read();T2=read();D=read();
s1[++N]=S1*+S2;t1[N]=s1[N]+D;
t1[++N]=T1*+T2;s1[N]=t1[N]-D;
}
Build();
solve();
for(i=;i<=n;i++)if(belong[*i-]==belong[*i]){printf("NO");return ;}
memset(Head1,-,sizeof(Head1));cnt1=;
for(i=;i<=cnt;i++)
{
if(belong[edge[i].begin]!=belong[edge[i].end]){addedge1(belong[edge[i].end],belong[edge[i].begin]);Id[belong[edge[i].begin]]++;}
}
memset(color,,sizeof(color));
for(i=;i<=n;i++)
{
op[belong[*i]]=belong[*i-];
op[belong[*i-]]=belong[*i];
}
Toposort();
printf("YES");
for(i=;i<=n;i++)
{
printf("\n");
if(color[belong[i*]]==){write(s1[i*]);printf(" ");write(t1[i*]);}
else {write(s1[i*-]);printf(" ");write(t1[i*-]);}
}
fclose(stdin);
fclose(stdout);
return ;
}