poj 3683Priest John's Busiest Day 2sat判断区间重叠

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

Source

POJ Founder Monthly Contest – 2008.08.31, Dagger and Facer

#include
#include
#include
#include
#include
#include
using namespace std;
#define maxn 1010
struct time
{
    int s,e,d;
    time()
    {

    }
    time(int s,int e,int d):s(s),e(e),d(d){}
}t[maxn];
struct two
{
    int n;
    vectorg[maxn*2];
    bool mark[maxn*2];
    int s[maxn*2];
    int c;
    bool dfs(int x)
    {
        if(mark[x^1])
            return false;
        if(mark[x])
            return true;
        mark[x]=true;
        s[c++]=x;
        for(int i=0;in=n;
        for(int i=0;i0)
                    mark[s[--c]]=false;
                    if(!dfs(i+1))
                        return false;
            }
        }
        return true;
    }
}TS;
int main()
{
    int n;
    scanf("%d",&n);
    for(int i=0;i

建单向边写法

#include
#include
#include
#include
#include
#define maxn 10005
using namespace std;
int n;
struct Time
{
    int s,e,d;
    Time()
    {

    }
    Time(int s,int e,int d):s(s),e(e),d(d){}
}t[maxn];
struct twosat
{
    int n;
    vectorg[maxn];
    bool mark[maxn*2];
    int s[maxn*2];
    int c;
    void init(int n)
    {
        this->n=n;
        for(int i=0;i<2*n;i++)
            g[i].clear();
        memset(mark,0,sizeof(mark));
    }
    void addedge(int x,int xval,int y,int yval)
    {
        x=x*2+xval;
        y=y*2+yval;
        g[x].push_back(y);

    }
    bool dfs(int x)
    {
        if(mark[x^1])
            return false;
        if(mark[x])
            return true;
        mark[x]=true;
        s[c++]=x;
        for(int i=0;i0)
           mark[s[--c]]=false;
           if(!dfs(i+1))
            return false;
        }
        }
        return true;
    }
}TS;
int main()
{while(~scanf("%d",&n))
{
    TS.init(n);
    for(int i=0;i

 

你可能感兴趣的:(2sat)