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 toTi). 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做的一路蒙蔽
判断个人觉得很简单,
输出解就要命了
有一个小镇上只有一个牧师。这个小镇上有一个传说,
在九月一日结婚的人会受到爱神的保佑,但是要牧师举办一个仪式。
这个仪式要么在婚礼刚刚开始的时候举行,要么举行完婚礼正好结束。
现在已知有n场婚礼,告诉你每一场的开始和结束时间,
以及举行仪式所需要的时间。问牧师能否参加所有的婚礼,
如果能则输出一种方案。
这题输出解的方法
构建包含2n个点的有向图,如果有a+b则在a和!b b和!a间连接一条边。
如果a和!a在一个强连通分量中,则无解。要求解集,
只需要将原图缩点后反向建图,然后染色,
具体染色方法是将遇到的第一个没有颜色的点染成红色,与它矛盾的点染成蓝色,
如此循环,所有的红色的点的集合就是解集。
多看点书还是有好处的 ,
这是大佬讲的,理解不了就记忆吧 ,也许这就是弱鸡吧
求大佬给出证明
这题的建图非常简单就没必要讲了
难受啊!!!!
1 #include <iostream> 2 #include <cstdio> 3 #include <cstring> 4 #include <queue> 5 #include <vector> 6 7 using namespace std; 8 const int maxn = 4e5 + 10; 9 struct w { 10 int s, e; 11 void disp() { 12 printf("%02d:%02d %02d:%02d ", s / 60, s % 60, e / 60, e % 60); 13 } 14 } wed[maxn]; 15 struct node { 16 int u, v, next; 17 } edge[maxn]; 18 int dfn[maxn], s[maxn], instack[maxn]; 19 int head[maxn], low[maxn], belong[maxn]; 20 int tot, flag, cnt, top, n, m; 21 void add(int u, int v) { 22 edge[tot].u = u; 23 edge[tot].v = v; 24 edge[tot].next = head[u]; 25 head[u] = tot++; 26 } 27 void init() { 28 tot = flag = top = cnt = 0; 29 memset(dfn, 0, sizeof(dfn)); 30 memset(head, -1, sizeof(head)); 31 memset(instack, 0, sizeof(head)); 32 memset(s, 0, sizeof(s)); 33 } 34 void tarjan(int v) { 35 low[v] = dfn[v] = ++flag; 36 instack[v] = 1; 37 s[top++] = v; 38 for (int i = head[v] ; ~i ; i = edge[i].next ) { 39 int j = edge[i].v; 40 if (!dfn[j]) { 41 tarjan(j); 42 low[v] = min(low[v], low[j]); 43 } else if (instack[j]) low[v] = min(low[v], dfn[j]); 44 } 45 if (dfn[v] == low[v]) { 46 cnt++; 47 int t; 48 do { 49 t = s[--top]; 50 instack[t] = 0; 51 belong[t] = cnt; 52 } while(t != v); 53 } 54 } 55 56 int check(int i, int j) { 57 if (wed[i].s >= wed[j].e || wed[i].e <= wed[j].s ) return 0; 58 return 1; 59 } 60 void build(int i, int j) { 61 if (check(2 * i, 2 * j)) add(2 * i, 2 * j + 1); 62 if (check(2 * i, 2 * j + 1)) add(2 * i, 2 * j); 63 if (check(2 * i + 1, 2 * j)) add(2 * i + 1, 2 * j + 1); 64 if (check(2 * i + 1, 2 * j + 1)) add(2 * i + 1, 2 * j); 65 } 66 int in[maxn]; 67 queue<int>q; 68 vector<int>tu[maxn]; 69 vector<int>ha[maxn]; 70 int color[maxn]; 71 void maketu() { 72 int v; 73 for (int u = 0 ; u < 2 * n ; u++) { 74 for (int i = head[u] ; ~i ; i = edge[i].next) { 75 v = edge[i].v; 76 if (belong[u] != belong[v]) { 77 tu[belong[v]].push_back(belong[u]); 78 in[belong[u]]++; 79 } 80 } 81 } 82 } 83 84 void topsort() { 85 for (int i = 1 ; i <= cnt ; i++) 86 if (!in[i]) q.push(i); 87 int u, v; 88 while(!q.empty()) { 89 u = q.front(); 90 q.pop(); 91 if (!color[u]) { 92 color[u] = 1; 93 for (int i = 0 ; i < ha[u].size() ; i++) 94 color[ha[u][i]] = 2; 95 } 96 for (int i = 0 ; i < tu[u].size() ; i++) { 97 v = tu[u][i]; 98 in[v]--; 99 if (!in[v]) q.push(v); 100 } 101 } 102 } 103 void solve() { 104 for (int i = 0 ; i < n ; i++) { 105 if (belong[i << 1] == belong[i << 1 | 1]) { 106 printf("NO "); 107 return ; 108 } else { 109 ha[belong[i << 1]].push_back(belong[i << 1 | 1]); 110 ha[belong[i << 1 | 1]].push_back(belong[i << 1]); 111 } 112 } 113 printf("YES "); 114 maketu(); 115 topsort(); 116 for (int i = 0 ; i < n ; i++) { 117 if (color[belong[i << 1]] == 1) wed[i << 1].disp(); 118 else wed[i << 1 | 1].disp(); 119 } 120 } 121 122 int main() { 123 // freopen("DATA.txt", "r", stdin); 124 scanf("%d", &n); 125 init(); 126 int x, y, x1, y1, d; 127 for (int i = 0 ; i < n ; i++) { 128 scanf("%d:%d %d:%d %d", &x, &y, &x1, &y1, &d); 129 wed[i << 1].s = x * 60 + y; 130 wed[i << 1].e = wed[i << 1].s + d; 131 wed[i << 1 | 1].e = x1 * 60 + y1; 132 wed[i << 1 | 1].s = wed[i << 1 | 1].e-d; 133 } 134 for (int i = 0 ; i < n ; i++) 135 for (int j = 0 ; j < n ; j++) 136 if (i != j) build(i, j); 137 for (int i = 0 ; i < 2 * n ; i++) 138 if (!dfn[i]) tarjan(i); 139 solve(); 140 return 0; 141 }