题目地址:POJ 3683

第一次做须要输出可行解的题目。

。大体思路是先用强连通来推断是否有可行解,然后用逆序建图。用拓扑排序来进行染色。然后输出可行解。

详细思路见传送门

由于推断的时候少写了一个等号。。检查了好长时间。。sad。。。

代码例如以下:

#include <iostream>
#include <cstdio>
#include <string>
#include <cstring>
#include <stdlib.h>
#include <math.h>
#include <ctype.h>
#include <queue>
#include <map>
#include <set>
#include <algorithm> using namespace std;
#define LL __int64
const int INF=0x3f3f3f3f;
int head[2100], cnt, index, top, ans;
int dfn[2100], low[2100], belong[2100], instack[2100], stak[2100];
struct N
{
int l, r;
} time[2100];
struct node
{
int u, v, next;
} edge[10000000];
void add(int u, int v)
{
edge[cnt].v=v;
edge[cnt].next=head[u];
head[u]=cnt++;
}
bool pan(N x, N y)
{
if((x.l<y.l&&x.r<=y.l)||(x.r>y.r&&x.l>=y.r))
return 1;
return 0;
}
void tarjan(int u)
{
dfn[u]=low[u]=++index;
instack[u]=1;
stak[++top]=u;
for(int i=head[u]; i!=-1; i=edge[i].next)
{
int v=edge[i].v;
if(!dfn[v])
{
tarjan(v);
low[u]=min(low[u],low[v]);
}
else if(instack[v])
{
low[u]=min(low[u],dfn[v]);
}
}
if(dfn[u]==low[u])
{
ans++;
while(1)
{
int v=stak[top--];
instack[v]=0;
belong[v]=ans;
if(u==v) break;
}
}
}
int head1[2100], cnt1, ct[2100], in[2100], color[2100];
struct node1
{
int u, v, next;
} edge1[10000000];
void add1(int u, int v)
{
edge1[cnt1].v=v;
edge1[cnt1].next=head1[u];
head1[u]=cnt1++;
}
void topo()
{
queue<int>q;
int i;
for(i=1; i<=ans;i++)
{
if(in[i]==0) q.push(i);
}
while(!q.empty())
{
int u=q.front();
q.pop();
if(!color[u])
{
color[u]=1;
color[ct[u]]=-1;
}
for(i=head1[u];i!=-1;i=edge1[i].next)
{
int v=edge[i].v;
in[v]--;
if(!in[v]) q.push(v);
}
}
}
void init()
{
memset(head,-1,sizeof(head));
cnt=top=ans=index=0;
memset(dfn,0,sizeof(dfn));
memset(instack,0,sizeof(instack));
memset(head1,-1,sizeof(head1));
memset(in,0,sizeof(in));
memset(color,0,sizeof(color));
cnt1=0;
}
int main()
{
int n, i, j, a1, b1, a2, b2, c;
scanf("%d",&n);
init();
for(i=0; i<n; i++)
{
scanf("%d:%d%d:%d%d",&a1,&b1,&a2,&b2,&c);
time[i<<1].l=a1*60+b1;
time[i<<1].r=a1*60+b1+c;
time[i<<1|1].l=a2*60+b2-c;
time[i<<1|1].r=a2*60+b2;
}
for(i=0; i<n<<1; i++)
{
for(j=0; j<i; j++)
{
if(!pan(time[i],time[j]))
{
add(i,j^1);
add(j,i^1);
//printf("%d %d\n",i,j);
}
}
}
for(i=0; i<n<<1; i++)
{
if(!dfn[i])
tarjan(i);
}
int flag=0;
for(i=0; i<n; i++)
{
if(belong[i<<1]==belong[i<<1|1])
{
flag=1;
break;
}
ct[belong[i<<1]]=belong[i<<1|1];
ct[belong[i<<1|1]]=belong[i<<1];
}
if(flag)
puts("NO");
else
{
puts("YES");
for(i=0;i<n<<1;i++)
{
for(j=head1[i];j!=-1;j=edge1[j].next)
{
int v=edge1[j].v;
if(belong[i]!=belong[v])
{
add1(belong[v],belong[i]);
in[belong[i]]++;
}
}
}
topo();
for(i=0;i<n;i++)
{
if(color[belong[i<<1]]==1)
{
printf("%02d:%02d %02d:%02d\n",time[i<<1].l/60,time[i<<1].l%60,time[i<<1].r/60,time[i<<1].r%60);
}
else
{
printf("%02d:%02d %02d:%02d\n",time[i<<1|1].l/60,time[i<<1|1].l%60,time[i<<1|1].r/60,time[i<<1|1].r%60);
}
}
}
return 0;
}

