poj 3683 Priest John's Busiest Day

Priest John's Busiest Day

 POJ - 3683 node

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 Sito 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 Sito 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.ios

Note that John can not be present at two weddings simultaneously.c++

Inputless

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

Outputthis

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

Sample Input.net

2
08:00 09:00 30
08:15 09:00 20

Sample Outputcode

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

 

 

 

#include<iostream>
#include<cstdio>
#include<cstring>
#define maxn 2010
using namespace std;
int a[maxn],b[maxn],belong[maxn],op[maxn],dfn[maxn],low[maxn],st[maxn];
bool vis[maxn];
int head[maxn],head2[maxn],d[maxn],col[maxn],num,num2,ind,cnt,top,scc,n;
struct node{int to,pre;}e[maxn*maxn],e2[maxn*maxn];
void Insert(int from,int to){
    e[++num].to=to;
    e[num].pre=head[from];
    head[from]=num;
}
void Insert2(int from,int to){
    d[to]++;
    e2[++num2].to=to;
    e2[num2].pre=head2[from];
    head2[from]=num2;
}
bool judge(int x,int y){
    if(b[x]<=a[y]||a[x]>=b[y])return 0;
    return 1;
}
void build(){
    for(int i=1;i<=n;i++)
        for(int j=i+1;j<=n;j++){
            if(judge(2*i,2*j)){Insert(2*i,2*j-1);Insert(2*j,2*i-1);}
            if(judge(2*i,2*j-1)){Insert(2*i,2*j);Insert(2*j-1,2*i-1);}
            if(judge(2*i-1,2*j)){Insert(2*i-1,2*j-1);Insert(2*j,2*i);}
            if(judge(2*i-1,2*j-1)){Insert(2*i-1,2*j);Insert(2*j-1,2*i);}
        }
}
void Tarjan(int u){
    dfn[u]=low[u]=++ind;
    st[++top]=u;vis[u]=1;
    for(int i=head[u];i;i=e[i].pre){
        int v=e[i].to;
        if(!dfn[v]){
            Tarjan(v);
            low[u]=min(low[u],low[v]);
        }
        else if(vis[v])
            low[u]=min(low[u],dfn[v]);
    }
    if(dfn[u]==low[u]){
        int now=0;scc++;
        while(now!=u){
            now=st[top--];
            vis[now]=0;
            belong[now]=scc;
        }
    }
}
void rebuild(){
    cnt=0;
    for(int x=1;x<=2*n;x++)
        for(int i=head[x];i;i=e[i].pre){
            if(belong[x]!=belong[e[i].to])
            Insert2(belong[e[i].to],belong[x]);
        }
}
void dfs(int x){
    if(col[x])return;
    col[x]=-1;
    for(int i=head2[x];i;i=e2[i].pre)
        dfs(e2[i].to);
}
void topsort(){
    for(int i=1;i<=scc;i++)
        if(!d[i])st[++top]=i;
    while(top){
        int now=st[top--];
        if(col[now])continue;
        col[now]=1;
        dfs(op[now]);
        for(int i=head2[now];i;i=e2[i].pre){
            d[e2[i].to]--;
            if(!d[e2[i].to])st[++top]=e2[i].to;
        }
    }
}
void print(int x){
    printf("%.2d:",x/60);
    printf("%.2d ",x%60);
}
int main(){
    scanf("%d",&n);
    int x,y;
    for(int i=1;i<=n;i++){
        scanf("%d:%d",&x,&y);a[2*i]=x*60+y;
        scanf("%d:%d",&x,&y);b[2*i-1]=x*60+y;
        scanf("%d",&x);
        b[2*i]=a[i*2]+x;
        a[2*i-1]=b[i*2-1]-x;
    }
    build();
    for(int i=1;i<=2*n;i++)
        if(!dfn[i])Tarjan(i);
    for(int i=1;i<=n;i++)
        if(belong[2*i]==belong[2*i-1]){
            puts("NO");
            return 0;
        }
    puts("YES");
    rebuild();
    for(int i=1;i<=n;i++){
        op[belong[2*i]]=belong[2*i-1];
        op[belong[2*i-1]]=belong[2*i];
    }
    topsort();
    for(int i=1;i<=n;i++)
        if(col[belong[2*i]]==1)
            print(a[2*i]),print(b[2*i]),puts("");
        else print(a[2*i-1]),print(b[2*i-1]),puts("");
    return 0;
}
相關文章
相關標籤/搜索