POJ 3684 Priest John's Busiest Day 2-SAT+输出路径
强连通算法推断是否满足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's Busiest Day 2-SAT+输出路径的更多相关文章
- POJ 3683 Priest John's Busiest Day (2-SAT+输出可行解)
题目地址:POJ 3683 第一次做须要输出可行解的题目. . .大体思路是先用强连通来推断是否有可行解,然后用逆序建图.用拓扑排序来进行染色.然后输出可行解. 详细思路见传送门 由于推断的时候少写了 ...
- HDU2491 Priest John's Busiest Day
题目链接 题意: 有n个人要进行乒乓球比赛,每一个人都一个能力值.每一个人出现的次序就是他们住的位置 如今要求进行一场比赛,三个人,裁判的能力值在两个选手之间,住的位置也在两个人的之间 问这样的比赛一 ...
- 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 3686 Priest John's Busiest Day
http://poj.org/problem?id=3683 2-sat 问题判定,输出一组可行解 http://www.cnblogs.com/TheRoadToTheGold/p/8436948. ...
- 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+方案输出)
Priest John's Busiest Day Time Limit: 2000MS Memory Limit: 65536K Total Submissions: 10010 Accep ...
- 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这段时间.问神父能否出席所有仪式,如 ...
随机推荐
- Windows查看进程taskList,终止进程tskill
TaskList: 列出当前所有运行进程. 使用方法:在命令提示符中输入tasklist 然后回车,会看到类似下面的列表: 映像名称 ...
- Windows7WithSP1/TeamFoundationServer2012update4/SQLServer2012
[Info @09:03:33.737] ====================================================================[Info @ ...
- [Cacti] memcache安装执行、cacti监控memcache实战
简单介绍 Memcache是danga.com的一个项目,最早是为 LiveJournal 服务的.眼下全世界不少人使用这个缓存项目来构建自己大负载的站点,来分担数据库的压力. Memcache官方站 ...
- JAVA的extends使用方法
理解继承是理解面向对象程序设计的关键.在Java中,通过keywordextends继承一个已有的类,被继承的类称为父类(超类,基类),新的类称为子类(派生类).在Java中不同意多继承. (1)继承 ...
- 解决xShell4某些情况下按删除键会输出^H的问题
当我们用Xshell登录进入linux后,在普通模式下,对输入进行删除等操作没有问题. 而在执行中,按delete,backspace键时会产生^H等乱码问题. 这是由于编码不匹配的问题. 解决方法: ...
- sqlplus登录、连接命令
经常使用: sqlplus username/password 如:普通用户登录 sqlplus scott/tiger sqlplus username/password@net_service ...
- projecteuler---->problem=9----Special Pythagorean triplet
title: A Pythagorean triplet is a set of three natural numbers, a b c, for which, a2 + b2 = c2 For e ...
- python语言学习5——输入和输出
输出 用print()在括号中加上字符串,就可以向屏幕上输出指定的文字. 注意点: 字符串用的是单引号 碰到逗号输出时就会输出一个空格 输入 python提供了一个input(),可以让用户输入一个字 ...
- liunx清理磁盘du -h --max-depth=1 /data/*
liunx清理磁盘du -h --max-depth=1 /data/*
- sql server2012附加的数据库问题
watermark/2/text/aHR0cDovL2Jsb2cuY3Nkbi5uZXQvdTAxMjM2NzUxMw==/font/5a6L5L2T/fontsize/400/fill/I0JBQk ...