强连通算法推断是否满足2-sat,然后反向建图,拓扑排序+染色。

一种选择是从 起点開始,还有一种是终点-持续时间那个点 開始。

若2个婚礼的某2种时间线段相交,则有矛盾,建边。

easy出错的地方就在于推断线段相交。

若s1<e2&&s2<e1则相交

输出路径的做法能够參考论文2-SAT解法浅析

#include <iostream>
#include<cstring>
#include<cstdio>
#include<string>
#include<vector>
#include<algorithm>
#include<queue>
using namespace std;
#define MAXN 5555
#define MAXM 3000010
struct node
{
int to,next;
}edge[MAXM];
int head[MAXN],en;
int low[MAXN],dfn[MAXN],stack[MAXN],cho[MAXN],top,set[MAXN],col,num,color[MAXN],counts[MAXN];
bool vis[MAXN],instack[MAXN];
int n;
int m;
void addedge(int a,int b)
{
edge[en].to=b;
edge[en].next=head[a];
head[a]=en++;
}
int son[MAXN];
void tarjan(int u)
{ vis[u]=1;
dfn[u]=low[u]=++num;
instack[u]=true;
stack[++top]=u;
for(int i=head[u];i!=-1;i=edge[i].next)
{
int v=edge[i].to;
if(!vis[v])
{
tarjan(v);
low[u]=min(low[u],low[v]);
}
else
if(instack[v])
low[u]=min(dfn[v],low[u]);
}
if(dfn[u]==low[u])
{
int j;
col++;
do
{
j=stack[top--];
instack[j]=false;
set[j]=col;
}
while (j!=u);
}
}
void init()
{
en=top=col=num=0;
memset(head,-1,sizeof(head));
memset(instack,0,sizeof(instack));
memset(vis,0,sizeof(vis));
memset(set,-1,sizeof(set));
memset(color,0,sizeof(color));
memset(counts,0,sizeof(counts));
memset(cho,0,sizeof(cho));
}
struct node2
{
int st,ed,la;
}p[2005];
vector<int> g[2005];
void topsort()
{
queue<int> q;
for(int i=1;i<=col;i++)
{
if(counts[i]==0)
q.push(i);
}
while(!q.empty())
{
int j=q.front();
if(!color[j]) color[j]=1,color[son[j]]=-1;
q.pop();
for(int k=0;k<g[j].size();k++)
{
if(--counts[g[j][k]]==0)
q.push(g[j][k]);
}
}
}
void print(int t1,int t2)
{ printf("%02d:%02d",t1/60,t1%60);
printf(" %02d:%02d\n",t2/60,t2%60);
}
int main()
{
int a,b,c,d,e;
while(~scanf("%d",&n))
{
init();
for(int i=1;i<=n;i++)
{
scanf("%d:%d%d:%d%d",&a,&b,&c,&d,&e);
p[i].st=a*60+b;
p[i].ed=c*60+d;
p[i].la=e;
}
for(int i=1;i<=n;i++)
{
for(int j=1;j<=n;j++)
{
if(i==j) continue;
if(p[i].st<p[j].st+p[j].la && p[i].st+p[i].la>p[j].st) {addedge(i,j+n);}
if(p[i].st<p[j].ed && p[i].st+p[i].la>p[j].ed-p[j].la) {addedge(i,j);} if(p[i].ed-p[i].la<p[j].st+p[j].la && p[i].ed>p[j].st) {addedge(i+n,j+n);}
if(p[i].ed-p[i].la<p[j].ed && p[i].ed>p[j].ed-p[j].la) {addedge(i+n,j);}
}
}
for(int i=1;i<=n*2;i++)
if(!vis[i])tarjan(i);
int ok=1;
for(int i=1;i<=n;i++)
{
if(set[i]==set[i+n]) {ok=0;break;}
son[set[i]]=set[i+n];
son[set[i+n]]=set[i];
}
if(ok==0) {puts("NO");continue;}
else puts("YES");
for(int i=1;i<=col;i++) g[i].clear();
for(int i=1;i<=2*n;i++)
for(int j=head[i];~j;j=edge[j].next)
if(set[i]!=set[edge[j].to])
{
g[set[edge[j].to]].push_back(set[i]);
counts[set[i]]++;
}
topsort(); for(int i=1;i<=2*n;i++)
{
if(color[set[i]]==1) cho[i]=1;
}
for(int i=1;i<=n;i++)
{
if(cho[i]) print(p[i].st,p[i].st+p[i].la);
else print(p[i].ed-p[i].la,p[i].ed);
}
}
return 0;
}
/*
3
08:00 09:00 30
08:15 09:00 20
08:00 09:00 30
*/

