POJ3683 Priest John's Busiest Day

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

YES
08:00 08:30
08:40 09:00
********************************************************************************
题目大意:某一天结婚的人特别多但是主持婚礼的神父只有一个。婚礼时间从s开始到e结束,神父必须在s到s+d或者e-d到e这段时间内在。给定了n个婚礼
的s,e,d,求一种方案能使得神父主持所有的婚礼。
题接思路:2-sat。然而,这是我第一道要输出某个方案的2-sat,写得好是艰难。这题建图不难。一般的要输出例子的2-sat,网上流传要首先tarjan
来判断是否有解,这是肯定的。然后要缩点然后反向建图。缩点再建图的话,貌似好难写,又多了一个图的储存和运算,太不爽了。
这个是缩点但不是反向建图的代码:
#include <stdio.h>
#include <string.h>
#include <vector>
#define N 5005
#define NN 50005
#define M 2000*2000
using namespace std;

struct Node
{
    int s,e,d;
}node[N];
int n,eid,id,now,nn;
int head[NN],nxt[M],ed[M];
int dfn[NN],low[NN];
int ins[NN],gid[NN];
int top,stack[NN];
int mark[NN],vis[NN];
vector<int>gra[NN];
int opp[NN];

void addedge(int s,int e)
{
    ed[eid]=e;
    nxt[eid]=head[s];
    head[s]=eid++;
}

int CH(int a,int b,int c,int d)
{
    if(a>=c&&a<d)return 1;
    if(c>=a&&c<b)return 1;
    return 0;
}

void tarjan(int s)
{
    low[s]=dfn[s]=++now;
    stack[++top]=s;
    ins[s]=1;
    for(int i=head[s];~i;i=nxt[i])
    {
        int t=ed[i];
        if(!dfn[t])
        {
            tarjan(t);
            low[s]=min(low[s],low[t]);
        }
        else if(ins[t])
            low[s]=min(low[s],dfn[t]);
    }
    if(low[s]==dfn[s])
    {
        id++;
        while(top)
        {
            int t=stack[top--];
            ins[t]=0;
            gid[t]=id;
            if(t==s)break;
        }
    }
}

void dfs(int s)
{
    vis[s]=1;
    int len=gra[s].size();
    for(int i=0;i<len;i++)
        if(!vis[gra[s][i]])
            dfs(gra[s][i]);
    stack[++top]=s;
}

void re(void)
{
    nn=2*n;
    for(int i=0;i<n;i++)
    {
        int a1,a2,b1,b2,c;
        scanf("%d:%d %d:%d %d",&a1,&a2,&b1,&b2,&c);
        node[i].s=a1*60+a2;
        node[i].e=b1*60+b2;
        node[i].d=c;
    }
}

void run(void)
{
    memset(head,-1,sizeof(head));
    eid=0;
    for(int i=1;i<n;i++)
        for(int j=0;j<i;j++)
        {
            if(CH(node[i].s,node[i].s+node[i].d,node[j].s,node[j].s+node[j].d))
                addedge(i*2,j*2+1),addedge(j*2,i*2+1);
            if(CH(node[i].s,node[i].s+node[i].d,node[j].e-node[j].d,node[j].e))
                addedge(i*2,j*2),addedge(j*2+1,i*2+1);
            if(CH(node[i].e-node[i].d,node[i].e,node[j].s,node[j].s+node[j].d))
                addedge(i*2+1,j*2+1),addedge(j*2,i*2);
            if(CH(node[i].e-node[i].d,node[i].e,node[j].e-node[j].d,node[j].e))
                addedge(i*2+1,j*2),addedge(j*2+1,i*2);
        }
    memset(dfn,0,sizeof(dfn));
    memset(ins,0,sizeof(ins));
    top=id=now=0;
    for(int i=0;i<nn;i++)
        if(!dfn[i])
            tarjan(i);
    for(int i=0;i<nn;i+=2)
        if(gid[i]==gid[i+1])
        {
            puts("NO");
            return ;
        }
    puts("YES");
    for(int i=0;i<nn;i++)
        opp[gid[i]]=gid[i^1];
    for(int i=1;i<=id;i++)
        gra[i].clear();
    for(int i=0;i<nn;i++)
        for(int j=head[i];~j;j=nxt[j])
            if(gid[i]!=gid[ed[j]])
                gra[gid[i]].push_back(gid[ed[j]]);
    top=0;
    memset(vis,0,sizeof(vis));
    for(int i=1;i<=id;i++)
        if(!vis[i])
            dfs(i);
    memset(mark,0,sizeof(mark));
    while(top)
    {
        int s=stack[top--];
        if(mark[s])continue;
        mark[s]=2;
        mark[opp[s]]=1;
    }
    for(int i=0;i<nn;i++)
        if(mark[gid[i]]==1)
        {
            int a=i/2;
            int b=i&1;
            int t1,t2,t3,t4;
            if(b==0)
            {
                t1=node[a].s/60;
                t2=node[a].s%60;
                t3=(node[a].s+node[a].d)/60;
                t4=(node[a].s+node[a].d)%60;
            }
            else
            {
                t1=(node[a].e-node[a].d)/60;
                t2=(node[a].e-node[a].d)%60;
                t3=node[a].e/60;
                t4=node[a].e%60;
            }
            printf("%02d:%02d %02d:%02d\n",t1,t2,t3,t4);
        }
}

