POJ 3683 Priest John's Busiest Day (2-SAT,常规)
题意:
一些人要在同一天进行婚礼,但是牧师只有1个,每一对夫妻都有一个时间范围[s , e]可供牧师选择,且起码要m分钟才主持完毕,但是要么就在 s 就开始,要么就主持到刚好 e 结束。因为人数太多了,这些时间可能会重叠,可能还会完全包含,可能还没有交叉,各种情况。问牧师能否主持完全部人的婚礼,若可以,给出每对夫妻占用牧师的一个时间段(记得按所给的夫妻的顺序哦)。
主要步骤如下。
(1)先建原图,不管是否会冲突。
(2)找强连通分量来缩点,如果会冲突,这个时候应该发现。
(3)建个缩点后,原图的反向图。
(4)着色法标记所要输出的时间段。
(5)时间段按照所给每对夫妻的顺序来输出。
实现:
(1)婚礼要么在s就开始,要么在e刚好结束,还有可能这对夫妻就要求从s-e都要主持呢。 先把时间给转换成分钟,每对夫妻拥有2个时间段。
(2)接下来主要是建图,建图时只考虑“冲突”,即如果i*2和j*2冲突了,那么选i*2就必须选j*2+1。先不用顾着i*2和j*2+1是否冲突,只要将4种可能的组合列出来,建图,其他的交给强连通分量去做。
(3)强连通分量承接第3步的重任,在求强连通分量时要顺便判断是否会冲突,当然这比较简单。
(4)建反向图只是穷举原图中的每条边,判断是否在同个scc中,如果不是就可以建边了,将边反过来这么简单。
(5)着色时还得将另外冲突的“全部“着其他颜色,那么根据已建好的反向图一直DFS下去,如果已经着色,可以退出了,别每次都深搜到底了,没必要。
(6)时间段还得按所给夫妻顺序!那得再稍微处理一下。
- #include <iostream>
- #include <stdio.h>
- #include <string.h>
- #include <vector>
- #include <stack>
- #include <algorithm>
- #define LL long long
- #define pii pair<int,int>
- #define INF 0x7f7f7f7f
- using namespace std;
- const int N=+;
- int t1[N], t2[N], t3[N], t4[N]; //保存时间点
- bool vis[N];
- vector<int> vect[N*], rg[N*], scc[N*]; //分别是:原图,反向图,
- stack<int> stac; //强连通分量必备
- int lowlink[N*], dfn[N*], scc_no[N*], scc_cnt, dfn_clock; //强连通分量必备
- int col[N*]; //用于着色
- bool dele[N*]; //标记输出点
- inline int to_time(int h, int m){return h*+m;}
- inline bool conflict(int a,int b, int c, int d){if(b<=c || d<=a) return true;return false;}
- void get_graph(int n) //难在建图吧?
- {
- for(int i=; i<n*; i++) vect[i].clear();
- for(int i=; i<n; i++)
- {
- for(int j=; j<n; j++) //考虑放在前面
- {
- if(i==j) continue;
- if(!conflict(t1[i],t2[i], t1[j],t2[j])) vect[i*].push_back(j*+);
- if(!conflict(t1[i],t2[i], t3[j],t4[j])) vect[i*].push_back(j*);
- if(!conflict(t3[i],t4[i], t1[j],t2[j])) vect[i*+].push_back(j*+);
- if(!conflict(t3[i],t4[i], t3[j],t4[j])) vect[i*+].push_back(j*);
- }
- }
- }
- void DFS(int x) //模板tarjan
- {
- stac.push(x);
- dfn[x]=lowlink[x]=++dfn_clock;
- for(int i=; i<vect[x].size(); i++)
- {
- int t=vect[x][i];
- if(!dfn[t])
- {
- DFS(t);
- lowlink[x]=min(lowlink[x],lowlink[t]);
- }
- else if(!scc_no[t]) lowlink[x]=min(lowlink[x],dfn[t]);
- }
- if(lowlink[x]==dfn[x])
- {
- scc[++scc_cnt].clear();
- while(true)
- {
- int t=stac.top();stac.pop();
- scc_no[t]=scc_cnt;
- scc[scc_cnt].push_back(t);
- if(t==x) break;
- }
- }
- }
- int find_scc(int n) //求强连通分量,顺便检查是否满足条件
- {
- scc_cnt=dfn_clock=;
- memset(lowlink,,sizeof(lowlink));
- memset(dfn,,sizeof(dfn));
- memset(scc_no,,sizeof(scc_no));
- for(int i=; i<n; i++) if(!dfn[i]) DFS(i);
- for(int i=; i<n; i+=) if(scc_no[i]==scc_no[i+]) return false; //检查是否冲突
- return true;
- }
- void build_rg(int n) //建反向图,为了要反向着色
- {
- for(int i=; i<n; i++) rg[i].clear();
- for(int i=; i<n; i++)
- {
- for(int j=; j<vect[i].size(); j++)
- {
- int t=vect[i][j];
- if(scc_no[i]!=scc_no[t]) //不属于同个强连通分量
- rg[scc_no[t]].push_back(scc_no[i]);
- }
- }
- }
- void del(int s) //删除的是反向边,之前已建好的rg图
- {
- while(!col[s])
- {
- col[s]=;
- for(int i=; i<rg[s].size(); i++) del(rg[s][i]); //递归“删除”,按反向边的方向(即着其他颜色)
- }
- }
- void color()
- {
- memset(col,,sizeof(col));
- for(int i=; i<=scc_cnt; i++) //没有按照拓扑顺序也能AC???
- if(!col[i])
- {
- col[i]=;
- del( scc_no[ scc[i][]%==?scc[i][]+:scc[i][]-]); //在第i个SCC中任取1点,取其对立点,再取其所在scc编号,进行着色
- }
- }
- void print(int n)
- {
- memset(dele,,sizeof(dele));
- puts("YES");
- for(int i=; i<scc_cnt; i++)
- {
- if(col[i]==) //col=2的都是要的,当然,选全部col=3也一样,因为对称
- for(int j=; j<scc[i].size(); j++)
- dele[scc[i][j] ]=true; //把所有要输出的点先标记出来
- }
- for(int i=; i<n; i++)
- {
- if(dele[i])
- if(i%==) //取前段
- {
- int a=t1[i/]/;
- int b=t1[i/]%;
- int c=t2[i/]/;
- int d=t2[i/]%;
- printf("%02d:%02d %02d:%02d\n",a,b,c,d);
- }
- else //取后段
- {
- int a=t3[i/]/;
- int b=t3[i/]%;
- int c=t4[i/]/;
- int d=t4[i/]%;
- printf("%02d:%02d %02d:%02d\n",a,b,c,d);
- }
- }
- }
- int main()
- {
- freopen("input.txt", "r", stdin);
- int n, a, b, c, d, e, f, g;
- while(~scanf("%d",&n))
- {
- memset(t1,,sizeof(t1));
- memset(t2,,sizeof(t2));
- memset(t3,,sizeof(t3));
- memset(t4,,sizeof(t4));
- for(int i=; i<n; i++)
- {
- scanf("%d %c %d %d %c %d %d",&a,&b,&c, &d,&e,&f, &g);
- t1[i]=to_time(a,c); //时间转成分钟,分别有2种,t1t2表示在前的start和end,t3t4表示在后
- t2[i]=to_time(a,c)+g;
- t3[i]=to_time(d,f)-g;
- t4[i]=to_time(d,f);
- }
- get_graph(n);
- if(!find_scc(n<<)) {puts("NO");continue;}
- build_rg(n*); //按缩点建新的反向图reverse_graph
- color(); //着色完,所需要的点也就出结果了
- print(n*);
- }
- return ;
- }
AC代码
POJ 3683 Priest John's Busiest Day (2-SAT,常规)的更多相关文章
- 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: 10010 Accep ...
- 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 并输出解)
Description John is the only priest in his town. September 1st is the John's busiest day in a year b ...
- 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简单题,判断一下两个开区间是否相交 #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 的第一段或者第二段矛盾,条件 ...
随机推荐
- Perl中的正则表达
前几天用到了Perl语言,主要看了一下Perl中的正则表达式,在各种网页语言中,正则表达式在处理字符串的时候十分有用,所以这里就简单说一下在Perl中正则表达式的应用. 先上代码 #!/usr/bin ...
- dom树的介绍,及原理分析
三.解析和DOM树的构建 1.解析: 由于解析渲染引擎是一个非常重要的过程,我们将会一步步的深入,现在让我们来介绍解析. 解析一个文档,意味着把它转换为一个有意义的结构——代码可以了解和使用的东西,解 ...
- The method of type must override a superclass method
导入android项目时,报The method of type must override asuperclass method 一堆错误, 解决方法: 将编译的jdk与使用的jdk版本一致即可.
- Kinetic使用注意点--ellipse
new Ellipse(config) 参数: config:包含所有配置项的对象. { radius: "半径,可以用数字a.数组[a,b]或对象{x:a,y:b}来表示" } ...
- gridView AspNetPager 翻页时 弹出窗体关闭报错
gridView AspNetPager 翻页后,你右击刷新或F5会发现弹出一个刷新页面. 这是因为默认翻页都是用dopostback方式回发的.因为这时的页面已经不是原来的页面.所以会弹出提示. 这 ...
- EXTJS4.2 控件之Grid 行点击事件
listeners: { 'itemclick': function (view, record, item, index, e) { //Ext.MessageBox.alert("标题& ...
- 滤镜简单demo(转,供参考)
NSURL *iamgeUrl = [NSURL fileURLWithPath:[[NSBundle mainBundle] pathForResource:@"default" ...
- ng-blur失去焦点执行事件
<label class="item item-input item-stacked-label"> <span class="input-label& ...
- 团体程序设计天梯赛-练习集L1-011. A-B
L1-011. A-B 时间限制 100 ms 内存限制 65536 kB 代码长度限制 8000 B 判题程序 Standard 作者 陈越 本题要求你计算A-B.不过麻烦的是,A和B都是字符串 — ...
- uva 1103
弄懂题意后 其实就是一个dfs /************************************************************************* > Aut ...