poj 3683 Priest John's Busiest Day(2-SAT 输出路径)

6 篇文章 0 订阅
Priest John's Busiest Day
Time Limit: 2000MS Memory Limit: 65536K
Total Submissions: 7177 Accepted: 2450 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 yearN couples plan to get married on the blessed day. The i-th couple plan to hold their wedding from timeSi 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. Thei-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 fromSi to Si + Di, or fromTi - 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 andDi. Si and Ti are in the format ofhh: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 anotherN 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个婚礼,牧师要给每个婚礼做一个仪式,每个仪式都需要耗费相应的时间。现在给出每个婚礼的开始时间s、结束时间e和仪式的持续时间d,仪式只能在时间段  (s,s+d)或(e-d,e)内进行,牧师在同一时间内只能给一个婚礼做仪式。问牧师能否给所有婚礼都做仪式。若能,则输出每个婚礼仪式的开始时间或持续时间。
思路:每个婚礼有两个仪式时间段供选择,容易想到是2-SAT问题。把每个婚礼i拆成两个点2*i和2*i+1分别代表上述的两个仪式时间段,然后分别和其他婚礼的两个仪式时间段比较,若相冲突,则可以构图了。构图完成后,用tarjan判断是否有解。若有解,则对于缩点后的新图,建立一个反图。在反图上进行拓扑排序,确定要输出的仪式时间。
 
AC代码:
#include <iostream>
#include <cstdio>
#include <cstring>
#include <string>
#include <algorithm>
#include <queue>
#include <vector>
#include <cmath>
#include <map>
#include <cstdlib>
#define L(rt) (rt<<1)
#define R(rt) (rt<<1|1)
#define ll long long
using namespace std;

const int maxn=2005;
const int INF=1000000000;
struct node
{
    int v,next;
}edge[maxn*maxn];
int G[maxn],NG[maxn],scc[maxn],stack[maxn];
int low[maxn],dfn[maxn],in[maxn],s[maxn][2],e[maxn][2];
bool ins[maxn],flag[maxn];
int n,m,num,cnt,top,snum;
void init()
{
    memset(G,-1,sizeof(G));
    memset(NG,-1,sizeof(NG));
    num=0;
}
void add(int *head,int u,int v)
{
    edge[num].v=v;
    edge[num].next=head[u];
    head[u]=num++;
}
bool ok(int x1,int y1,int x2,int y2)
{
    if(y1<=x2||y2<=x1) return false;
    return true;
}
void input()
{
   int h1,h2,m1,m2,d;
   for(int i=0;i<n;i++)
   {
       scanf("%d:%d%d:%d%d",&h1,&m1,&h2,&m2,&d);
       s[i][0]=h1*60+m1;
       s[i][1]=s[i][0]+d;
       e[i][1]=h2*60+m2;
       e[i][0]=e[i][1]-d;
   }
   for(int i=0;i<n;i++)
   for(int j=i+1;j<n;j++)
   {
       if(ok(s[i][0],s[i][1],s[j][0],s[j][1]))
       {
           add(G,2*i,2*j+1);
           add(G,2*j,2*i+1);
       }
       if(ok(s[i][0],s[i][1],e[j][0],e[j][1]))
       {
           add(G,2*i,2*j);
           add(G,2*j+1,2*i+1);
       }
       if(ok(e[i][0],e[i][1],s[j][0],s[j][1]))
       {
           add(G,2*i+1,2*j+1);
           add(G,2*j,2*i);
       }
       if(ok(e[i][0],e[i][1],e[j][0],e[j][1]))
       {
           add(G,2*i+1,2*j);
           add(G,2*j+1,2*i);
       }
   }
}
void dfs(int u)
{
    int x;
    dfn[u]=low[u]=++cnt;
    stack[top++]=u;
    ins[u]=true;
    for(int i=G[u];i!=-1;i=edge[i].next)
    {
        int v=edge[i].v;
        if(!dfn[v])
        {
            dfs(v);
            low[u]=min(low[u],low[v]);
        }
        else if(ins[v]) low[u]=min(low[u],dfn[v]);
    }
    if(low[u]==dfn[u])
    {
        do{
            x=stack[--top];
            ins[x]=false;
            scc[x]=snum;
        }while(x!=u);
        snum++;
    }
}
void tarjan()
{
    memset(dfn,0,sizeof(dfn));
    memset(ins,false,sizeof(ins));
    cnt=top=snum=0;
    for(int i=0;i<2*n;i++)
    if(!dfn[i]) dfs(i);
}
void toposort()
{
    int x;
    queue<int>Q;
    memset(flag,false,sizeof(flag));
    for(int i=0;i<snum;i++)
    if(!in[i]) Q.push(i);
    while(!Q.empty())
    {
        x=Q.front();
        Q.pop();
        for(int i=0;i<2*n;i++)
        if(scc[i]==x&&!flag[i^1])
        flag[i]=true;
        for(int i=NG[x];i!=-1;i=edge[i].next)
        {
            int v=edge[i].v;
            in[v]--;
            if(!in[v]) Q.push(v);
        }
    }
    for(int i=0;i<2*n;i++)
    if(flag[i])
    {
        if(i&1) printf("%02d:%02d %02d:%02d\n",e[i/2][0]/60,e[i/2][0]%60,e[i/2][1]/60,e[i/2][1]%60);
        else printf("%02d:%02d %02d:%02d\n",s[i/2][0]/60,s[i/2][0]%60,s[i/2][1]/60,s[i/2][1]%60);
    }
}
void solve()
{
    for(int i=0;i<2*n;i+=2)
    if(scc[i]==scc[i^1])
    {
        printf("NO\n");
        return;
    }
    memset(in,0,sizeof(in));
    for(int u=0;u<2*n;u++)
    for(int i=G[u];i!=-1;i=edge[i].next)
    {
        int v=edge[i].v;
        if(scc[u]!=scc[v])
        {
            add(NG,scc[v],scc[u]);
            in[scc[u]]++;
        }
    }
    printf("YES\n");
    toposort();
}
int main()
{
    while(~scanf("%d",&n))
    {
        init();
        input();
        tarjan();
        solve();
    }
    return 0;
}


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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值