Priest John's Busiest Day (2-SAT decision outputs)

Priest John's Busiest Day(luogu)

Description

Pastor John in September 1 this day is very busy.

There are N couple to get married on this day, each couple have a good time pre-planned wedding ceremony, in which the i-th wedding couple from the moment Si start to time  T I  ends.

There must be a wedding ceremony: standing in front of the priest to listen to God's blessing.

This ceremony was held either at the time of the wedding, held either at the end.

I-couple need  D i  minutes to complete the ritual, i.e. must select  S i ~ S i + Di or ~ Di-ti T I  one of the two time periods.

Pastor wants to know if he can meet the requirements of each wedding, that is, to each couple to arrange S~ i S i + Di or ~ Di-ti T I , so that the time period does not overlap these rituals.

If we can meet, but also to help pastors find any specific programs.

Note that John can not hosted two weddings at the same time.

If the end time of a ritual ceremony and the start time of another are the same, not overlap.

For example: a ceremony scheduled 8:00 ~ 9:00, another ceremony scheduled 9:00 ~ 10:00, the ceremony is not considered two overlap.

Input Format

The first row contains an integer N.

Next N rows, each row containing  S I , T I , D , where  S and Ti is an h: m form.

Output Format

The first line output can meet, can then output "YES", otherwise a "NO".

Next N rows, each row is given a specific time schedule.

data range

1N1000

 

Solution

Each couple has a constraint relationship between the two options and time makes us think of using 2-SAT is easy to do.

First hour in terms of minutes (Note that T1 seconds) to facilitate subsequent calculation

Even the directed edge between the time the couple will have a conflict. (Refer to code)

The key problem is that this output arrangement

We know the strong points Unicom shrink from small to large in accordance with the order topology is the number descending

Sequence Topological large and there is no path to a certain point sequence topology small dots

That is, when there is a legitimate program, for each couple, the topology sequence corresponding to the point taken large

 

Code

 

#include <cstdio>
#include <cstdlib>
#include <vector>
#include <algorithm>
using namespace std;
const int N=2e3+10,M=4e6+10;
int n,s[N][2],t[N][2],s_h,s_m,t_h,t_m,a,b,c;
int head[N],nxt[M],to[M],cnt,d[N],opp[N];
int dfn[N],low[N],tot,st[N],top,scc;
bool in[N];
struct node
{
    int st,ed;
    bool operator <(const node &o)const
    {
        return st<o.st;
    }
};
vector <node> ans;
void add(int x,int y)
{
    to[++cnt]=y,nxt[cnt]=head[x],head[x]=cnt;
}
void tarjan(int u)
{
    dfn[u]=low[u]=++tot;
    st[++top]=u,in[u]=true;
    for(int i=head[u];i;i=nxt[i])
    {
        int v=to[i];
        if(!dfn[v]) tarjan(v),low[u]=min(low[v],low[u]);
        else if(in[v]) low[u]=min(low[u],dfn[v]);
    }
    if(low[u]==dfn[u])
    {
        int z;
        scc++;
        do{
            z=st[top--];
            in[z]=false,d[z]=scc;
        }while(z!=u);
    }
}
int main()
{
    scanf("%d",&n);
    for(int i=1;i<=n;i++)
    {
        scanf("%d:%d %d:%d %d",&s_h,&s_m,&t_h,&t_m,&c);
        a=s_h*60+s_m,b=t_h*60+t_m;
        s[i][0]=a,t[i][0]=a+c,s[i][1]=b-c,t[i][1]=b;
    }
    for(int i=1;i<=n;i++) for(int j=i+1;j<=n;j++)
        for(int p=0;p<2;p++) for(int q=0;q<2;q++)
            if(!(t[i][p]<=s[j][q] || t[j][q]<=s[i][p]))
                add(i+n*p,j+n*(1-q)),add(j+n*q,i+n*(1-p));
    for(int i=1;i<=n*2;i++)
        if(!dfn[i]) tarjan(i);
    for(int i=1;i<=n;i++)
    {
        if(d[i]==d[i+n])
        {
            puts("NO");
            return 0;
        }
        opp[i]=i+n,opp[i+n]=i;
    }
    puts("YES");
    for(int i=1;i<=n;i++)
        ans.push_back((node){s[i][d[i]>d[opp[i]]],t[i][d[i]>d[opp[i]]]});
    sort(ans.begin(),ans.end());
    for(int i=0;i<n;i++)
    {
        node tp=ans[i];
        s_h=tp.st/60,s_m=tp.st%60;
        t_h=tp.ed/60,t_m=tp.ed%60;
        printf("%02d:%02d %02d:%02d\n",s_h,s_m,t_h,t_m);
    }
    return 0;
}

 

Guess you like

Origin www.cnblogs.com/hsez-cyx/p/12348517.html