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.
题目大意:给n个婚礼,每个婚礼要举办一次祝福,这个祝福只能在婚礼的开始或结束的时候举办(大大的2SAT标志),问能否举办所有祝福,并输出祝福的时间段(任意解)。
思路:可以参考国家集训队2003年伍昱的论文,判冲连边就成。
PS:这题各种不严谨,没说Ti - Si ≥ Di,没保证不会超过24小时,反正都忽视掉是可以AC的,不忽视能不能AC我就不知道了……
1 #include <cstdio> 2 #include <cstring> 3 4 const int MAXN = 1010*2; 5 const int MAXM = MAXN * MAXN * 4; 6 7 struct Topological{ 8 int St[MAXN], c; 9 int n, ecnt, cnt; 10 int head[MAXN], order[MAXN], indeg[MAXN]; 11 int next[MAXM], to[MAXM]; 12 13 void addEdge(int x, int y){ 14 to[ecnt] = y; next[ecnt] = head[x]; head[x] = ecnt++; 15 ++indeg[y]; 16 //printf("%d->%d\n",x,y); 17 } 18 19 void init(int nn){ 20 n = nn; ecnt = 2; 21 memset(head, 0, sizeof(head)); 22 memset(indeg,0,sizeof(indeg)); 23 } 24 25 void build(){ 26 c = cnt = 0; 27 for(int i = 1; i <= n; ++i) 28 if(indeg[i] == 0) St[++c] = i; 29 while(c > 0){ 30 int u = St[c--]; order[cnt++] = u; 31 for(int p = head[u]; p; p = next[p]){ 32 int &v = to[p]; 33 --indeg[v]; 34 if(indeg[v] == 0) St[++c] = v; 35 } 36 } 37 } 38 } T; 39 40 struct TwoSAT{ 41 int St[MAXN], c; 42 int n, ecnt, dfs_clock, scc_cnt; 43 int head[MAXN], sccno[MAXN], pre[MAXN], lowlink[MAXN]; 44 int next[MAXM], to[MAXM]; 45 int select[MAXN], sccnox[MAXN]; 46 47 void dfs(int u){ 48 lowlink[u] = pre[u] = ++dfs_clock; 49 St[++c] = u; 50 for(int p = head[u]; p; p = next[p]){ 51 int &v = to[p]; 52 if(!pre[v]){ 53 dfs(v); 54 if(lowlink[u] > lowlink[v]) lowlink[u] = lowlink[v]; 55 }else if(!sccno[v]){ 56 if(lowlink[u] > pre[v]) lowlink[u] = pre[v]; 57 } 58 } 59 if(lowlink[u] == pre[u]){ 60 sccnox[++scc_cnt] = u; 61 while(true){ 62 int x = St[c--]; 63 sccno[x] = scc_cnt; 64 if(x == u) break; 65 } 66 } 67 } 68 69 void init(int nn){ 70 n = nn; 71 ecnt = 2; dfs_clock = scc_cnt = 0; 72 memset(head,0,sizeof(head)); 73 memset(pre,0,sizeof(pre)); 74 memset(sccno,0,sizeof(sccno)); 75 } 76 77 void addEdge(int x, int y){//x, y clash 78 to[ecnt] = y^1; next[ecnt] = head[x]; head[x] = ecnt++; 79 to[ecnt] = x^1; next[ecnt] = head[y]; head[y] = ecnt++; 80 //printf("%d<>%d\n",x,y); 81 } 82 83 bool solve(){ 84 for(int i = 0; i < n; ++i) 85 if(!pre[i]) dfs(i); 86 for(int i = 0; i < n; i += 2) 87 if(sccno[i] == sccno[i^1]) return false; 88 return true; 89 } 90 91 void bulid_select(){ 92 T.init(scc_cnt); 93 for(int u = 0; u < n; ++u){ 94 for(int p = head[u]; p; p = next[p]){ 95 int &v = to[p]; 96 if(sccno[u] == sccno[v]) continue; 97 T.addEdge(sccno[u], sccno[v]); 98 } 99 } 100 T.build(); 101 memset(select,255,sizeof(select)); 102 for(int i = T.n - 1; i > 0; --i) { 103 int &x = T.order[i]; 104 if(select[x] == -1){ 105 select[x] = 1; 106 select[sccno[sccnox[x]^1]] = 0; 107 } 108 } 109 } 110 } G; 111 112 const int MAXNN = 1010; 113 114 int a1[MAXNN], b1[MAXNN], a2[MAXNN], b2[MAXNN], a3[MAXNN], b3[MAXNN], c[MAXNN]; 115 116 inline bool clash(int beg1, int end1, int beg2, int end2){ 117 if(end1 <= beg2 || end2 <= beg1) return false; 118 return true; 119 } 120 121 int main(){ 122 int n; 123 while(scanf("%d", &n)!=EOF){ 124 G.init(n*2); 125 for(int i = 0; i < n; ++i) { 126 scanf("%d:%d %d:%d %d", &a1[i], &a2[i], &b1[i], &b2[i], &c[i]); 127 a3[i] = a1[i] * 60 + a2[i]; 128 b3[i] = b1[i] * 60 + b2[i]; 129 } 130 for(int i = 0; i < n; ++i) for(int j = i+1; j < n; ++j) if(i != j){ 131 if(clash(a3[i], a3[i] + c[i], a3[j], a3[j] + c[j])) G.addEdge(i*2, j*2); 132 if(clash(a3[i], a3[i] + c[i], b3[j] - c[j], b3[j])) G.addEdge(i*2, j*2+1); 133 if(clash(b3[i] - c[i], b3[i], a3[j], a3[j] + c[j])) G.addEdge(i*2+1, j*2); 134 if(clash(b3[i] - c[i], b3[i], b3[j] - c[j], b3[j])) G.addEdge(i*2+1, j*2+1); 135 } 136 if(G.solve()) printf("YES\n"); 137 else {printf("NO\n"); continue;} 138 G.bulid_select(); 139 for(int i = 0; i < n; ++i){ 140 //printf("%d %d\n",i*2,G.sccno[i*2]); 141 if(G.select[G.sccno[i*2]]){ 142 b1[i] = a1[i]; b2[i] = a2[i] + c[i]; 143 while(b2[i] >= 60) ++b1[i], b2[i] -= 60; 144 } else { 145 a1[i] = b1[i]; a2[i] = b2[i] - c[i]; 146 while(a2[i] < 0) --a1[i], a2[i] += 60; 147 } 148 printf("%02d:%02d %02d:%02d\n", a1[i], a2[i], b1[i], b2[i]); 149 } 150 } 151 return 0; 152 }
后记:
上面那个输出解的方法略显麻烦了,看到了一个新的方法,就是若sccno[2i]<sccno[2i+1]就选i,否则选i的对立面,不用求拓扑排序了。省了一大堆代码量。
证明:
对于任意点a、b(设~a、~b分别为他们的对立面),rank为他们的拓扑序号
那么我们选择a当且仅当rank[a]>rank[~a],选择b当且仅当rank[b]>rank[~b]
假设a和b有矛盾,那么有a到~b有路径(由对称性b到~a有路径),则rank[~b]>rank[a](rank[~a]>rank[b])
联合上式,有rank[a]>rank[~a]>rank[b]>rank[~b]>rank[a]矛盾
假设不成立,a和b没有矛盾
所以对于任意点a,选择a当且仅当rank[a]>rank[~a]是合法的
当我们用tarjan求强联通分量的时候,实际上求出来的scnno[]的值便是一个逆序的拓扑排序值。
仔细想想这没什么问题,因为再tarjan求强联通分量的时候,对于任意点x,它的后继顶点一定会比x先编入强联通分量,那么x的后继一定要比x的scnno值要小。
代码(125MS):
1 #include <cstdio> 2 #include <cstring> 3 4 const int MAXN = 1010*2; 5 const int MAXM = MAXN * MAXN * 4; 6 7 struct TwoSAT{ 8 int St[MAXN], c; 9 int n, ecnt, dfs_clock, scc_cnt; 10 int head[MAXN], sccno[MAXN], pre[MAXN], lowlink[MAXN]; 11 int next[MAXM], to[MAXM]; 12 13 void dfs(int u){ 14 lowlink[u] = pre[u] = ++dfs_clock; 15 St[++c] = u; 16 for(int p = head[u]; p; p = next[p]){ 17 int &v = to[p]; 18 if(!pre[v]){ 19 dfs(v); 20 if(lowlink[u] > lowlink[v]) lowlink[u] = lowlink[v]; 21 }else if(!sccno[v]){ 22 if(lowlink[u] > pre[v]) lowlink[u] = pre[v]; 23 } 24 } 25 if(lowlink[u] == pre[u]){ 26 ++scc_cnt; 27 while(true){ 28 int x = St[c--]; 29 sccno[x] = scc_cnt; 30 if(x == u) break; 31 } 32 } 33 } 34 35 void init(int nn){ 36 n = nn; 37 ecnt = 2; dfs_clock = scc_cnt = 0; 38 memset(head,0,sizeof(head)); 39 memset(pre,0,sizeof(pre)); 40 memset(sccno,0,sizeof(sccno)); 41 } 42 43 void addEdge(int x, int y){//x, y clash 44 to[ecnt] = y^1; next[ecnt] = head[x]; head[x] = ecnt++; 45 to[ecnt] = x^1; next[ecnt] = head[y]; head[y] = ecnt++; 46 //printf("%d<>%d\n",x,y); 47 } 48 49 bool solve(){ 50 for(int i = 0; i < n; ++i) 51 if(!pre[i]) dfs(i); 52 for(int i = 0; i < n; i += 2) 53 if(sccno[i] == sccno[i^1]) return false; 54 return true; 55 } 56 } G; 57 58 const int MAXNN = 1010; 59 60 int a1[MAXNN], b1[MAXNN], a2[MAXNN], b2[MAXNN], a3[MAXNN], b3[MAXNN], c[MAXNN]; 61 62 inline bool clash(int beg1, int end1, int beg2, int end2){ 63 if(end1 <= beg2 || end2 <= beg1) return false; 64 return true; 65 } 66 67 int main(){ 68 int n; 69 while(scanf("%d", &n)!=EOF){ 70 G.init(n*2); 71 for(int i = 0; i < n; ++i) { 72 scanf("%d:%d %d:%d %d", &a1[i], &a2[i], &b1[i], &b2[i], &c[i]); 73 a3[i] = a1[i] * 60 + a2[i]; 74 b3[i] = b1[i] * 60 + b2[i]; 75 } 76 for(int i = 0; i < n; ++i) for(int j = i+1; j < n; ++j) if(i != j){ 77 if(clash(a3[i], a3[i] + c[i], a3[j], a3[j] + c[j])) G.addEdge(i*2, j*2); 78 if(clash(a3[i], a3[i] + c[i], b3[j] - c[j], b3[j])) G.addEdge(i*2, j*2+1); 79 if(clash(b3[i] - c[i], b3[i], a3[j], a3[j] + c[j])) G.addEdge(i*2+1, j*2); 80 if(clash(b3[i] - c[i], b3[i], b3[j] - c[j], b3[j])) G.addEdge(i*2+1, j*2+1); 81 } 82 if(G.solve()) printf("YES\n"); 83 else {printf("NO\n"); continue;} 84 for(int i = 0; i < n; ++i){ 85 if(G.sccno[i * 2] < G.sccno[i * 2 + 1]) { 86 b1[i] = a1[i]; b2[i] = a2[i] + c[i]; 87 while(b2[i] >= 60) ++b1[i], b2[i] -= 60; 88 } else { 89 a1[i] = b1[i]; a2[i] = b2[i] - c[i]; 90 while(a2[i] < 0) --a1[i], a2[i] += 60; 91 } 92 printf("%02d:%02d %02d:%02d\n", a1[i], a2[i], b1[i], b2[i]); 93 } 94 } 95 return 0; 96 }