POJ 3683 神父赶婚宴 2-SAT+输出模板
题意:一个小镇里面只有一个牧师,现在有些新人要结婚,需要牧师分别去主持一个仪式,给出每对新人婚礼的开始时间 s 和结束时间 t ,还有他们俩的这个仪式需要的时间(每对新人需要的时间长短可能不同) d ,牧师可以在婚礼开始的时间 d 内(s 到 s+d)或者是结束前的时间 d 内(t - d 到 t)完成这个仪式。现在问能否给出一种安排,让牧师能完成所有夫妇婚礼的仪式,如果可以,输出一种安排。
#include <iostream>
#include <cstdio>
#include <cstring>
#include <cstdlib>
#include <cmath>
#include <vector>
#include <queue>
#include <stack>
#include <map>
#include <algorithm>
#include <set>
using namespace std;
typedef long long ll;
typedef unsigned long long Ull;
#define MM(a,b) memset(a,b,sizeof(a));
const double eps = 1e-10;
const int inf =0x7f7f7f7f;
const double pi=acos(-1);
const int maxn=100+1000; int n,m,pre[2*maxn],lowlink[2*maxn],dfs_clock,sccno[2*maxn],scc_cnt;
int opp[2*maxn],st[2*maxn],ed[2*maxn],tt[2*maxn],indeg[2*maxn],color[2*maxn]; struct node{
int l,r;
}ne[2*maxn];
vector<int> G[2*maxn],g[2*maxn];
stack<int> S; void tarjan(int u)
{
pre[u]=lowlink[u]=++dfs_clock;
S.push(u);
for(int i=0;i<G[u].size();i++)
{
int v=G[u][i];
if(!pre[v])
{
tarjan(v);
lowlink[u]=min(lowlink[u],lowlink[v]);
}
else if(!sccno[v])
lowlink[u]=min(lowlink[u],pre[v]);
} if(pre[u]==lowlink[u])
{
scc_cnt++;
for(;;)
{
int cur=S.top();S.pop();
sccno[cur]=scc_cnt;
if(u==cur) break;
}
}
} bool find_scc()
{
MM(pre,0);
MM(sccno,0);
MM(lowlink,0);
dfs_clock=scc_cnt=0; for(int i=0;i<2*n;i++)
if(!pre[i])
tarjan(i); for(int i=0;i<2*n;i++)
if(sccno[i]==sccno[i^1])
return false;
return true;
} bool inter(node a,node b)
{
return a.r>b.l&&a.l<b.r;
} void build()
{
for(int i=0;i<2*n;i++)
G[i].clear(); for(int i=0;i<2*n;i++)
for(int j=i+1;j<2*n;j++)//对任意两个点都进行下判断
if(inter(ne[i],ne[j]))
{
G[i].push_back(j^1);
G[j].push_back(i^1);
}
} void topsort()
{
MM(indeg,0);
MM(color,0);
for(int i=1;i<=scc_cnt;i++)
g[i].clear(); for(int i=0;i<2*n;i++)
{
opp[sccno[i]]=sccno[i^1];
opp[sccno[i^1]]=sccno[i];
} for(int u=0;u<2*n;u++)
for(int i=0;i<G[u].size();i++)
{
int a=sccno[u],b=sccno[G[u][i]];
if(a!=b)
{
indeg[a]++;
g[b].push_back(a);
}
} queue<int> q;
for(int i=1;i<=scc_cnt;i++)
if(!indeg[i]) q.push(i); while(q.size())
{
int u=q.front();q.pop();
if(!color[u])
{
color[u]=1;
color[opp[u]]=-1;
}
for(int i=0;i<g[u].size();i++)
{
int v=g[u][i];
indeg[v]--;
if(!indeg[v]) q.push(v);
}
} for(int i=0;i<n;i++)
if(color[sccno[i<<1]]==1)
printf("%02d:%02d %02d:%02d\n",ne[i<<1].l/60,ne[i<<1].l%60,ne[i<<1].r/60,ne[i<<1].r%60);
else
printf("%02d:%02d %02d:%02d\n",ne[i<<1^1].l/60,ne[i<<1^1].l%60,ne[i<<1^1].r/60,ne[i<<1^1].r%60);
} int main()
{
while(~scanf("%d",&n))
{
for(int i=0;i<n;i++)
{
int a,b,c,d,e; scanf("%d:%d %d:%d %d",&a,&b,&c,&d,&e);
ne[i<<1].l=a*60+b;
ne[i<<1].r=a*60+b+e;
ne[i<<1^1].l=60*c+d-e;
ne[i<<1^1].r=60*c+d;
}//针对每个点,其反点是^关系 build(); if(find_scc())
{
printf("YES\n");
topsort();
}
else printf("NO\n");
}
return 0;
}
POJ 3683 神父赶婚宴 2-SAT+输出模板的更多相关文章
- poj 3683 2-sat建图+拓扑排序输出结果
发现建图的方法各有不同,前面一题连边和这一题连边建图的点就不同,感觉这题的建图方案更好. 题意:给出每个婚礼的2个主持时间,每个婚礼的可能能会冲突,输出方案. 思路:n个婚礼,2*n个点,每组点是对称 ...
- 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+输出可行解)
题目地址:POJ 3683 第一次做须要输出可行解的题目. . .大体思路是先用强连通来推断是否有可行解,然后用逆序建图.用拓扑排序来进行染色.然后输出可行解. 详细思路见传送门 由于推断的时候少写了 ...
- 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 ...
- 2-SAT的小总结(POJ 3683 POJ 3207)
记住几个最重要的公式: xANDy=0<=>(x=>y′)AND(y=>x′) xANDy=1<=>(x′=>x)AND(y′=>y) xORy=0&l ...
- poj 3683(2-sat+输出一组可行解)
题目链接:http://poj.org/problem?id=3683 思路:对于每个结婚仪式,只有在开始或结束时进行这两种选择,我们可以定义xi为真当且仅当在开始时进行.于是我们可以通过时间先后确定 ...
- 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 2-sat问题,输出任意一组可行解
/* 2sat问题 输出任意一组可行解 */ #include<stdio.h> #include<string.h> #include<stdlib.h> #in ...
- poj 3683(2-SAT+SCC)
传送门:Problem 3683 https://www.cnblogs.com/violet-acmer/p/9769406.html 参考资料: [1]:挑战程序设计竞赛 题意: 有n场婚礼,每场 ...
随机推荐
- C++ Primer 回炉重铸(一)
过去学C++语法都是用的这本C++Primer第五版 说实话,这本书应该是业界用的最多的一本类似于C++语法的百科全书了.. 但是感觉自己学了这么长时间的C++,语法层次还是不够牢固. 比如templ ...
- pycharm 快捷键练习 和基本英语单词练习
通过练习 一下快捷键 打代码的速度得到提升 pycharm以下 快捷键+快捷键意义 ctrl+a 全选 ctrl+c 复制(不选中默认复制一行) ctrl+v 粘贴 ctrl+x 剪切 ctrl+f ...
- Hive 教程(二)-认知hive
在大数据领域,hive 的位置非常重要,排名前三的大数据工具为 spark.hive.kafka 什么是hive 在大数据领域有 3 种需求场景:传输.存储.计算: hive 是一个处理海量的结构化数 ...
- SpringMVC实现全局异常处理器 (转)
出处: SpringMVC实现全局异常处理器 我们知道,系统中异常包括:编译时异常和运行时异常RuntimeException,前者通过捕获异常从而获取异常信息,后者主要通过规范代码开发.测试通过手 ...
- 导出excel模版
方法一: public void ToExcel(){ //第一步:获取模版物理路径 string file_1 = Server.MapPath("/Content/Excel/downE ...
- Kong/Konga - Docker容器化安装
1.0 安装kong + postgresDB docker network create kong-net docker pull postgres:latest docker run -d --n ...
- git简易操作手册
从远程仓库新建 $ git clone https://github.com/xxxr/Repository.git $ cd Repository/ $ git config --global us ...
- springboot(二十)-配置文件 bootstrap和application区别
用过 Spring Boot 的都知道在 Spring Boot 中有以下两种配置文件 bootstrap (.yml 或者 .properties) application (.yml 或者 .pr ...
- b/s和c/s
一.B/S结构 B是英文单词“Browser”的首字母,即浏览器的意思:S是英文单词“Server”的首字母,即服务器的意思.B/S就是“Browser/Server”的缩写,即“浏览器/服务器”模式 ...
- CSS media queries 媒体查询
最近在做一些页面打印时的特殊处理接触到了media queries,想系统学习一下,在MOZILLA DEVELOPER NETWORK看到一篇文章讲的很不错,结合自己的使用总结一下. CSS2/me ...