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场婚礼,每场 ...
随机推荐
- 云数据库RDS SQL Server 版
云数据库RDS SQL Server版是一种可弹性伸缩的在线数据库服务,并具备自动监控.备份.容灾恢复等方面的全套解决方案,彻底解决数据库运维的烦恼 请观看视频简介 SQL Server是发行最早的商 ...
- MVVM 和 VUE三要素:响应式、模板引擎、渲染
MVVM 和 VUE三要素:响应式.模板引擎.渲染:https://blog.csdn.net/weixin_37644989/article/details/94409430
- hashmap存储数据
在HashMap中,为什么不能使用基本数据类型作为key? 其实和HashMap底层的存储原理有关,HashMap存储数据的特点是:无序.无索引.不能存储重复元素. 存储元素采用的是hash表存储数据 ...
- mongoose操作笔记
一.mongoose文档地址: https://cn.mongoosedoc.top/docs/api.html#update_update https://www.cnblogs.com/web-f ...
- C# using用法之一(命名空间相关)
通过using关键字可以引入命名空间 using System; using System.Collections.Generic; using System.Linq; using System.T ...
- LLVM源码安装教程
LLVM4.0源码安装教程 环境:ubuntu16.04 llvm-4.0 clang-4.0 步骤: 1.依赖库安装,注意llvm的编译对gcc版本和cmake版本有要求,请根据版本进行匹配 $ s ...
- 多线程编程-- part 4 线程间的通信
线程间的相互作用 线程之间需要一些协调通信,来共同完成一件任务. Object类相关的方法:notify(),notifyAll(),wait().会被所有的类继承,这些方法是final不能被重写.他 ...
- php 多肽实例
多态定义:只关心一个接口或者基类,而不关心一个对象的具体类.(同一类型,不同结果) 这里两个例子: 第一个,我们发现,基类定义了标准,子类进行了自我规则的实现.这是多态的一个要求.同时,这是满足重写: ...
- service与pod关联
当我们创建pod时,仅仅是创建了pod,要为其创建rc(ReplicationController),他才会有固定的副本,然后为其创建service,集群内部才能访问该pod,使用 NodePort ...
- zabbix 启到不起来:active check configuration update from [127.0.0.1:10051] started to fail (cannot connect to [[127.0.0.1]:10051]: [111] Connection refused)
cat /var/log/zabbix_agent_log 查看日记出现报错:active check configuration update from [127.0.0.1:10051] star ...