Poj 3683-Priest John's Busiest Day 2-sat,拓扑排序
Priest John's Busiest Day
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). 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 Sample Output YES Source POJ Founder Monthly Contest – 2008.08.31, Dagger and Facer
|
题意:有n对人要结婚,牧师分别去主持每个婚礼中的仪式,给出每对新人的开始和结束时间,还有仪式需要的时间,仪式必须要在开始或结束的时间举办,问是否能给出一种方案,使得牧师可以参加所有仪式。
题解:
2-sat。。。
终于把这道题A了。。。
首先,先把每个婚礼的每一段开始时间标记为i*2,结束时间标记为i*2-1。然后缩点,逆向用强联通分量建图。然后用拓扑排序做一遍,输出即可。
安利一个2-sat讲解:2-sat总结
#include<bits/stdc++.h>
using namespace std;
#define MAXN 2010
struct node
{
int begin,end,next;
}edge[*MAXN*MAXN];
struct NODE
{
int begin,end,next;
}edge1[*MAXN*MAXN];
int cnt,Head[MAXN],cnt1,Head1[MAXN],n,s1[MAXN],t1[MAXN],LOW[MAXN],DFN[MAXN],STACK[MAXN],color[MAXN],q[MAXN],op[MAXN],belong[MAXN],SCC,Id[MAXN],SIZE,top,N;
bool INSTACK[MAXN];
void addedge(int bb,int ee)
{
edge[++cnt].begin=bb;edge[cnt].end=ee;edge[cnt].next=Head[bb];Head[bb]=cnt;
}
void addedge1(int bb,int ee)
{
edge1[++cnt1].begin=bb;edge1[cnt1].end=ee;edge1[cnt1].next=Head1[bb];Head1[bb]=cnt1;
}
int read()
{
int s=,fh=;char ch=getchar();
while(ch<''||ch>''){if(ch=='-')fh=-;ch=getchar();}
while(ch>=''&&ch<=''){s=s*+(ch-'');ch=getchar();}
return s*fh;
}
int Judge(int ii,int jj)
{
if(s1[jj]>=t1[ii]||s1[ii]>=t1[jj])return ;
return ;
}
void Build()
{
int i,j;
memset(Head,-,sizeof(Head));cnt=;
for(i=;i<=n;i++)
{
for(j=i+;j<=n;j++)
{
if(Judge(*i,*j)==)
{
addedge(*i,*j-);
addedge(*j,*i-);
}
if(Judge(*i,*j-)==)
{
addedge(*i,*j);
addedge(*j-,*i-);
}
if(Judge(*i-,*j)==)
{
addedge(*i-,*j-);
addedge(*j,*i);
}
if(Judge(*i-,*j-)==)
{
addedge(*i-,*j);
addedge(*j-,*i);
}
}
}
}
void Tarjan(int u)
{
int i,v;
LOW[u]=DFN[u]=++SIZE;
STACK[top++]=u;INSTACK[u]=true;
for(i=Head[u];i!=-;i=edge[i].next)
{
v=edge[i].end;
if(!DFN[v])
{
Tarjan(v);
LOW[u]=min(LOW[u],LOW[v]);
}
else if(INSTACK[v]==true)LOW[u]=min(LOW[u],DFN[v]);
}
if(LOW[u]==DFN[u])
{
SCC++;
do
{
v=STACK[--top];
INSTACK[v]=false;
belong[v]=SCC;
}while(u!=v);
}
}
void solve()
{
SIZE=;
for(int i=;i<=N;i++)if(!DFN[i])Tarjan(i);
}
void dfs(int u)
{
if(color[u]!=)return;
color[u]=-;
for(int i=Head1[u];i!=-;i=edge1[i].next)dfs(edge1[i].end);
}
void Toposort()
{
int head=,tail=,i,u,v;
for(i=;i<=SCC;i++)if(Id[i]==)q[++tail]=i;
while(head<=tail)
{
u=q[++head];
if(color[u]!=)continue;//已经被标记过就不用搜索了.
color[u]=;/*若将color[]标记为,代表被标记为选择(即1).*/dfs(op[u]);/*将和u对立的点标记为选择u的对立点(即-1).*/
for(i=Head1[u];i!=-;i=edge1[i].next)
{
v=edge1[i].end;
Id[v]--;if(Id[v]==)q[++tail]=v;
}
}
}
void write(int k)
{
printf("%.2d:",k/);
printf("%.2d",k%);
}
int main()
{
int S1,S2,T1,T2,D,i;
n=read();
N=;
for(i=;i<=n;i++)
{
S1=read();S2=read();T1=read();T2=read();D=read();
s1[++N]=S1*+S2;t1[N]=s1[N]+D;
t1[++N]=T1*+T2;s1[N]=t1[N]-D;
}
Build();
solve();
for(i=;i<=n;i++)if(belong[*i-]==belong[*i]){printf("NO");return ;}
memset(Head1,-,sizeof(Head1));cnt1=;
for(i=;i<=cnt;i++)
{
if(belong[edge[i].begin]!=belong[edge[i].end]){addedge1(belong[edge[i].end],belong[edge[i].begin]);Id[belong[edge[i].begin]]++;}
}
memset(color,,sizeof(color));
for(i=;i<=n;i++)
{
op[belong[*i]]=belong[*i-];
op[belong[*i-]]=belong[*i];
}
Toposort();
printf("YES");
for(i=;i<=n;i++)
{
printf("\n");
if(color[belong[i*]]==){write(s1[i*]);printf(" ");write(t1[i*]);}
else {write(s1[i*-]);printf(" ");write(t1[i*-]);}
}
fclose(stdin);
fclose(stdout);
return ;
}
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,常规)
题意: 一些人要在同一天进行婚礼,但是牧师只有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 的第一段或者第二段矛盾,条件 ...
随机推荐
- linux相关解压命令
ZIP 我们可以使用下列的命令压缩一个目录: # zip -r archive_name.zip directory_to_compress 下面是如果解压一个zip文档: # unzip archi ...
- markdown与textile之间互相转换
markdown与textile之间互相转换 redmine中默认使用的是textile那么从别的地方复制过来的markdown格式的内容需要进行转换 找到一款工具叫做pandoc http://jo ...
- php文件锁解决少量并发问题
阻塞(等待)模式: <?php $fp = fopen("lock.txt", "r"); if(flock($fp,LOCK_EX)) { //..处理 ...
- yii2源码学习笔记(十七)
Theme 类,应用的主题,通过替换路径实现主题的应用,方法为获取根路径和根链接:yii2\base\Theme.php <?php /** * @link http://www.yiifram ...
- PHP全局变量
1.global 关键字 2.$GLOBALS 3.使用静态变量
- Python学习笔记——几种数据类型
1. 列表list: Python内置的一种数据类型是列表:list,用中括号[]表示.list是一种有序的集合,可以随时添加和删除其中的元素,而且元素的类型不必相同.list可以通过下标来访问,范围 ...
- WPF中将四个数字字符串值(比如:"10,10,300,300")转为Rect
RectConverter rectConverter = new RectConverter(); string parseString = viewportEntry.Text; if (pars ...
- 【web安全】第四弹:防火墙技术笔记
参考资料: <黑客攻防演习>第二版 Ed SKoudis Tom Liston著 <防火墙.入侵检测与VPN> 马春光 郭方方著 OSI在理论上将网络分为七层,物理层.数 ...
- 个人学习笔记--MyBatis官方推荐DAO开发方案
1.导入Jar包 2.编写全局配置文件configuration.xml <?xml version="1.0" encoding="UTF-8" ?&g ...
- [BZOJ 2738] 矩阵乘法 【分块】
题目链接:BZOJ - 2738 题目分析 题目名称 “矩阵乘法” 与题目内容没有任何关系..就像VFK的 A+B Problem 一样.. 题目大意是给定一个矩阵,有许多询问,每次询问一个子矩阵中的 ...