POJ 3683 Priest John&#39;s Busiest Day (2-SAT+输出可行解)的更多相关文章

  1. POJ 3684 Priest John&#39;s Busiest Day 2-SAT+输出路径

    强连通算法推断是否满足2-sat,然后反向建图,拓扑排序+染色. 一种选择是从 起点開始,还有一种是终点-持续时间那个点 開始. 若2个婚礼的某2种时间线段相交,则有矛盾,建边. easy出错的地方就 ...

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

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

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

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

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

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

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

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

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

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

  8. POJ 3683 Priest John's Busiest Day (2-SAT,常规)

    题意: 一些人要在同一天进行婚礼,但是牧师只有1个,每一对夫妻都有一个时间范围[s , e]可供牧师选择,且起码要m分钟才主持完毕,但是要么就在 s 就开始,要么就主持到刚好 e 结束.因为人数太多了 ...

  9. POJ 3683 Priest John's Busiest Day

    2-SAT简单题,判断一下两个开区间是否相交 #include<cstdio> #include<cstring> #include<cmath> #include ...

随机推荐

  1. 主从 binlog_format 设置关系

    1. 主库是row,从库必须是row/mixed.如果是statement,主库有变更时,从库报如下错误(无论什么变更都报错,如insert/update/delete/alter等):     La ...

  2. 简单了解了下SEO与SEM的机制

    SEO:搜索引擎优化SEM:搜索引擎营销 SEO排名机制:搜索引擎蜘蛛 权重 算法 排名规则 搜索引擎提交入口: 1.百度搜索网站登入口 2.Google网站登入口 3.360搜索引擎登入入口 4.搜 ...

  3. Python 开发初识

    从今天开始记录自己的python开发之路,用博客记录自己的学习经历,以及学习小结,小的项目模块,努力充实,做最好的自己!!!

  4. Windows提高_2.2第二部分:用户区同步

    第二部分:用户区同步 同步和互斥 同步:就是按照一定的顺序执行不同的线程 互斥:当一个线程访问某一资源的时候,其它线程不能同时访问 多线程产生的问题 #include <stdio.h> ...

  5. 并发编程学习笔记(15)----Executor框架的使用

    Executor执行已提交的 Runnable 任务的对象.此接口提供一种将任务提交与每个任务将如何运行的机制(包括线程使用的细节.调度等)分离开来的方法.通常使用 Executor 而不是显式地创建 ...

  6. token机制完成登录状态保持/身份认证

    一般APP都是刚安装后,第一次启动时需要登录(提示你需要登录或者直接启动在登录界面).而只要登录成功后,以后每次启动时都是登录状态,不需要每次启动时再次登录.不过,也有些APP若你长期未启动,再次启动 ...

  7. Vue指令6:v-show

    根据表达式的真假值来渲染元素 用法大致一样: <h1 v-show="ok">Hello!</h1> 不同的是带有 v-show 的元素始终会被渲染并保留在 ...

  8. Mybatis学习总结一

    一.Mybatis架构  JAR包下载地址 1.  mybatis配置 SqlMapConfig.xml,此文件作为mybatis的全局配置文件,配置了mybatis的运行环境等信息. mapper. ...

  9. JAVA程序员面试笔试宝典4

    1.HTTP中GET与POST方法有什么区别? GET方法上传数据时,数据添加在URL后面.同时,数据大小有限制,通常在1024Byte左右.POST方法传递数据是通过HTTP请求的附件进行的,传递的 ...

  10. Could not resolve type alias 'map '. Cause: java.lang.ClassNotFoundException: Cannot find class: map

    把resultType改为resultMap, 把parameterType改为parameterMap,重新发布并运行.