Priest John's Busiest Day
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). 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
|
1~n表示第i场仪式在开始,n+1~2n表示结束
2个时段i,j冲突: ~i∩~j
2-SAT 本身的原理是 :
若i成立 ,则j成立 则连一条边 i->j
则可利用强连通分量缩点,得到的DAG
显然每个大点内元素是非相同,线i->j表示 若i成立,j成立
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
using namespace std;
#define For(i,n) for(int i=1;i<=n;i++)
#define Fork(i,k,n) for(int i=k;i<=n;i++)
#define Rep(i,n) for(int i=0;i=0;i--)
#define Forp(x) for(int p=pre[x];p;p=next[p])
#define Lson (x<<1)
#define Rson ((x<<1)+1)
#define MEM(a) memset(a,0,sizeof(a));
#define MEMI(a) memset(a,127,sizeof(a));
#define MEMi(a) memset(a,128,sizeof(a));
#define INF (2139062143)
#define F (100000007)
#define MAXn (1000+10)
#define MAXN (2000+10)
long long mul(long long a,long long b){return (a*b)%F;}
long long add(long long a,long long b){return (a+b)%F;}
long long sub(long long a,long long b){return (a-b+(a-b)/F*F+F)%F;}
typedef long long ll;
class SSC
{
public:
int n,b[MAXN],num[MAXN];
vector G[MAXN],rG[MAXN]; //图,反向后的图
vector vs; //后续遍历顶点列表
void mem(int _n)
{
n=_n; MEM(num)
For(i,n) G[i].clear(),rG[i].clear();
vs.clear();
}
void addedge(int u,int v)
{
G[u].push_back(v);
rG[v].push_back(u);
}
void dfs(int x)
{
b[x]=1;
Rep(i,G[x].size())
{
if (!b[G[x][i]]) dfs(G[x][i]);
}
vs.push_back(x);
}
void rdfs(int x,int k)
{
b[x]=1;num[x]=k;
Rep(i,rG[x].size())
{
if (!b[rG[x][i]]) rdfs(rG[x][i],k);
}
}
int ssc()
{
MEM(b)
For(i,n) if (!b[i]) dfs(i);
MEM(b) int k=0;
RepD(i,vs.size()-1) if (!b[vs[i]]) rdfs(vs[i],++k);
return k;
}
};
class Two_Sat //(a||!b)&&(c||b)&&...
{
public:
int n;
SSC S;
void mem(int _n)
{
n=_n;
S.mem(2*n);
}
int no(int u){if (u>0) return u;else return n-u; }
void addedge(int u,int v) //((b∨ ~c) : b -c
{
S.addedge(no(-u),no(v));
S.addedge(no(-v),no(u));
}
}S;
bool cross(int a,int b,int c,int d) //区间[a,b] [c,d]是否相交
{
if (min(b,d)>max(a,c)) return 1;
return 0;
}
int n,s[MAXN],t[MAXN],d[MAXN];
int main()
{
// freopen("poj3683_template.in","r",stdin);
// freopen(".out","w",stdout);
cin>>n;
For(i,n)
{
int hh,mm,hh2,mm2;
scanf("%d:%d %d:%d %d",&hh,&mm,&hh2,&mm2,&d[i]);
s[i]=hh*60+mm;
t[i]=hh2*60+mm2;
t[i]-=d[i];
}
S.mem(n);
For(i,n)
For(j,n)
if (i^j)
{
if (cross(s[i],s[i]+d[i],s[j],s[j]+d[j])) S.addedge(-i,-j);
if (cross(s[i],s[i]+d[i],t[j],t[j]+d[j])) S.addedge(-i,j);
if (cross(t[i],t[i]+d[i],s[j],s[j]+d[j])) S.addedge(i,-j);
if (cross(t[i],t[i]+d[i],t[j],t[j]+d[j])) S.addedge(i,j);
}
S.S.ssc();
For(i,n)
if (S.S.num[i]==S.S.num[n+i]) {printf("NO\n");return 0; }
printf("YES\n");
For(i,n)
{
if (S.S.num[i]~i 说明i为false
else printf("%02d:%02d %02d:%02d\n",s[i]/60,s[i]%60,(s[i]+d[i])/60,(s[i]+d[i])%60);
}
return 0;
}