Time Limit: 2000MS | Memory Limit: 65536K | |||
Total Submissions: 8640 | Accepted: 2947 | 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
题目大意:某一天结婚的人特别多但是主持婚礼的神父只有一个。婚礼时间从s开始到e结束,神父必须在s到s+d或者e-d到e这段时间内在。给定了n个婚礼的s,e,d,求一种方案能使得神父主持所有的婚礼。
我们把每个婚礼可能的两个时间段看做两个点 A 和 A’,显然,如果两个时间段冲突(比如 A 和 B 的时间重合),那么需要建边(A -> B' ),(B -> A‘),判断出是否存在解后,需要输出一组解,这里的方法是 赵爽 的《2-SAT 解法浅析》里面看的,有详细的证明。
具体操作就是:求强联通,缩点重新建图(这里建反向图,参见论文),然后给图中的点着色,将一个未着色点 x 上色同时,把与它矛盾的点 y 以及 y 的所有子孙节点上另外一种颜色,上色完成后,进行拓扑排序,选择一种颜色的点输出就是一组可行解。
代码:
var a:array [1..3010,0..3010] of longint; v,f:array [1..3010] of boolean; low,dfn,st,belong,c:Array [1..3010] of longint; ff:array [1..3010,1..4] of longint; a1,a2,a3,a4,i,j,m,n,x,y,mm,nn,p,ii,s2,len,s1,d,jj,t,k:longint; ch:char; fff:boolean; s:string; procedure input1; begin readln(m); for i:=1 to m do begin readln(s); len:=length(s); jj:=0; x:=ord(s[1])*10+ord(s[2])-ord('0')*11; y:=ord(s[4])*10+ord(s[5])-ord('0')*11; mm:=ord(s[7])*10+ord(s[8])-ord('0')*11; nn:=ord(s[10])*10+ord(s[11])-ord('0')*11; for j:=13 to len do jj:=jj*10+ord(s[j])-ord('0'); s1:=x*60+y; s2:=mm*60+nn; ff[i,1]:=s1; ff[i,2]:=s1+jj; ff[i,3]:=s2-jj; ff[i,4]:=s2; end; end; procedure output1; begin for i:=1 to m do begin if belong[i]>belong[i+m] then begin a1:=ff[i,1] div 60; a2:=ff[i,1] mod 60; a3:=ff[i,2] div 60; a4:=ff[i,2] mod 60; writeln(a1 div 10,a1 mod 10,':',a2 div 10,a2 mod 10,' ',a3 div 10,a3 mod 10,':',a4 div 10,a4 mod 10); end else begin a1:=ff[i,3] div 60; a2:=ff[i,3] mod 60; a3:=ff[i,4] div 60; a4:=ff[i,4] mod 60; writeln(a1 div 10,a1 mod 10,':',a2 div 10,a2 mod 10,' ',a3 div 10,a3 mod 10,':',a4 div 10,a4 mod 10); end; end; end; function pp:longint; begin f[st[t]]:=false; dec(t); exit(st[t+1]); end; procedure add(x,y:longint); begin inc(c[x]); a[x,c[x]]:=y; end; procedure check1; var i:longint; begin for i:=1 to m do if belong[i]=belong[m+i] then begin fff:=false; exit; end; end; function min(x,y:longint):longint; begin if x>y then exit(y); exit(x); end; procedure check(x,y:longint); begin if not ((ff[x,2]<=ff[y,1]) or (ff[x,1]>=ff[y,2])) then begin add(y+m,x); add(x+m,y); end; if not ((ff[x,4]<=ff[y,3]) or (ff[x,3]>=ff[y,4])) then begin add(y,x+m); add(x,y+m); end; if not ((ff[x,3]>=ff[y,2]) or (ff[x,4]<=ff[y,1])) then begin add(x,y); add(y+m,m+x); end; if not ((ff[x,2]<=ff[y,3]) or (ff[x,1]>=ff[y,4])) then begin add(y,x); add(x+m,y+m); end; end; procedure tarjan(x:longint); var i,y:longint; begin inc(d); low[x]:=d; dfn[x]:=d; inc(t); st[t]:=x; f[x]:=true; for i:=1 to c[x] do begin if not v[a[x,i]] then begin v[a[x,i]]:=true; tarjan(a[x,i]); low[x]:=min (low[x],low[a[x,i]]); end else begin if f[a[x,i]] then low[x]:=min(low[x],dfn[a[x,i]]); end; end; if dfn[x]=low[x] then begin inc(p); belong[x]:=p; y:=x-1; while x<>y do begin y:=pp; belong[y]:=p; end; end; end; begin input1; for i:=1 to m do for j:=i+1 to m do check(i,j); for i:=1 to 2*m do if not v[i] then begin v[i]:=true; tarjan(i); end; fff:=true; check1; if fff then writeln('YES') else begin writeln('NO'); halt; end; output1; end.