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
Source
POJ Founder Monthly Contest – 2008.08.31, Dagger and Facer
hdu1814Peaceful Commission【2-SAT】輸出最小解
+
uva1146Now or later飛機排程【2-SAT】入門
沒了,怎麼可以這麼一個小破題拖了這麼久
/***********
poj3683
2016.7.21
8140K 344MS C++ 4295B
***********/
#include <iostream>
#include<cstring>
#include<cstdio>
#include<vector>
using namespace std;
#define maxn 8004
struct TWOSAT
{
int n;
vector<int>G[maxn*2];
bool mark[maxn*2];
int S[maxn*2],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;i<G[x].size();i++)
if(!dfs(G[x][i])) return false;
return true;
}
void init(int n)
{
this->n=n;
for(int i=0;i<n*2;i++) G[i].clear();
memset(mark,0,sizeof(mark));
}
void add_clause(int x,int xval,int y,int yval)
{
x=x*2+xval;
y=y*2+yval;
G[x^1].push_back(y);
G[y^1].push_back(x);///不一定是+1、-1!!而且本身就2n不用乘以2
}
bool solve()
{
for(int i=0;i<2*n;i+=2)
{
if(!mark[i]&&!mark[i+1])
{
c=0;
if(!dfs(i))
{
while(c>0) mark[S[--c]]=false;
if(!dfs(i+1)) return false;
}
}
}
return true;
}
}solver;
bool judge(int s1,int e1,int s2,int e2)
{
if (s1 >= s2 && s1 < e2) return true;
if (s2 >= s1 && s2 < e1) return true;
return false;
}
int n,m;
struct time{
int st1,ed1;
int st2,ed2;
}num[maxn];
int main()
{
// freopen("cin.txt","r",stdin);
while(~scanf("%d",&n))
{
solver.init(n);
char tm1[20],tm2[20];
int tm;
for(int i=0;i<n;i++)
{
scanf("%s%s%d",tm1,tm2,&tm);
num[i].st1=((tm1[0]-'0')*10+(tm1[1]-'0'))*60+(tm1[3]-'0')*10+(tm1[4]-'0');
num[i].ed1=num[i].st1+tm;
num[i].ed2=((tm2[0]-'0')*10+(tm2[1]-'0'))*60+(tm2[3]-'0')*10+(tm2[4]-'0');
num[i].st2=num[i].ed2-tm;
int s1,s2,e1,e2;
for(int j=0;j<i;j++)
{
/** if(num[i].st1>num[j].ed1||num[i].ed1<num[j].st1)
solver.add_clause(i,0,j,0);//printf("i=%d,0,j=%d,0\n",i,j);
if(num[i].st1>num[j].ed2||num[i].ed1<num[j].st2)
solver.add_clause(i,0,j,1);//printf("i=%d,0,j=%d,1\n",i,j);
if(num[i].st2>num[j].ed1||num[i].ed2<num[j].st1)
solver.add_clause(i,1,j,0);//printf("i=%d,1,j=%d,0\n",i,j);
if(num[i].st2>num[j].ed2||num[i].ed2<num[j].st2)
solver.add_clause(i,1,j,1);//printf("i=%d,1,j=%d,1\n",i,j);**/
for(int x=0;x<2;x++)
{
for(int y=0;y<2;y++)
{
if(x==0)
{
s1=num[i].st1;
e1=num[i].ed1;
}
else
{
s1=num[i].st2;
e1=num[i].ed2;
}
if(y==0)
{
s2=num[j].st1;
e2=num[j].ed1;
}
else
{
s2=num[j].st2;
e2=num[j].ed2;
}
//if(s1>e1||s2>e2)continue;
if(judge(s1,e1,s2,e2))
{
solver.add_clause(i,x^1,j,y^1);
// solver.add_clause(j,y,i,x^1);
}
}
}
}
}
if(!solver.solve()) puts("NO");
else
{
puts("YES");
for(int i=0;i<n;i++)
{
// printf("i=%d,mark[i<<1]=%d,mark[i<<1|1]=%d \n",i,solver.mark[i<<1],solver.mark[i<<1|1]);
}
for(int i=0;i<n;i++)
{
if(solver.mark[i<<1]&solver.mark[i<<1|1]==0) printf("%02d:%02d %02d:%02d\n",num[i].st1/60,num[i].st1%60,num[i].ed1/60,num[i].ed1%60);
else printf("%02d:%02d %02d:%02d\n",num[i].st2/60,num[i].st2%60,num[i].ed2/60,num[i].ed2%60);
}
}
}
return 0;
}