Time Limit: 2000MS | Memory Limit: 65536K | |||
Total Submissions: 8300 | Accepted: 2834 | 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 timeTi. 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 Diminutes 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可行性判断+方案输出模板题。
输出方案的方法:
缩点后将图反向,建成新图。
然后拓扑排序。
最后进行染色标记,只传递不选择的标记,不传递选择标记。
#include <iostream> #include <cstring> #include <cstdio> #include <algorithm> #include <cstdlib> #include <cmath> #include <stack> #define m 5005 using namespace std; int cf[m],l[m],t=0,sccno[m],pre[m],n,h[m],h2[m],a[m],b[m],done[m],color[m],scc_cnt,tot=0,tot2=0,lowlink[m],id[m]; int dfs_clock=0; stack<int> S; struct edge { int y,ne; }e[m*200],e2[m*200]; char s[10]; int Change(char *s) { int ans=0; ans=(s[0]-'0')*600+(s[1]-'0')*60+(s[3]-'0')*10+s[4]-'0'; return ans; } bool Conflict(int a,int b,int x,int y) { if (b<=x||y<=a) return false; return true; } void Add(int x,int y) { tot++; e[tot].ne=h[x]; e[tot].y=y; h[x]=tot; } void Add2(int x,int y) { tot2++; e2[tot2].ne=h2[x]; e2[tot2].y=y; h2[x]=tot2; } void dfs(int u) { pre[u]=lowlink[u]=++dfs_clock; S.push(u); for (int i=h[u];i;i=e[i].ne) { int v=e[i].y; if (!pre[v]) { dfs(v); lowlink[u]=min(lowlink[u],lowlink[v]); } else if (!sccno[v]) lowlink[u]=min(lowlink[u],pre[v]); } if (pre[u]==lowlink[u]) { scc_cnt++; for (;;) { int x=S.top(); S.pop(); sccno[x]=scc_cnt; if (x==u) break; } } } void Findscc() { dfs_clock=scc_cnt=0; memset(pre,0,sizeof(pre)); memset(sccno,0,sizeof(sccno)); for (int i=1;i<=n*2;i++) if (!pre[i]) dfs(i); } void Topsort(int x) //拓扑排序 { id[x]=-1; done[++t]=x; for (int i=h2[x];i;i=e2[i].ne) { int y=e2[i].y; id[y]--; if (!id[y]) Topsort(y); } } void Paint(int x) //染色,即传递不选择标记 { color[x]=2; for (int i=h2[x];i;i=e2[i].ne) { int y=e2[i].y; if (!color[y]) Paint(y); } } void Print(int x) { if (x/60<10) printf("0"); printf("%d:",x/60); if (x%60<10) printf("0"); printf("%d",x%60); } int main() { scanf("%d",&n); for (int i=1;i<=n;i++) { scanf("%s",s); a[i]=Change(s); scanf("%s",s); b[i]=Change(s); scanf("%d",&l[i]); } for (int i=1;i<=n;i++) for (int j=i+1;j<=n;j++) { if (Conflict(a[i],a[i]+l[i],a[j],a[j]+l[j])) Add(i,j+n),Add(j,i+n); if (Conflict(a[i],a[i]+l[i],b[j]-l[j],b[j])) Add(i,j),Add(j+n,i+n); if (Conflict(b[i]-l[i],b[i],a[j],a[j]+l[j])) Add(i+n,j+n),Add(j,i); if (Conflict(b[i]-l[i],b[i],b[j]-l[j],b[j])) Add(i+n,j),Add(j+n,i); } Findscc(); for (int i=1;i<=n;i++) //判断可行性 if (sccno[i]==sccno[i+n]) { printf("NO\n"); return 0; } printf("YES\n"); for (int i=1;i<=2*n;i++) //建立反向后的新图 for (int j=h[i];j;j=e[j].ne) { int y=e[j].y; if (sccno[y]!=sccno[i]) { Add2(sccno[y],sccno[i]); id[sccno[i]]++; } } for (int i=1;i<=scc_cnt;i++) if (!id[i]) Topsort(i); for (int i=1;i<=n;i++) //找到每一个块的对立块(由对称性可知,对立块是唯一的) cf[sccno[i]]=sccno[i+n],cf[sccno[i+n]]=sccno[i]; for (int i=1;i<=scc_cnt;i++) { int x=done[i]; if (color[x]!=0) continue; color[x]=1; Paint(cf[x]); } for (int i=1;i<=n;i++) if (color[sccno[i]]==1) Print(a[i]),printf(" "),Print(a[i]+l[i]),printf("\n"); else Print(b[i]-l[i]),printf(" "),Print(b[i]),printf("\n"); return 0; }
感悟:
1.WA是"YES"写成了"Yes";在拓扑排序中用到的是缩点之后(e2,h2)的图,而我还用的缩点前(e,h)的。
RE是数组开小:edge最多可能有n^2条边
2.对于2-SAT的正确性至今还不能完全的理解。。目前的理解就是:
若选择B'的后代结点A,那么A'将被标记为不选择,然后不选择标记下传给A'的后代结点B;
因为整个图是对称的,所以选A的前代结点是B',A'的后代结点一定是B。
图的对称性:
12.19UPD:
对于2—SAT的新理解:
Q:为什么要把图反向呢?
A:当选择了A点,那么A'被标记为不选择,此时要把不选择的标记向前面传递(A'不选择,A'之前的一定不选择,A'之后的不一定)。
如果此时图没有反向,那么要找到A'之前的点很麻烦(现在连得边都是向后走的);
而反向之后直接沿着边走,就相当于沿着反向边走了!
Q:为什么要拓扑排序呢?
A:我们从入度为零的点A开始染色,A标记为选择,那么A'标记为不选择;
因为图有对称性,A'的出度一定为0,只要把A’标记为不可选择就行了,此时没有其他任何点受到影响。。
然后把A和A’当做删掉了。
接下来继续按照拓扑序找到下一个点B,此时B入度为0,B'出度为0,只要把B’标记为不可选择就行了,没有必要写一个Paint()函数,
因为他后面的点之前就已经置好标记了!
附上新的代码:
#include <iostream> #include <cstring> #include <cstdio> #include <algorithm> #include <cstdlib> #include <cmath> #include <stack> #define m 5005 using namespace std; int cf[m],l[m],t=0,sccno[m],pre[m],n,h[m],h2[m],a[m],b[m],done[m],color[m],scc_cnt,tot=0,tot2=0,lowlink[m],id[m]; int dfs_clock=0; stack<int> S; struct edge { int y,ne; }e[m*200],e2[m*200]; char s[10]; int Change(char *s) { int ans=0; ans=(s[0]-'0')*600+(s[1]-'0')*60+(s[3]-'0')*10+s[4]-'0'; return ans; } bool Conflict(int a,int b,int x,int y) { if (b<=x||y<=a) return false; return true; } void Add(int x,int y) { tot++; e[tot].ne=h[x]; e[tot].y=y; h[x]=tot; } void Add2(int x,int y) { tot2++; e2[tot2].ne=h2[x]; e2[tot2].y=y; h2[x]=tot2; } void dfs(int u) { pre[u]=lowlink[u]=++dfs_clock; S.push(u); for (int i=h[u];i;i=e[i].ne) { int v=e[i].y; if (!pre[v]) { dfs(v); lowlink[u]=min(lowlink[u],lowlink[v]); } else if (!sccno[v]) lowlink[u]=min(lowlink[u],pre[v]); } if (pre[u]==lowlink[u]) { scc_cnt++; for (;;) { int x=S.top(); S.pop(); sccno[x]=scc_cnt; if (x==u) break; } } } void Findscc() { dfs_clock=scc_cnt=0; memset(pre,0,sizeof(pre)); memset(sccno,0,sizeof(sccno)); for (int i=1;i<=n*2;i++) if (!pre[i]) dfs(i); } void Topsort(int x) { id[x]=-1; done[++t]=x; for (int i=h2[x];i;i=e2[i].ne) { int y=e2[i].y; id[y]--; if (!id[y]) Topsort(y); } } void Print(int x) { if (x/60<10) printf("0"); printf("%d:",x/60); if (x%60<10) printf("0"); printf("%d",x%60); } int main() { scanf("%d",&n); for (int i=1;i<=n;i++) { scanf("%s",s); a[i]=Change(s); scanf("%s",s); b[i]=Change(s); scanf("%d",&l[i]); } for (int i=1;i<=n;i++) for (int j=i+1;j<=n;j++) { if (Conflict(a[i],a[i]+l[i],a[j],a[j]+l[j])) Add(i,j+n),Add(j,i+n); if (Conflict(a[i],a[i]+l[i],b[j]-l[j],b[j])) Add(i,j),Add(j+n,i+n); if (Conflict(b[i]-l[i],b[i],a[j],a[j]+l[j])) Add(i+n,j+n),Add(j,i); if (Conflict(b[i]-l[i],b[i],b[j]-l[j],b[j])) Add(i+n,j),Add(j+n,i); } Findscc(); for (int i=1;i<=n;i++) if (sccno[i]==sccno[i+n]) { printf("NO\n"); return 0; } printf("YES\n"); for (int i=1;i<=2*n;i++) for (int j=h[i];j;j=e[j].ne) { int y=e[j].y; if (sccno[y]!=sccno[i]) { Add2(sccno[y],sccno[i]); id[sccno[i]]++; } } for (int i=1;i<=scc_cnt;i++) if (!id[i]) Topsort(i); for (int i=1;i<=n;i++) cf[sccno[i]]=sccno[i+n],cf[sccno[i+n]]=sccno[i]; for (int i=1;i<=scc_cnt;i++) { int x=done[i]; if (color[x]!=0) continue; color[x]=1; color[cf[x]]=2; //原来这里是paint()函数,直接删掉 } for (int i=1;i<=n;i++) if (color[sccno[i]]==1) Print(a[i]),printf(" "),Print(a[i]+l[i]),printf("\n"); else Print(b[i]-l[i]),printf(" "),Print(b[i]),printf("\n"); return 0; }
(时间短了~)