POJ 3683 Priest John's Busiest Day(2-SAT 并输出解)
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我就不知道了……
- #include <cstdio>
- #include <cstring>
- const int MAXN = *;
- const int MAXM = MAXN * MAXN * ;
- struct Topological{
- int St[MAXN], c;
- int n, ecnt, cnt;
- int head[MAXN], order[MAXN], indeg[MAXN];
- int next[MAXM], to[MAXM];
- void addEdge(int x, int y){
- to[ecnt] = y; next[ecnt] = head[x]; head[x] = ecnt++;
- ++indeg[y];
- //printf("%d->%d\n",x,y);
- }
- void init(int nn){
- n = nn; ecnt = ;
- memset(head, , sizeof(head));
- memset(indeg,,sizeof(indeg));
- }
- void build(){
- c = cnt = ;
- for(int i = ; i <= n; ++i)
- if(indeg[i] == ) St[++c] = i;
- while(c > ){
- int u = St[c--]; order[cnt++] = u;
- for(int p = head[u]; p; p = next[p]){
- int &v = to[p];
- --indeg[v];
- if(indeg[v] == ) St[++c] = v;
- }
- }
- }
- } T;
- struct TwoSAT{
- int St[MAXN], c;
- int n, ecnt, dfs_clock, scc_cnt;
- int head[MAXN], sccno[MAXN], pre[MAXN], lowlink[MAXN];
- int next[MAXM], to[MAXM];
- int select[MAXN], sccnox[MAXN];
- void dfs(int u){
- lowlink[u] = pre[u] = ++dfs_clock;
- St[++c] = u;
- for(int p = head[u]; p; p = next[p]){
- int &v = to[p];
- if(!pre[v]){
- dfs(v);
- if(lowlink[u] > lowlink[v]) lowlink[u] = lowlink[v];
- }else if(!sccno[v]){
- if(lowlink[u] > pre[v]) lowlink[u] = pre[v];
- }
- }
- if(lowlink[u] == pre[u]){
- sccnox[++scc_cnt] = u;
- while(true){
- int x = St[c--];
- sccno[x] = scc_cnt;
- if(x == u) break;
- }
- }
- }
- void init(int nn){
- n = nn;
- ecnt = ; dfs_clock = scc_cnt = ;
- memset(head,,sizeof(head));
- memset(pre,,sizeof(pre));
- memset(sccno,,sizeof(sccno));
- }
- void addEdge(int x, int y){//x, y clash
- to[ecnt] = y^; next[ecnt] = head[x]; head[x] = ecnt++;
- to[ecnt] = x^; next[ecnt] = head[y]; head[y] = ecnt++;
- //printf("%d<>%d\n",x,y);
- }
- bool solve(){
- for(int i = ; i < n; ++i)
- if(!pre[i]) dfs(i);
- for(int i = ; i < n; i += )
- if(sccno[i] == sccno[i^]) return false;
- return true;
- }
- void bulid_select(){
- T.init(scc_cnt);
- for(int u = ; u < n; ++u){
- for(int p = head[u]; p; p = next[p]){
- int &v = to[p];
- if(sccno[u] == sccno[v]) continue;
- T.addEdge(sccno[u], sccno[v]);
- }
- }
- T.build();
- memset(select,,sizeof(select));
- for(int i = T.n - ; i > ; --i) {
- int &x = T.order[i];
- if(select[x] == -){
- select[x] = ;
- select[sccno[sccnox[x]^]] = ;
- }
- }
- }
- } G;
- const int MAXNN = ;
- int a1[MAXNN], b1[MAXNN], a2[MAXNN], b2[MAXNN], a3[MAXNN], b3[MAXNN], c[MAXNN];
- inline bool clash(int beg1, int end1, int beg2, int end2){
- if(end1 <= beg2 || end2 <= beg1) return false;
- return true;
- }
- int main(){
- int n;
- while(scanf("%d", &n)!=EOF){
- G.init(n*);
- for(int i = ; i < n; ++i) {
- scanf("%d:%d %d:%d %d", &a1[i], &a2[i], &b1[i], &b2[i], &c[i]);
- a3[i] = a1[i] * + a2[i];
- b3[i] = b1[i] * + b2[i];
- }
- for(int i = ; i < n; ++i) for(int j = i+; j < n; ++j) if(i != j){
- if(clash(a3[i], a3[i] + c[i], a3[j], a3[j] + c[j])) G.addEdge(i*, j*);
- if(clash(a3[i], a3[i] + c[i], b3[j] - c[j], b3[j])) G.addEdge(i*, j*+);
- if(clash(b3[i] - c[i], b3[i], a3[j], a3[j] + c[j])) G.addEdge(i*+, j*);
- if(clash(b3[i] - c[i], b3[i], b3[j] - c[j], b3[j])) G.addEdge(i*+, j*+);
- }
- if(G.solve()) printf("YES\n");
- else {printf("NO\n"); continue;}
- G.bulid_select();
- for(int i = ; i < n; ++i){
- //printf("%d %d\n",i*2,G.sccno[i*2]);
- if(G.select[G.sccno[i*]]){
- b1[i] = a1[i]; b2[i] = a2[i] + c[i];
- while(b2[i] >= ) ++b1[i], b2[i] -= ;
- } else {
- a1[i] = b1[i]; a2[i] = b2[i] - c[i];
- while(a2[i] < ) --a1[i], a2[i] += ;
- }
- printf("%02d:%02d %02d:%02d\n", a1[i], a2[i], b1[i], b2[i]);
- }
- }
- return ;
- }
141MS
后记:
上面那个输出解的方法略显麻烦了,看到了一个新的方法,就是若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):
- #include <cstdio>
- #include <cstring>
- const int MAXN = *;
- const int MAXM = MAXN * MAXN * ;
- struct TwoSAT{
- int St[MAXN], c;
- int n, ecnt, dfs_clock, scc_cnt;
- int head[MAXN], sccno[MAXN], pre[MAXN], lowlink[MAXN];
- int next[MAXM], to[MAXM];
- void dfs(int u){
- lowlink[u] = pre[u] = ++dfs_clock;
- St[++c] = u;
- for(int p = head[u]; p; p = next[p]){
- int &v = to[p];
- if(!pre[v]){
- dfs(v);
- if(lowlink[u] > lowlink[v]) lowlink[u] = lowlink[v];
- }else if(!sccno[v]){
- if(lowlink[u] > pre[v]) lowlink[u] = pre[v];
- }
- }
- if(lowlink[u] == pre[u]){
- ++scc_cnt;
- while(true){
- int x = St[c--];
- sccno[x] = scc_cnt;
- if(x == u) break;
- }
- }
- }
- void init(int nn){
- n = nn;
- ecnt = ; dfs_clock = scc_cnt = ;
- memset(head,,sizeof(head));
- memset(pre,,sizeof(pre));
- memset(sccno,,sizeof(sccno));
- }
- void addEdge(int x, int y){//x, y clash
- to[ecnt] = y^; next[ecnt] = head[x]; head[x] = ecnt++;
- to[ecnt] = x^; next[ecnt] = head[y]; head[y] = ecnt++;
- //printf("%d<>%d\n",x,y);
- }
- bool solve(){
- for(int i = ; i < n; ++i)
- if(!pre[i]) dfs(i);
- for(int i = ; i < n; i += )
- if(sccno[i] == sccno[i^]) return false;
- return true;
- }
- } G;
- const int MAXNN = ;
- int a1[MAXNN], b1[MAXNN], a2[MAXNN], b2[MAXNN], a3[MAXNN], b3[MAXNN], c[MAXNN];
- inline bool clash(int beg1, int end1, int beg2, int end2){
- if(end1 <= beg2 || end2 <= beg1) return false;
- return true;
- }
- int main(){
- int n;
- while(scanf("%d", &n)!=EOF){
- G.init(n*);
- for(int i = ; i < n; ++i) {
- scanf("%d:%d %d:%d %d", &a1[i], &a2[i], &b1[i], &b2[i], &c[i]);
- a3[i] = a1[i] * + a2[i];
- b3[i] = b1[i] * + b2[i];
- }
- for(int i = ; i < n; ++i) for(int j = i+; j < n; ++j) if(i != j){
- if(clash(a3[i], a3[i] + c[i], a3[j], a3[j] + c[j])) G.addEdge(i*, j*);
- if(clash(a3[i], a3[i] + c[i], b3[j] - c[j], b3[j])) G.addEdge(i*, j*+);
- if(clash(b3[i] - c[i], b3[i], a3[j], a3[j] + c[j])) G.addEdge(i*+, j*);
- if(clash(b3[i] - c[i], b3[i], b3[j] - c[j], b3[j])) G.addEdge(i*+, j*+);
- }
- if(G.solve()) printf("YES\n");
- else {printf("NO\n"); continue;}
- for(int i = ; i < n; ++i){
- if(G.sccno[i * ] < G.sccno[i * + ]) {
- b1[i] = a1[i]; b2[i] = a2[i] + c[i];
- while(b2[i] >= ) ++b1[i], b2[i] -= ;
- } else {
- a1[i] = b1[i]; a2[i] = b2[i] - c[i];
- while(a2[i] < ) --a1[i], a2[i] += ;
- }
- printf("%02d:%02d %02d:%02d\n", a1[i], a2[i], b1[i], b2[i]);
- }
- }
- return ;
- }
POJ 3683 Priest John's Busiest Day(2-SAT 并输出解)的更多相关文章
- POJ 3683 Priest John's Busiest Day(2-SAT+方案输出)
Priest John's Busiest Day Time Limit: 2000MS Memory Limit: 65536K Total Submissions: 10010 Accep ...
- POJ 3683 Priest John's Busiest Day / OpenJ_Bailian 3788 Priest John's Busiest Day(2-sat问题)
POJ 3683 Priest John's Busiest Day / OpenJ_Bailian 3788 Priest John's Busiest Day(2-sat问题) Descripti ...
- POJ 3683 Priest John's Busiest Day (2-SAT)
Priest John's Busiest Day Time Limit: 2000MS Memory Limit: 65536K Total Submissions: 6900 Accept ...
- poj - 3683 - Priest John's Busiest Day(2-SAT)
题意:有N场婚礼,每场婚礼的开始时间为Si,结束时间为Ti,每场婚礼有个仪式,历时Di,这个仪式要么在Si时刻开始,要么在Ti-Di时刻开始,问能否安排每场婚礼举行仪式的时间,使主持人John能参加所 ...
- POJ 3683 Priest John's Busiest Day (2-SAT)
题意:有n对新人要在同一天结婚.结婚时间为Ti到Di,这里有时长为Si的一个仪式需要神父出席.神父可以在Ti-(Ti+Si)这段时间出席也可以在(Di-Si)-Si这段时间.问神父能否出席所有仪式,如 ...
- POJ 3683 Priest John's Busiest Day (2-SAT,常规)
题意: 一些人要在同一天进行婚礼,但是牧师只有1个,每一对夫妻都有一个时间范围[s , e]可供牧师选择,且起码要m分钟才主持完毕,但是要么就在 s 就开始,要么就主持到刚好 e 结束.因为人数太多了 ...
- POJ 3683 Priest John's Busiest Day
2-SAT简单题,判断一下两个开区间是否相交 #include<cstdio> #include<cstring> #include<cmath> #include ...
- POJ 3683 Priest John's Busiest Day[2-SAT 构造解]
题意: $n$对$couple$举行仪式,有两个时间段可以选择,问是否可以不冲突举行完,并求方案 两个时间段选择对应一真一假,对于有时间段冲突冲突的两人按照$2-SAT$的规则连边(把不冲突的时间段连 ...
- POJ 3683 Priest John's Busiest Day 【2-Sat】
这是一道裸的2-Sat,只要考虑矛盾条件的判断就好了. 矛盾判断: 对于婚礼现场 x 和 y,x 的第一段可以和 y 的第一段或者第二段矛盾,同理,x 的第二段可以和 y 的第一段或者第二段矛盾,条件 ...
随机推荐
- 20145330《Java程序设计》第四次实验报告
20145330<Java程序设计>第四次实验报告 实验四 Android环境搭建 实验内容 1.搭建Android环境 2.运行Android 3.修改代码,能输出学号 实验步骤 搭建A ...
- nmap的script参数列表
在新的nmap版本中,添加了script功能的使用.在nmap的安装目录的share/nmap/scripts中,已经有将61个写好的脚本提供. 具体的用法可以参考:http://nmap.org/b ...
- ios修改产品名
在创建项目的时候,会设置一个项目名,以后生成的APP名字也就是这个了,但由于某种原因,我想修改APP名字,也就是屏幕程序图标下面显示的那个,这该怎么办呢? 下面有三种方法都可以: 修改Product ...
- NodeJS学习笔记之Connect中间件模块(一)
NodeJS学习笔记之Connect中间件模块(一) http://www.jb51.net/article/60430.htm NodeJS学习笔记之Connect中间件模块(二) http://w ...
- WebApp之Meta标签
<meta name="apple-touch-fullscreen" content="yes">"添加到主屏幕“后,全屏显示 < ...
- cookie的设置、获取和删除封装
在我们为了去完成数据储存,有时会用到cookie,简单封装一下cookie: <!DOCTYPE html> <html lang="en"> <he ...
- Android-----工程文件目录介绍
- Jquery,YUI这个著名js库名称作用的理解
看廖雪峰大神的教程,其中讲到变量作用域问题.在命名空间中,写到:因为全局变量绑到了window上,不同的js文件访问相同全局变量或者定义了相同名字的顶层函数,都会造成命名冲突,并且很难被发现. 减少冲 ...
- Vim 常见操作
1.复制所有内容 按照此顺序敲即可:gg(光标定位到文件头) V(选中该行) G(选中该行开始到最后一行结尾) y(复制选中内容) 2.粘贴所有内容 正常模式下,敲p即可,如果遇到粘贴内容不全,需要 ...
- NeuSoft(2)添加系统调用
1.下载内核 apt-get install linux-source 在/usr/src下 2.解压内核 cd /usr/src tar -jxvf linux-source-3.2.0.tar.b ...