POJ 3683 Priest John's Busiest Day(2-sa路径输出,4级)

K - Priest John's Busiest Day
Time Limit:2000MS     Memory Limit:65536KB     64bit IO Format:%I64d & %I64u
Submit  Status
Appoint description:  System Crawler  (2013-05-30)

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 SiTi and DiSi 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 Nlines 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

思路:正常建图就行了。输出路径用拓扑排序,染色就OK 了。


#include<cstdio>
#include<iostream>
#include<cstring>
#define FOR(i,a,b) for(int i=a;i<=b;++i)
#define clr(f,z) memset(f,z,sizeof(f))
using namespace std;
const int mp=2e3+9;
const int me=2e3+9;
int AA[me],BB[me];
int A[me],B[me],T[me];
class Edge
{
 public:int u,v,next;
};

class TWO_SAT
{
 public:
 int e_to[mp],dfn[mp],stack[mp],edge;
 int head[mp],dfs_clock,bcc,top;
 int qhead[mp],od[mp],mark[mp],hh,tt,que[mp],cor[mp];
 Edge e[mp*mp*2];
 void clear()
 {
  clr(head,-1);edge=0;clr(qhead,-1);
 }
 void add(int u,int v,int*head)
 {
  e[edge].u=u; e[edge].v=v;e[edge].next=head[u];head[u]=edge++;
 }
 void add_clause(int x,int xval,int y,int yval)///x or y
 {
  x=x+x+xval;y=y+y+yval;
  add(x^1,y,head);add(y^1,x,head);
 }
// void add_my(int x,int xval,int y,int yval)
// {
//  x=x+x+xval;y=y+y+yval;
//  add(x,y);
// }
// void add_con(int x,int xval)
// {
//  x=x+x+xval;
//  add(x^1,x);
// }
 int tarjan(int u)
 {
  int lowu,lowv,v;
  lowu=dfn[u]=++dfs_clock;
  stack[top++]=u;
  for(int i=head[u];~i;i=e[i].next)
  {
    v=e[i].v;
    if(!dfn[v])
    {
     lowv=tarjan(v);
     lowu=min(lowu,lowv);
    }
    else if(e_to[v]==-1)
    lowu=min(dfn[v],lowu);
  }
  if(lowu==dfn[u])
  {
   ++bcc;
   do
   {
    v=stack[--top];
    e_to[v]=bcc;
   }while(v^u);
  }
  return lowu;
 }
 bool find_bcc(int n)
 {
  top=bcc=dfs_clock=0;
  clr(dfn,0);clr(e_to,-1);clr(od,0);
  for(int i=0;i<n;++i)
  if(!dfn[i])tarjan(i);
  for(int i=0;i<n;i+=2)
  { mark[ e_to[i] ]=e_to[i^1];
    mark[ e_to[i^1] ]=e_to[i];
   if(e_to[i]==e_to[i^1])
   return 0;
  }
  return 1;
 }
 bool intersex(int i,int vi,int j,int vj)
 {
  int x,y,xx,yy;
  if(!vi)
  {
   x=A[i];y=A[i]+T[i];
  }
  else x=B[i],y=B[i]-T[i];
  if(!vj)
  {
   xx=A[j];yy=A[j]+T[j];
  }
  else xx=B[j],yy=B[j]-T[j];
  if(x>y)swap(x,y);
  if(xx>yy)swap(xx,yy);
  if(max(x,xx)<min(y,yy))return 1;
  return 0;
 }
 void build(int n)
 {
  clear();
  for(int i=0;i<n;++i)
  for(int j=i+1;j<n;++j)
  FOR(k,0,1)FOR(l,0,1)
  if(intersex(i,k,j,l))
  {
   add_clause(i,k^1,j,l^1);
  }
  if(!find_bcc(n+n))printf("NO\n");
  else
  {
   n+=n;printf("YES\n");
   int cnt=edge,u,v;
   FOR(i,0,cnt-1)
   { u=e[i].u;v=e[i].v;
     if(e_to[u]!=e_to[v])
     {
      add(e_to[v],e_to[u],qhead);
      ++od[ e_to[u] ];
     }
   }
   ///topsort
   hh=0,tt=0;
   FOR(i,1,bcc)
   if(od[i]==0)
   {
    que[tt++]=i;
   }
   clr(cor,-1);
   while(hh<tt)
   {
     u=que[hh++];
     if(cor[u]==-1)
     {
      cor[u]=1;cor[ mark[u] ]=0;
     }
     for(int i=qhead[u];~i;i=e[i].next)
     {
      v=e[i].v;
      if(--od[v]==0)
      que[tt++]=v;
     }
   }
   ///ans output
   for(int j=0,i;j<n;j+=2)
   {i=j/2;
   if(cor[ e_to[j] ])///如果标记 选择前面
   printf("%02d:%02d %02d:%02d\n",A[i]/60,A[i]%60,(A[i]+T[i])/60,(A[i]+T[i])%60);
   else
   printf("%02d:%02d %02d:%02d\n",(B[i]-T[i])/60,(B[i]-T[i])%60,B[i]/60,B[i]%60);
   }
  }
 }
}two;
int n,m;
int main()
{
  //freopen("data.in","r",stdin);
  int a,b,c,d,o;
  while(~scanf("%d",&n))
  {
    FOR(i,1,n)
    {
       scanf("%d:%d %d:%d %d",&a,&b,&c,&d,&o);
       A[i-1]=a*60+b;B[i-1]=c*60+d;T[i-1]=o;
    }
    two.build(n);
  }
  return 0;
}




你可能感兴趣的:(POJ 3683 Priest John's Busiest Day(2-sa路径输出,4级))