poj 3683 Priest John's Busiest Day(2-SAT)

3 篇文章 0 订阅

Language:
Priest John's Busiest Day
Time Limit: 2000MS Memory Limit: 65536K
Total Submissions: 8547 Accepted: 2918 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 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 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


n对新人举办婚礼 需要神父主持 有一个特殊的仪式 必须需要神父在场

给出婚礼开始和结束的时间 以及特殊仪式所需要的时间 另外这个特殊仪式必须在开始或者结束时进行

神父不能同时参加多个仪式 问有木有一种时间安排可以使得神父能够参加全部的仪式


2-SAT解法

a或b转化为 非a=>b且非b=>a

xi为真<=>在开始时进行特别仪式

记#xi为xi的反 

如果Si~Si+Di和Sj~Sj+Dj冲突 则有#xi或#xj为真(xi且xj为假)

类似于这样的还有

Si~Si+Di和Tj-Dj~Tj  Ti-Di~Ti和Sj~Sj+Dj   Ti-Di~Ti和Tj-Dj~Tj

建图 利用连通分量 求出是否有可行解  利用拓扑序 求出任意一种解

#include <cstdio>
#include <iostream>
#include <cstring>
#include <cmath>
#include <algorithm>
#include <string.h>
#include <string>
#include <vector>
#include <queue>

#define MEM(a,x) memset(a,x,sizeof a)
#define eps 1e-8
#define MOD 10009
#define MAXN 5010
#define MAXM 2000100
#define INF 99999999
#define ll __int64
#define bug cout<<"here"<<endl
#define fread freopen("ceshi.txt","r",stdin)
#define fwrite freopen("out.txt","w",stdout)

using namespace std;

int Read()
{
    char ch;
    int a = 0;
    while((ch = getchar()) == ' ' | ch == '\n');
    a += ch - '0';
    while((ch = getchar()) != ' ' && ch != '\n')
    {
        a *= 10;
        a += ch - '0';
    }
    return a;
}

void Print(int a)
{
     if(a>9)
         Print(a/10);
     putchar(a%10+'0');
}
struct Edge
{
    int to,next;
}edge[MAXM];
int head[MAXN],tot;
int low[MAXN],dfn[MAXN],stack[MAXN],belong[MAXN];
int index,top;
bool instack[MAXN];
int scc;
void addedge(int u,int v)
{
    edge[tot].to=v; edge[tot].next=head[u]; head[u]=tot++;
}
void Tarjan(int u)
{
    int v;
    low[u]=dfn[u]=++index;
    stack[top++]=u;
    instack[u]=1;
    for(int i=head[u];i!=-1;i=edge[i].next)
    {
        v=edge[i].to;
        if(!dfn[v])
        {
            Tarjan(v);
            if(low[u]>low[v]) low[u]=low[v];
        }
        else if(instack[v]&&low[u]>dfn[v])
            low[u]=dfn[v];
    }
    if(low[u]==dfn[u])
    {
        scc++;
        do
        {
            v=stack[--top];
            instack[v]=0;
            belong[v]=scc;
        }while(u!=v);
    }
}

int solvable(int N)
{
    MEM(dfn,0); MEM(instack,0);
    index=scc=top=0;
    for(int i=1;i<=2*N;i++)
        if(!dfn[i])
            Tarjan(i);
    for(int i=1;i<=N;i++)
    {
        if(belong[i]==belong[N+i])
        {
            return 0;
        }
    }
    return 1;
}
void init()
{
    tot=0;
    MEM(head,-1);
}

int s[MAXN],t[MAXN],d[MAXN];
queue<int> q1,q2;
vector<vector<int> > dag;
char color[MAXN];
int indeg[MAXN];
int cf[MAXN];
void solve(int n)
{
    dag.assign(scc+1,vector<int>());
    MEM(indeg,0); MEM(color,0);
    for(int u=1;u<=2*n;u++)
    {
        for(int i=head[u];i!=-1;i=edge[i].next)
        {
            int v=edge[i].to;
            if(belong[u]!=belong[v])
            {
                dag[belong[v]].push_back(belong[u]);
                indeg[belong[u]]++;
            }
        }
    }
    for(int i=1;i<=n;i++)
    {
        cf[belong[i]]=belong[i+n];
        cf[belong[i+n]]=belong[i];
    }
    while(!q1.empty()) q1.pop();
    while(!q2.empty()) q2.pop();
    for(int i=1;i<=scc;i++)
        if(indeg[i]==0)
            q1.push(i);
    while(!q1.empty())
    {
        int u=q1.front();
        q1.pop();
        if(color[u]==0)
        {
            color[u]='R';
            color[cf[u]]='B';
        }
        int sz=dag[u].size();
        for(int i=0;i<sz;i++)
        {
            indeg[dag[u][i]]--;
            if(indeg[dag[u][i]]==0)
                q1.push(dag[u][i]);
        }
    }
}


int main()
{
//    fread;
    int n;
    while(scanf("%d",&n)!=EOF)
    {
        int s1,s2,t1,t2;
        for(int i=1;i<=n;i++)
        {
            scanf("%d:%d%d:%d%d",&s1,&s2,&t1,&t2,&d[i]);
            s[i]=s1*60+s2;
            t[i]=t1*60+t2;
        }
        init();
        for(int i=1;i<=n;i++)
        {
            for(int j=1;j<i;j++)
            {
                if(min(s[i]+d[i],s[j]+d[j])>max(s[i],s[j]))
                {
                    addedge(i,n+j);
                    addedge(j,n+i);
                }
                if(min(s[i]+d[i],t[j])>max(s[i],t[j]-d[j]))
                {
                    addedge(i,j);
                    addedge(n+j,n+i);
                }
                if(min(t[i],s[j]+d[j])>max(t[i]-d[i],s[j]))
                {
                    addedge(n+i,n+j);
                    addedge(j,i);
                }
                if(min(t[i],t[j])>max(t[i]-d[i],t[j]-d[j]))
                {
                    addedge(n+i,j);
                    addedge(n+j,i);
                }
            }
        }
        int flag=solvable(n);

        if(!flag)
        {
            puts("NO");
            continue;
        }
        puts("YES");
        solve(n);
        for(int i=1;i<=n;i++)
        {
            if(color[belong[i]]=='R')
            {
                printf("%02d:%02d %02d:%02d\n",s[i]/60,s[i]%60,(s[i]+d[i])/60,(s[i]+d[i])%60);
            }
            else
            {
                printf("%02d:%02d %02d:%02d\n",(t[i]-d[i])/60,(t[i]-d[i])%60,t[i]/60,t[i]%60);
            }
        }
    }
    return 0;
}





  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值