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 SiTi and DiSi 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 }
141MS

相关文章:

  • 2021-12-30
  • 2021-07-24
  • 2021-08-01
  • 2021-10-01
  • 2021-07-28
  • 2022-12-23
猜你喜欢
  • 2021-09-16
  • 2021-08-01
  • 2022-01-16
  • 2021-12-23
  • 2021-10-07
  • 2022-12-23
  • 2022-01-25
相关资源
相似解决方案