首页 > 代码库 > poj3683 2-SAT 同上一道

poj3683 2-SAT 同上一道

Priest John‘s Busiest Day
Time Limit: 2000MS   Memory Limit: 65536K
Total Submissions: 10151   Accepted: 3475   Special Judge

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

#include<cstdio>
#include<cstring>
#include<algorithm>
#include<vector>
#include<queue>
using namespace std;
const int N=2008;
struct node{
   int st,ed;
}e[N];
int dfn[N],low[N],bl[N],in[N],q[N],col[N],con[N];
bool instack[N];
int cnt,scnt,l,n;
vector<int>v[N];
vector<int>g[N];
void init(){
    memset(dfn,0,sizeof(dfn));
    memset(low,0,sizeof(in));
    memset(col,0,sizeof(col));
    memset(instack,0,sizeof(instack));
    for(int i=0;i<=2*n;++i) v[i].clear(),g[i].clear();
    cnt=scnt=l=0;
}
inline void add(int a,int b){v[a].push_back(b);}
void Tarjan(int u){
    low[u]=dfn[u]=++cnt;
    q[l++]=u;
    instack[u]=1;
    for(int i=0;i<(int)v[u].size();++i){
        int x=v[u][i];
        if(!dfn[x]){
            Tarjan(x);
            low[u]=min(low[u],low[x]);
        }
        else if(instack[x]&&dfn[x]<low[u]) low[u]=dfn[x];
    }
    if(low[u]==dfn[u]){
        int t;++scnt;
        do{
            t=q[--l];
            instack[t]=0;
            bl[t]=scnt;
        }while(t!=u);
    }
}
void rebuild(){
   for(int i=0;i<2*n;++i) for(int j=0;j<(int)v[i].size();++j){
    int a=bl[i],b=bl[v[i][j]];
    if(a==b) continue;
    ++in[a];
    g[b].push_back(a);
   }
}
void topsort(){
   queue<int>Q;
   for(int i=1;i<=scnt;++i) if(!in[i]) Q.push(i);
   while(!Q.empty()){
    int u=Q.front();
    Q.pop();
    if(!col[u]) {
        col[u]=1;
        col[con[u]]=2;
    }
    for(int i=0;i<(int)g[u].size();++i){
        int x=g[u][i];
        --in[x];
        if(in[x]==0) Q.push(x);
    }
   }
}
void solve(){
   for(int i=0;i<2*n;++i) if(!dfn[i]) Tarjan(i);
   for(int i=0;i<n;++i) {
     if(bl[2*i]==bl[2*i+1]) {puts("NO");return;}
     int a=bl[2*i],b=bl[2*i+1];
     con[a]=b;con[b]=a;
   }
   rebuild();
   topsort();
   puts("YES");
   for(int i=0;i<2*n;i+=2){
    if(col[bl[i]]==1) {
        int h1=e[i].st/60,m1=e[i].st%60,h2=e[i].ed/60,m2=e[i].ed%60;
        printf("%02d:%02d %02d:%02d\n",h1,m1,h2,m2);
    }
    else {
        int h1=e[i^1].st/60,m1=e[i^1].st%60,h2=e[i^1].ed/60,m2=e[i^1].ed%60;
        printf("%02d:%02d %02d:%02d\n",h1,m1,h2,m2);
    }
   }
}
int getx(char *s){
   int h,m;
   h=(s[0]-0)*10+s[1]-0;
   m=(s[3]-0)*10+s[4]-0;
   return h*60+m;
}
bool Ju(const node &a,const node &b){
   if (b.st>=a.st && b.st < a.ed) return 1;
   if(a.st>=b.st && a.st < b.ed) return 1;
   return 0;
}
int main(){
   char st[8],ed[8];
   int time;
   while(scanf("%d",&n)!=EOF){
    init();
    for(int i=0;i<n;++i){
        scanf("%s %s %d",st,ed,&time);
        e[2*i].st=getx(st);e[2*i].ed=getx(st)+time;
        e[2*i+1].st=getx(ed)-time;e[2*i+1].ed=getx(ed);
    }
    for(int i=0;i<2*n;++i)
    for(int j=i+1;j<2*n;++j){
         if(i==(j^1)) continue;
         if(Ju(e[i],e[j])) {add(i,j^1);add(j,i^1);}
    }
    solve();
   }
}

 

YES
08:00 08:30
08:40 09:00




poj3683 2-SAT 同上一道