Time Limit: 2000MS | Memory Limit: 65536K | |||
Total Submissions: 9545 | Accepted: 3256 | 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 yearN couples plan to get married on the blessed day. The i-th couple plan to hold their wedding from timeSi 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. Thei-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 fromSi to Si + Di, or fromTi - 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 andDi. Si and Ti are in the format ofhh: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 anotherN 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 题意:在一个小村庄,杰森作为婚礼祝福者,在某一天,有n场婚礼,杰森要给所有婚礼祝福,时间在婚礼的开始或结束之前。问杰森是否能参加所有婚礼,若可以则输出参加的时间。 思路:很明显的2—sat的题目,一场婚礼可以有两个参加的时间段,婚礼之间可能会发生冲突,解决这种问题通常使用2-sat。 注意点:1.数组要开的足够大,本人由于没开足够大的数组wa了半个小时。 2.注意运算符之间的优先级比如i!=(j^1),注意,如果不打括号是会发生错误的。 个人关于2-sat的一点小理解: 1.由于这是做的第一道2sat,建图的时候总感觉不太对。当两场婚礼的时间段有所冲突的时候,就在冲突与不冲突之间连一条边,这样的连边正确么??? 从理解上来上,冲突之后的确只能这样选择,但是如果考虑到连的边也是冲突的话,那么这相应点就会在一个强连通分量,在最终判断的时候终究是NO,所以建图没有问题。 2.建边的时候是addedge(i,j^1);addedge(j,i^1),由于是有向图,建边方向很重要,为何要这样建??? 这是我个人的小猜测:由于i和j是冲突的时间段,所以i和j是不能形成互联的状态的。这样的连边方式就是为了杜绝这个现象。 代码://#include<bits/stdc++h> #include<stdio.h> #include<string.h> #include<algorithm> #include<string> #include<math.h> #include<map> #include<vector> #include<set> #include<queue> using namespace std; const int N = 2010; const int M = N*N; struct node { int l,r,id; node (int _l=0,int _r=0,int _id=0) { l=_l; r=_r; id=_id; } } A[N*2]; struct Edge { int from, to, next; } edge[M], edge2[M]; int head[N]; int cntE, cntE2; void addedge(int u, int v) { edge[cntE].from = u; edge[cntE].to = v; edge[cntE].next = head[u]; head[u] = cntE++; } void addedge2(int u, int v) { edge2[cntE2].from = u; edge2[cntE2].to = v; edge2[cntE2].next = head[u]; head[u] = cntE2++; } int dfn[N], low[N], idx; int stk[N], top; int in[N]; int kind[N], cnt; void tarjan(int u) { dfn[u] = low[u] = ++idx; in[u] = true; stk[++top] = u; for (int i = head[u]; i != -1; i = edge[i].next) { int v = edge[i].to; if (!dfn[v]) tarjan(v), low[u] = min(low[u], low[v]); else if (in[v]) low[u] = min(low[u], dfn[v]); } if (low[u] == dfn[u]) { ++cnt; while (1) { int v = stk[top--]; kind[v] = cnt; in[v] = false; if (v == u) break; } } } int opp[N], ind[N], col[N]; // 相对的点 入度 染色 col[]=1选择 bool topsort(int n) // 序号从0开始 { for (int i = 0; i < 2*n; i += 2) { int k1 = kind[i]; int k2 = kind[i^1]; // 相对的两个点 if (k1 == k2) return false; opp[k1] = k2; opp[k2] = k1; } memset(head, -1, sizeof head); int u, v; for (int i = 0; i < cntE; ++i) { u = edge[i].from, v = edge[i].to; if (kind[u] != kind[v]) // 反向建图 { addedge2(kind[v], kind[u]); ind[kind[u]]++; } } queue<int> q; for (int i = 1; i <= cnt; ++i) if (!ind[i]) q.push(i); while (q.size()) { u = q.front(); q.pop(); if (!col[u]) col[u] = 1, col[ opp[u] ] = -1; for (int i = head[u]; i != -1; i = edge2[i].next) if (--ind[edge2[i].to] == 0) q.push(edge2[i].to); } return true; } void init() { cntE = cntE2 = 0; memset(head, -1, sizeof head); memset(dfn, 0, sizeof dfn); memset(in, false, sizeof in); idx = top = cnt = 0; memset(ind, 0, sizeof ind); memset(col, 0, sizeof col); } int judge(int i,int j) { if(A[i].l<=A[j].l&&A[j].l<A[i].r)return 1; if(A[j].l<=A[i].l&&A[i].l<A[j].r)return 1; return 0; } int main() { int n; scanf("%d",&n); int a,b,c,d,e,flag=0; init(); for(int i=0; i<n; i++) { scanf("%d:%d %d:%d %d",&a,&b,&c,&d,&e); int l=a*60+b,r=c*60+d; A[i*2]= {l,l+e,i}; A[i*2+1]= {r-e,r,i}; } for(int i=0; i<2*n; i++) for(int j=i+1; j<2*n; j++) { if(i!=(j^1)&&judge(i,j)) { addedge(i,j^1); addedge(j,i^1); } } for(int i=0; i<2*n; i++) if(!dfn[i]) tarjan(i); for(int i=0;i<2*n;i+=2) if(kind[i]==kind[i+1]) { printf("NO\n"); return 0; }/* printf("YES\n"); for(int i=0;i<2*n;i+=2) if(kind[i]<kind[i+1]) { printf("%02d:%02d %02d:%02d\n",A[i].l/60,A[i].l%60,A[i].r/60,A[i].r%60); } else printf("%02d:%02d %02d:%02d\n",A[i+1].l/60,A[i+1].l%60,A[i+1].r/60,A[i+1].r%60); */if(topsort(n)) { printf("YES\n"); for(int i=0; i<2*n; i++) if(col[kind[i]]==1) { printf("%02d:%02d %02d:%02d\n",A[i].l/60,A[i].l%60,A[i].r/60,A[i].r%60); } } else printf("NO\n"); return 0; }