POJ 3684 Priest John&#39;s Busiest Day 2-SAT+输出路径的更多相关文章

  1. POJ 3683 Priest John&#39;s Busiest Day (2-SAT+输出可行解)

    题目地址:POJ 3683 第一次做须要输出可行解的题目. . .大体思路是先用强连通来推断是否有可行解,然后用逆序建图.用拓扑排序来进行染色.然后输出可行解. 详细思路见传送门 由于推断的时候少写了 ...

  2. HDU2491 Priest John&#39;s Busiest Day

    题目链接 题意: 有n个人要进行乒乓球比赛,每一个人都一个能力值.每一个人出现的次序就是他们住的位置 如今要求进行一场比赛,三个人,裁判的能力值在两个选手之间,住的位置也在两个人的之间 问这样的比赛一 ...

  3. 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 ...

  4. poj 3686 Priest John's Busiest Day

    http://poj.org/problem?id=3683 2-sat 问题判定,输出一组可行解 http://www.cnblogs.com/TheRoadToTheGold/p/8436948. ...

  5. POJ 3683 Priest John's Busiest Day (2-SAT)

    Priest John's Busiest Day Time Limit: 2000MS   Memory Limit: 65536K Total Submissions: 6900   Accept ...

  6. POJ 3683 Priest John's Busiest Day(2-SAT+方案输出)

    Priest John's Busiest Day Time Limit: 2000MS   Memory Limit: 65536K Total Submissions: 10010   Accep ...

  7. 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 ...

  8. poj - 3683 - Priest John's Busiest Day(2-SAT)

    题意:有N场婚礼,每场婚礼的开始时间为Si,结束时间为Ti,每场婚礼有个仪式,历时Di,这个仪式要么在Si时刻开始,要么在Ti-Di时刻开始,问能否安排每场婚礼举行仪式的时间,使主持人John能参加所 ...

  9. POJ 3683 Priest John's Busiest Day (2-SAT)

    题意:有n对新人要在同一天结婚.结婚时间为Ti到Di,这里有时长为Si的一个仪式需要神父出席.神父可以在Ti-(Ti+Si)这段时间出席也可以在(Di-Si)-Si这段时间.问神父能否出席所有仪式,如 ...

随机推荐

  1. ANDROID L——Material Design综合应用(Demo)

    转载请注明本文出自大苞米的博客(http://blog.csdn.net/a396901990),谢谢支持! Material Design: Material Design是Google推出的一个全 ...

  2. tar.gz文件命名和压缩解压方法

    tar.gz文件命名 tar这是文件打成一个包,无压缩; gz同gzip标记的包.tar文件压缩; 所以它成为一个.tar.gz档 压缩 # tar cvfz backup.tar.gz /xxx/ ...

  3. System单元对所有与COM相关的声明就这么多,需要倒背如流

    是首先是VM表,但是和COM相关的函数地址都废弃了,这几个VM函数具体放在哪里,还得在研究: { Virtual method table entries } vmtSelfPtr = -; vmtI ...

  4. LeetCode My Solution: Minimum Depth of Binary Tree

    Minimum Depth of Binary Tree Total Accepted: 24760 Total Submissions: 83665My Submissions Given a bi ...

  5. CheckBox和RadioButton以及RadioGroup

    CheckBox:复选框 有两种状态 选中状态(true),未选状态(false) 属性 android:checked= "false"(表示该复选框未被选中) RadioGro ...

  6. BZOJ 3112 Zjoi2013 防守战线 单纯形

    题目大意: 单纯形*2.. . #include <cmath> #include <cstdio> #include <cstring> #include < ...

  7. 杭州电ACM1098——Ignatius&#39;s puzzle

    这个话题.简单的数学. 对于函数,f(x)=5*x^13+13*x^5+k*a*x,输入k,对于休闲x,一个数字的存在a,使f(x)是65可分. 对于休闲x. 因此,当x = 1时间,f(x) = 1 ...

  8. 向GridView的模板列绑定OnClientClick的函数时出现了奇怪的问题

    原文:向GridView的模板列绑定OnClientClick的函数时出现了奇怪的问题 GridView的一个模板列中的内容是按钮,需要实现以下的效果: GridView分页显示数据,点击编辑按钮(模 ...

  9. 服务器编程入门(3)TCP协议详解

    问题聚焦:     本节从如下四个方面讨论TCP协议:     TCP头部信息:指定通信的源端端口号.目的端端口号.管理TCP连接,控制两个方向的数据流     TCP状态转移过程:TCP连接的任意一 ...

  10. 高版本jQuery设置checkbox状态注意事项

    jQuery 1.9 以后, 使用 .attr(“checked”, true) 或  attr(“checked”, “checked”) 将无法正确设置 checkbox的状态, 同样的, 使用 ...