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

思路

首先我们发现对于每个婚礼的仪式,可以有两种选择:在婚礼开始和婚礼结束,那么容易想到2-sat问题
然后如果
i婚礼的开始和j婚礼的开始冲突则连接i,!j
i婚礼的开始和j婚礼的结束冲突则连接i,j
i婚礼的结束和j婚礼的开始冲突则连接!i,!j
i婚礼的结束和j婚礼的结束冲突则连接i,j

代码

#include<iostream>
#include<cstdio>
#include<cstring>
#include<cstdlib>
#include<cmath>
#include<vector>
#include<queue>
#include<algorithm>
using namespace std;

const int MAX=10500;

int Col[MAX],In[MAX],len[MAX],Opp[MAX];
int s[MAX],G[MAX],dfn[MAX],low[MAX];
int group=0,top=0,tim=0;
bool vis[MAX];
int n;
vector<int> E[MAX];

struct Time
{
      int b,e;
}t[MAX];
struct Node
{
       int v,next;
}e[MAX*MAX];

int h[MAX],cnt=0;

inline void Add(int u,int v)//连边,从u->v
{
       e[cnt]=(Node){v,h[u]};
       h[u]=cnt++;
}

bool Wrong(int i,int j)//判断两个时间是否矛盾 
{
      if(t[i].b>=t[j].e||t[i].e<=t[j].b)
          return false;
      else
          return true;
         //只要某一个结束时间在另一个的开始时间之前则无矛盾
         //反之必定存在矛盾 
}

void Link()//连边 
{
      for(int i=1;i<=n;++i)
       for(int j=1;j<=n;++j)//枚举任意两组仪式的时间,查看是否存在矛盾  
       {
               if(i==j)continue;//自己和自己没有比较的意义 
               if(Wrong(i,j))//开头和开头矛盾
                  Add(i,j+n);
               if(Wrong(i,j+n))//开头和结尾矛盾 
                  Add(i,j);
               if(Wrong(i+n,j))//结尾和开头矛盾 
                  Add(i+n,j+n);
               if(Wrong(i+n,j+n))//结尾和结尾矛盾 
                  Add(i+n,j);
       }
}

void Tarjan(int u)//缩点 
{
      dfn[u]=low[u]=++tim;
      s[++top]=u;
      vis[u]=true;
      int v;
      for(int i=h[u];i!=-1;i=e[i].next)
      {
           v=e[i].v;
           if(!dfn[v])
           {
               Tarjan(v);
               low[u]=min(low[u],low[v]);
           }
           else
            if(vis[v])
             low[u]=min(low[u],dfn[v]);
      }
      if(low[u]==dfn[u])
      {
             ++group;
             do
             {
                   v=s[top--];
                   vis[v]=false;
                   G[v]=group;
             }while(v!=u&&top);
      }
}

void Top()//拓扑排序 
{
       memset(In,0,sizeof(In));//存入度 
       memset(Col,-1,sizeof(Col));//存颜色
       for(int i=1;i<=2*n;++i)//连接超级点 
       {
             for(int j=h[i];j!=-1;j=e[j].next)//枚举邻边 
             {
                  int v=e[j].v;
                  if(G[i]==G[v])continue;//在同一个强连通分量中无需考虑 
                  E[G[v]].push_back(G[i]);//边要反着存 
                  In[G[i]]++;//统计入度 
             }
       }
       queue<int> Q;//拓扑排序的队列 
       for(int i=1;i<=group;++i)
         if(In[i]==0)Q.push(i);//入度为0的进队 
       while(!Q.empty())
       {
             int v=Q.front();
             Q.pop();
             if(Col[v]==-1)//未染色
             {
                    Col[v]=1;     //染为1 
                    Col[Opp[v]]=0;//对立点染为0 
             }
             for(int i=0;i<E[v].size();++i)//依次减入度 
             {
                  In[E[v][i]]--;
                  if(In[E[v][i]]==0)//入度为0 
                    Q.push(E[v][i]);
             }
       }
}

int main()
{
    while(cin>>n)
    {
          memset(h,-1,sizeof(h));
          cnt=0;tim=group=top=0;
          memset(dfn,0,sizeof(dfn));
          memset(low,0,sizeof(low));
          memset(In,0,sizeof(In));
          memset(vis,0,sizeof(vis));
          memset(Opp,0,sizeof(Opp));
          for(int i=1;i<=n;++i)
          {
               int a,b,c,d,e;
               scanf("%d:%d %d:%d %d",&a,&b,&c,&d,&len[i]);
               //这里都把时间转化为分钟来计算 
               t[i].b=a*60+b;
               t[i+n].e=c*60+d;
               t[i].e=t[i].b+len[i];//计算开头的结束时间 
               t[i+n].b=t[i+n].e-len[i];//计算结尾的开始时间 
               //i   表示第i个婚礼的仪式在开始的时候举行
               //i+m 表示第i个婚礼的仪式在结束的时候举行 
          }

          Link();//连边 

          for(int i=1;i<=2*n;++i)//缩点 
            if(!dfn[i])Tarjan(i);

          for(int i=1;i<=n;++i)//判断是否有解 
          {
               if(G[i]==G[i+n])//某个婚礼的 开始/结束 在同一个强连通分量中,无解 
               {
                    cout<<"NO"<<endl;
                    return 0;
               }
               Opp[G[i]]=G[i+n];//存一下超级点的的对立点 
               Opp[G[i+n]]=G[i];//反向也要存 
          }

          cout<<"YES"<<endl;//不存在矛盾,则有解 

          Top();//Top排序 

          for(int i=1;i<=n;++i)
          {
                if(Col[G[i]]==1)//选择了再婚礼开始时 
                  printf("%.2d:%.2d %.2d:%.2d\n",t[i].b/60,t[i].b%60,t[i].e/60,t[i].e%60);
                else //在婚礼结束时 
                  printf("%.2d:%.2d %.2d:%.2d\n",t[i+n].b/60,t[i+n].b%60,t[i+n].e/60,t[i+n].e%60);
          }
    }

}

猜你喜欢

转载自blog.csdn.net/eric1561759334/article/details/80379683