int main()
{
    //freopen("/home/fatedayt/in","r",stdin);
    //freopen("/home/fatedayt/o1","w",stdout);
    while(scanf("%d",&n)==1)
    {
        re();
        run();
    }
    return 0;
}

 个人觉得,貌似不缩点也可以,反正这题AC了:

#include <stdio.h>
#include <string.h>
#include <vector>
#define N 5005
#define NN 50005
#define M 2000*2000
using namespace std;

struct Node
{
    int s,e,d;
}node[N];
int n,eid,id,now,nn;
int head[NN],nxt[M],ed[M];
int dfn[NN],low[NN];
int ins[NN],gid[NN];
int top,stack[NN];
int mark[NN],vis[NN];

void addedge(int s,int e)
{
    ed[eid]=e;
    nxt[eid]=head[s];
    head[s]=eid++;
}

int CH(int a,int b,int c,int d)
{
    if(a>=c&&a<d)return 1;
    if(c>=a&&c<b)return 1;
    return 0;
}

void tarjan(int s)
{
    low[s]=dfn[s]=++now;
    stack[++top]=s;
    ins[s]=1;
    for(int i=head[s];~i;i=nxt[i])
    {
        int t=ed[i];
        if(!dfn[t])
        {
            tarjan(t);
            low[s]=min(low[s],low[t]);
        }
        else if(ins[t])
            low[s]=min(low[s],dfn[t]);
    }
    if(low[s]==dfn[s])
    {
        id++;
        while(top)
        {
            int t=stack[top--];
            ins[t]=0;
            gid[t]=id;
            if(t==s)break;
        }
    }
}

void dfs(int s)
{
    vis[s]=1;
    for(int i=head[s];~i;i=nxt[i])
        if(!vis[ed[i]])
            dfs(ed[i]);
    stack[++top]=s;
}

void re(void)
{
    nn=2*n;
    for(int i=0;i<n;i++)
    {
        int a1,a2,b1,b2,c;
        scanf("%d:%d %d:%d %d",&a1,&a2,&b1,&b2,&c);
        node[i].s=a1*60+a2;
        node[i].e=b1*60+b2;
        node[i].d=c;
    }
}

void run(void)
{
    memset(head,-1,sizeof(head));
    eid=0;
    for(int i=1;i<n;i++)
        for(int j=0;j<i;j++)
        {
            if(CH(node[i].s,node[i].s+node[i].d,node[j].s,node[j].s+node[j].d))
                addedge(i*2,j*2+1),addedge(j*2,i*2+1);
            if(CH(node[i].s,node[i].s+node[i].d,node[j].e-node[j].d,node[j].e))
                addedge(i*2,j*2),addedge(j*2+1,i*2+1);
            if(CH(node[i].e-node[i].d,node[i].e,node[j].s,node[j].s+node[j].d))
                addedge(i*2+1,j*2+1),addedge(j*2,i*2);
            if(CH(node[i].e-node[i].d,node[i].e,node[j].e-node[j].d,node[j].e))
                addedge(i*2+1,j*2),addedge(j*2+1,i*2);
        }
    memset(dfn,0,sizeof(dfn));
    memset(ins,0,sizeof(ins));
    top=id=now=0;
    for(int i=0;i<nn;i++)
        if(!dfn[i])
            tarjan(i);
    for(int i=0;i<nn;i+=2)
        if(gid[i]==gid[i+1])
        {
            puts("NO");
            return ;
        }
    puts("YES");
    top=0;
    memset(vis,0,sizeof(vis));
    for(int i=0;i<nn;i++)
        if(!vis[i])
            dfs(i);
    memset(mark,0,sizeof(mark));
    while(top)
    {
        int s=stack[top--];
        if(mark[s])continue;
        mark[s]=2;
        mark[s^1]=1;
    }
    for(int i=0;i<nn;i++)
        if(mark[i]==1)
        {
            int a=i/2;
            int b=i&1;
            int t1,t2,t3,t4;
            if(b==0)
            {
                t1=node[a].s/60;
                t2=node[a].s%60;
                t3=(node[a].s+node[a].d)/60;
                t4=(node[a].s+node[a].d)%60;
            }
            else
            {
                t1=(node[a].e-node[a].d)/60;
                t2=(node[a].e-node[a].d)%60;
                t3=node[a].e/60;
                t4=node[a].e%60;
            }
            printf("%02d:%02d %02d:%02d\n",t1,t2,t3,t4);
        }
}

int main()
{
    //freopen("/home/fatedayt/in","r",stdin);
    //freopen("/home/fatedayt/o1","w",stdout);
    while(scanf("%d",&n)==1)
    {
        re();
        run();
    }
    return 0;
}

原文地址:https://www.cnblogs.com/Fatedayt/p/2260449.html