[2-sat]POJ 3683——Priest John's Busiest Day
来源:互联网 发布:mac xcode 玩游戏 编辑:程序博客网 时间:2024/06/05 06:58
题目传送门
题目概述
安排n个婚礼是这n个婚礼没有时间相交。
每个婚礼有2个安排时间段。
解题思路
不难看出这题是一道求2-sat任意解问题。
如果时间段
如果看不懂上面这句话点这里
接下来用tarjan缩点是毋庸置疑的,因为如果你选择了
接下来就是如何求答案的问题。
网上有介绍刷拓扑然后染色。
但是Manchery dalao的blog中介绍,完全不需要以上两种操作。
什么!!!
整个人顿时凌乱了,稍微思考一下发现染色听着高端其实是一个十分鸡肋的过程,但是对于用tarjan求出的scc的顺序代替拓扑序迟迟不能理解,非常混乱。
这时Manchery dalao过来叫我冷静一下,画个图看下。
以下过程读者脑补吧(我记不得了QAQ),再稍微用逻辑关系推一下就可以发现这个神奇的操作。
这样代码的实现难度减少了很多,跪烂Manchery dalao
#include<cstdio>#include<algorithm>using namespace std;const int maxn=2005,maxm=2000005;struct jz{ int s,t;}a[maxn];int n,tot,lnk[maxn],son[maxm],nxt[maxm],ans[maxn];int dfn[maxn],low[maxn],scc[maxn],top,s[maxn],time,G;bool ins[maxn];inline int _read(){ int num=0;char ch=getchar(); while (ch<'0'||ch>'9') ch=getchar(); while (ch>='0'&&ch<='9') num=num*10+ch-48,ch=getchar(); return num;}void add(int x,int y){ nxt[++tot]=lnk[x];lnk[x]=tot;son[tot]=y;}bool check(int x,int y){ if (a[y].s<a[x].s) swap(y,x); return a[y].s<a[x].t;}void tarjan(int x){ dfn[x]=++time;low[x]=time; s[++top]=x;ins[x]=1; for (int j=lnk[x];j;j=nxt[j]) if (!dfn[son[j]]){tarjan(son[j]);low[x]=min(low[x],low[son[j]]);} else if (ins[son[j]]) low[x]=min(low[x],dfn[son[j]]); if (dfn[x]==low[x]){ G++; while(s[top]!=x) scc[s[top]]=G,ins[s[top--]]=0; scc[x]=G;ins[s[top--]]=0; }}int main(){ freopen("exam.in","r",stdin); freopen("exam.out","w",stdout); n=_read(); for (int i=0;i<n;i++){ int h1=_read(),m1=_read(),h2=_read(),m2=_read(),d=_read(); int s=h1*60+m1,t=h2*60+m2; a[2*i].s=s;a[2*i].t=s+d; a[2*i^1].s=t-d;a[2*i^1].t=t; } for (int i=0;i<2*n;i++) for (int j=0;j<i;j++) if ((j/2!=i/2)&&check(i,j)) add(i,j^1),add(j,i^1); for (int i=0;i<2*n;i++) if (!dfn[i]) tarjan(i); for (int i=0;i<n;i++) if (scc[2*i]==scc[2*i^1]){printf("NO\n");return 0;} else if (scc[2*i]<scc[2*i^1]) ans[i]=2*i;else ans[i]=2*i^1; printf("YES\n"); for (int i=0;i<n;i++) printf("%02d:%02d %02d:%02d\n",a[ans[i]].s/60,a[ans[i]].s%60,a[ans[i]].t/60,a[ans[i]].t%60); return 0;}
阅读全文
0 0
- POJ 3683 Priest John's Busiest Day(2-SAT)
- poj 3683 Priest John's Busiest Day 2-sat
- poj 3683 Priest John's Busiest Day 2-SAT
- 2-sat->poj 3683 Priest John's Busiest Day
- poj - 3683 - Priest John's Busiest Day(2-SAT)
- 【POJ】3683 Priest John's Busiest Day 2-sat
- POJ 3683 Priest John's Busiest Day(2-sat)
- poj 3683 Priest John's Busiest Day 2-sat
- poj 3683 Priest John's Busiest Day 2-sat
- POJ 3683 - Priest John's Busiest Day(2-SAT)
- poj 3683 Priest John's Busiest Day(2-SAT)
- poj 3683 Priest John's Busiest Day 2-SAT
- POJ 3683 Priest John's Busiest Day(2-SAT)
- POJ - 3683 Priest John's Busiest Day(2-SAT)
- poj 3683 Priest John's Busiest Day 2-SAT输出
- Priest John's Busiest Day poj 3683 tarjan+2-SAT
- POJ 3683 Priest John's Busiest Day (2-SAT)
- |poj 3683|2-SAT|Priest John's Busiest Day
- 动态规划经典题目:最大连续子序列和
- 【差分约束系统】POJ 1201--Intervals
- javaWeb编程_Servlet01
- 极客公园 Mac 第三方客户端(swift)
- leetcode: 8. String to Integer (atoi)
- [2-sat]POJ 3683——Priest John's Busiest Day
- EBS-通用导入
- CSDN日报20170704——《和代码的青春七年》
- forward和redirect的区别联系
- Linux学习笔记-yum源安装
- MySQL(一)
- Servlet的一些学习笔记
- 利用RNN做脑电信号的分类(一)
- java解析xml文档