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
2-sat问题,把一对夫妻的2个时间段看成一个点对,如果与其他人矛盾就连一条边
#include<set> #include<map> #include<list> #include<queue> #include<vector> #include<cstdio> #include<cstring> #include<iostream> #include<algorithm> using namespace std; const int N=2100; struct node { int from; int to; int next; }edge1[N*N],edge2[N*N]; int head1[N],head2[N]; int tot1,tot2,sccnum,index,Top,cnt; int DFN[N],low[N]; int stack[N]; int in_deg[N]; int cfl[N]; int toposort[N]; int block[N]; int color[N]; bool instack[N]; int s[1010],t[1010],d[1010]; void addedge1(int from,int to)//原图 { edge1[tot1].from=from; edge1[tot1].to=to; edge1[tot1].next=head1[from]; head1[from]=tot1++; } void addedge2(int from,int to)//缩点后的逆图 { edge1[tot2].from=from; edge2[tot2].to=to; edge2[tot2].next=head2[from]; head2[from]=tot2++; } void tarjan(int u) { DFN[u]=low[u]=++index; stack[Top++]=u; instack[u]=1; for (int i=head1[u];i!=-1;i=edge1[i].next) { int v=edge1[i].to; 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]) { int v; sccnum++; do { Top--; v=stack[Top]; block[v]=sccnum; instack[v]=0; } while (v!=u); } } void topo_sort() { queue<int>qu; while (!qu.empty()) qu.pop(); for (int i=1;i<=sccnum;i++)//这里已经用到逆图了 if (in_deg[i]==0) qu.push(i); while (!qu.empty()) { int u=qu.front(); if(color[u]==0) { color[u]=1; color[cfl[u]]=-1; } qu.pop(); for (int i=head2[u];i!=-1;i=edge2[i].next) { int v=edge2[i].to; toposort[++cnt]=v; in_deg[v]--; if (in_deg[v]==0) qu.push(v); } } } void init() { memset(instack,0,sizeof(instack)); memset(head1,-1,sizeof(head1)); memset(head2,-1,sizeof(head2)); memset(DFN,0,sizeof(DFN)); memset(low,0,sizeof(low)); memset(color,0,sizeof(color)); memset(in_deg,0,sizeof(in_deg)); cnt=0; sccnum=0; index=0; tot1=0; tot2=0; Top=0; } bool judge(int s1,int t1,int s2,int t2) { if(t1<=s2 || t2<=s1) return false; return true; } void out_put(int s,int t) { if(s/60<10) printf("0"); printf("%d:",s/60); s%=60; if(s<10) printf("0"); printf("%d",s); printf(" "); if(t/60<10) printf("0"); printf("%d:",t/60); t%=60; if(t<10) printf("0"); printf("%d\n",t); } int main() { int n; while(~scanf("%d",&n)) { init(); int h1,m1,h2,m2; bool flag=false; for(int i=1;i<=n;i++) { scanf("%d:%d %d:%d %d",&h1,&m1,&h2,&m2,&d[i]); s[i]=h1*60+m1; t[i]=h2*60+m2; } for(int i=1;i<=n;i++) for(int j=i+1;j<=n;j++) { if(judge(s[i],s[i]+d[i],s[j],s[j]+d[j])) { addedge1(i,j+n); addedge1(j,i+n); } if(judge(s[i],s[i]+d[i],t[j]-d[j],t[j])) { addedge1(i,j); addedge1(j+n,i+n); } if(judge(t[i]-d[i],t[i],s[j],s[j]+d[j])) { addedge1(i+n,j+n); addedge1(j,i); } if(judge(t[i]-d[i],t[i],t[j]-d[j],t[j])) { addedge1(i+n,j); addedge1(j+n,i); } } for(int i=1;i<=2*n;i++) if(DFN[i]==0) tarjan(i); for(int i=1;i<=n;i++) { if(block[i]==block[i+n]) { flag=true; break; } } if(flag) { printf("NO\n"); continue; } //既然有解,缩点后建立逆图,并且把矛盾块找好 for(int i=1;i<=sccnum;i++) { if(block[edge1[i].from]!=block[edge1[i].to]) { addedge2(block[edge1[i].from],block[edge1[i].to]); } } for(int i=1;i<=n;i++) { cfl[block[i]]=block[i+n]; cfl[block[i+n]]=block[i]; } topo_sort(); printf("YES\n"); for(int i=1;i<=n;i++) { if(color[block[i]]==1) out_put(s[i],s[i]+d[i]); else out_put(t[i]-d[i],t[i]); } } return 0; }