POJ - 3683 Priest John's Busiest Day

版权声明:选经典题目,写精品文章. https://blog.csdn.net/nka_kun/article/details/83036173

Priest John’s Busiest Day
Time Limit: 2000MS Memory Limit: 65536K
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

扫描二维码关注公众号,回复: 3731039 查看本文章

题意:给出每个人结婚可选的两个时间,问是否可以不冲突安排婚礼.
思路:当然每个人可选的这两个时间必须二选一,然后有冲突的时间不可同时选,这样就成了一个2-SAT模型,
(A,B)不可同时选,所以按照
模型一:两者(A,B)不能同时取
  那么选择了A就只能选择B’,选择了B就只能选择A’
  连边A→B’,B→A’
然后跑一下就行了.
代码:

#include<iostream>
#include<cstdio>
#include<cstring>
using namespace std;
#define MAXN 20000
#define MAXM 1000010
#define mem(a,b) memset(a,b,sizeof(a))

int n,m;
struct node
{
    int v;
    node *next;
};

int st[MAXN],et[MAXN];

node edge[MAXM*2];
node *cnt=&edge[0];
node *adj[MAXN];
node edge2[MAXM*2];
node *cnt2=&edge2[0];
node *adj2[MAXN];
int dfn[MAXN],low[MAXN],dcnt;
int stack[MAXN],top;
int Belong[MAXN],Num[MAXN],opp[MAXN],scc;
int In[MAXN],q[MAXN],col[MAXN];
bool Instack[MAXN],ans[MAXN];

inline void Get_int(int &Ret)
{
    char ch;
    bool flag=false;
    for(;ch=getchar(),ch<'0'||ch>'9';)
        if(ch=='-')
            flag=true;
    for(Ret=ch-'0';ch=getchar(),ch>='0'&&ch<='9';Ret=Ret*10+ch-'0');
    flag&&(Ret=-Ret);
}

inline int Get(int x)
{
    if(x%2)
        return x+1;
    return x-1;
}


inline void Addedge(int u,int v)
{
    node *p=++cnt;
    p->v=v;
    p->next=adj[u];
    adj[u]=p;
}

inline void Addedge2(int u,int v)
{
    node *p=++cnt2;
    p->v=v;
    p->next=adj2[u];
    adj2[u]=p;
}

bool conflict(int x,int y)
{
	return !(st[x]>= et[y]||et[x]<= st[y]);
}

void Read()
{
    Get_int(n);
    int i,j,k;
    
    for(int i = 1;i<= n+n;i+= 2)
    {
    	int x1,x2;
    	int y1,y2;
    	int d;
    	scanf("%d:%d",&x1,&x2);
    	scanf("%d:%d",&y1,&y2);
    	scanf("%d",&d);
    	x1 = x1*60+x2;
    	y1 = y1*60+y2;
    	st[i] = x1;
    	et[i] = x1+d;
    	st[i+1] = y1-d;
    	et[i+1] = y1;
    }
    
    n+= n;
    for(int i = 1;i<= n;i++)
    {
    	for(int j = (i%2)?i+2:i+1;j<= n;j++)
    	{
    		if(conflict(i,j))
    		{
    			Addedge(i,Get(j));
    			Addedge(j,Get(i));
    		}
    	}
    }
    return ;
}

void Tarjan(int u)
{
    int v;
    dfn[u]=low[u]=++dcnt;
    stack[++top]=u;
    Instack[u]=true;
    for(node *p=adj[u];p;p=p->next)
    {
        v=p->v;
        if(!dfn[v])
        {
            Tarjan(v);
            low[u]=min(low[u],low[v]);
        }
        else if(Instack[v])
            low[u]=min(low[u],dfn[v]);
    }
    if(dfn[u]==low[u])
    {
        scc++;
        do
        {
            v=stack[top];
            top--;
            Instack[v]=false;
            Belong[v]=scc;
            Num[scc]++;
        }while(v!=u);
    }
}

bool Work()
{
    int i;
    for(i=1;i<=n;i++)
        if(!dfn[i])
            Tarjan(i);
    for(i=1;i<=n;i+=2)
    {
        if(Belong[i]==Belong[i+1])
            return false;
        opp[Belong[i]]=Belong[i+1];
        opp[Belong[i+1]]=Belong[i];
    }
    int u,v;
    for(i=1;i<=n;i++)
        for(node *p=adj[i];p;p=p->next)
        {
            v=p->v;
            if(Belong[i]!=Belong[v])
            {
                Addedge2(Belong[v],Belong[i]);
                In[Belong[i]]++;
            }
        }
    int l=0,r=0;
    for(i=1;i<=scc;i++)
        if(!In[i])
        {
            q[r]=i;
            r++;
        }
    while(l<r)
    {
        u=q[l];
        l++;
        if(!col[u])
        {
            col[u]=1;
            col[opp[u]]=-1;
        }
        for(node *p=adj2[u];p;p=p->next)
        {
            v=p->v;
            In[v]--;
            if(!In[v])
            {
                q[r]=v;
                r++;
            }
        }
    }
    for(i=1;i<=n;i+=2)
        if(col[Belong[i]]==1)
            ans[i]=true;
    return true;
}

void Print()
{
    if(Work())
    {
    	printf("YES\n");
        int i;
        for(i=1;i<=n;i+= 2)
        {
        	if(ans[i])
                printf("%02d:%02d %02d:%02d\n",st[i]/60,st[i]%60,et[i]/60,et[i]%60);
            else
                printf("%02d:%02d %02d:%02d\n",st[i+1]/60,st[i+1]%60,et[i+1]/60,et[i+1]%60);
        }
    }
    else
        printf("NO\n");
}

int main()
{
	Read();
	Print();
	
    return 0;
}

猜你喜欢

转载自blog.csdn.net/nka_kun/article/details/83036173
今日推荐