题目地址: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. Java获取一个文件夹内的所有文件(包括所有子文件夹内的)

    输入文件数组.文件夹路径 返回的文件在输入的文件数组中 private void getFiles(ArrayList<File> fileList, String path) { Fil ...

  2. C++ 泛型程序设计与STL模板库(1)---泛型程序设计简介及STL简介与结构

    泛型程序设计的基本概念 编写不依赖于具体数据类型的程序 将算法从特定的数据结构中抽象出来,成为通用的 C++的模板为泛型程序设计奠定了关键的基础 术语:概念 用来界定具备一定功能的数据类型.例如: 将 ...

  3. 系统异常 NSException.h

    FOUNDATION_EXPORT NSExceptionName const NSGenericException; FOUNDATION_EXPORT NSExceptionName const ...

  4. sharepoint services

    I have got solution for authentication to share point web service I have use fedAuth Cookie and rtfa ...

  5. WEB前端响应式布局之BootStarp使用

    1.Bootstrap简介:1. 概念: 一个前端开发的框架,Bootstrap,来自 Twitter,是目前很受欢迎的前端框架.Bootstrap 是基于 HTML.CSS.JavaScript 的 ...

  6. First Project -用函数写的ATM+购物商城程序

    作业需求:模拟实现一个ATM + 购物商城程序 额度15000或自定义 实现购物商城,买东西加入 购物车,调用信用卡接口结账 可以提现,手续费5% 每月22号出账单,每月10号为还款日,过期未还,按欠 ...

  7. vue基础---条件渲染

    (1)v-if条件渲染 v-if 指令用于条件性地渲染一块内容.这块内容只会在指令的表达式返回 truthy 值的时候被渲染. 可以用 v-else 添加一个“else 块”: ①表达式 <di ...

  8. "ping: unknown host www.baidu.com"问题解决方式

    参考:https://blog.csdn.net/wbainngg123/article/details/51540535 在虚拟机VMware里选择桥接模式,并配置网络之后,发现ping ip地址可 ...

  9. A4. JVM 内存分配及回收策略

    [概述] Java 技术体系中所提倡的自动内存管理最终可以归结为自动化地解决两个问题:给对象分配内存以及回收分配给对象的内存. 对象的内存分配,往大方向讲,就是在堆上分配,对象主要分配在新生代的 Ed ...

  10. Scrapy 组件的具体用法

    一.Spider 用法 在 Scrapy 中,要抓取网站的链接配置.抓取逻辑.解析逻辑都是在 Spider 里完成的.Spider 的一些基础属性和基础方法: name:爬虫名字,Spider的名字定 ...