poj3683 Priest John's Busiest Day
2-SAT。
读入用了黄学长的快速读入,在此膜拜感谢。
把每对时间当作俩个点。如果有交叉代表相互矛盾。
然后tarjan缩点,这样就能得出当前的2-SAT问题是否有解。
如果有解,跑拓扑排序就能找出一个特定的解。
#include<cstdio>
#include<algorithm>
#include<cstring>
using namespace std;
const int maxn = 5000 + 10;
const int maxm = 2000000 + 10;
inline int read() //by hzwer 实在太好了。。我用下。。跪谢。
{
int x=0,f=1;char ch=getchar();
while(ch<'0'||ch>'9'){if(ch=='-')f=-1;ch=getchar();}
while(ch>='0'&&ch<='9'){x=x*10+ch-'0';ch=getchar();}
return x*f;
} int g[maxn],v[maxm],next[maxm],eid;
int deg[maxn],g2[maxn],v2[maxm],next2[maxm],eid2;
int color[maxn],cid;
int vis[maxn];
int dfn[maxn],low[maxn],vid;
int s[maxn],sp;
int a[maxn],b[maxn];
int op[maxn];
int q[maxm],l,r,u;
int c[maxn];
int n; void addedge(int a,int b) {
v[eid]=b; next[eid]=g[a]; g[a]=eid++;
} void addedge2(int a,int b) {
deg[b]++;
v2[eid2]=b; next2[eid2]=g2[a]; g2[a]=eid2++;
} bool con(int x,int y) {
if(b[x]<=a[y] || b[y]<=a[x]) return 0;
return 1;
} void build() {
memset(g,-1,sizeof(g));
n=read();
for(int i=1,t;i<=n;i++) {
a[i<<1]=read();
a[i<<1]=a[i<<1]*60+read();
b[i<<1|1]=read();
b[i<<1|1]=b[i<<1|1]*60+read();
t=read();
b[i<<1]=a[i<<1]+t;
a[i<<1|1]=b[i<<1|1]-t;
}
for(int i=1;i<=n;i++)
for(int j=i;j<=n;j++) if(i!=j) {
if(con(i<<1,j<<1)) {
addedge(i<<1,j<<1|1);
addedge(j<<1,i<<1|1);
}
if(con(i<<1,j<<1|1)) {
addedge(i<<1,j<<1);
addedge(j<<1|1,i<<1|1);
}
if(con(i<<1|1,j<<1)) {
addedge(i<<1|1,j<<1|1);
addedge(j<<1,i<<1);
}
if(con(i<<1|1,j<<1|1)) {
addedge(i<<1|1,j<<1);
addedge(j<<1|1,i<<1);
}
} }
void tarjan(int u) {
dfn[u]=low[u]=++vid;
s[++sp]=u; vis[u]=1;
for(int i=g[u];~i;i=next[i]) {
if(vis[v[i]]==0) {
tarjan(v[i]);
low[u]=min(low[u],low[v[i]]);
}
else if(vis[v[i]]==1) {
low[u]=min(low[u],dfn[v[i]]);
}
} if(dfn[u]==low[u]) {
++cid;
do {
color[s[sp]]=cid;
vis[s[sp]]=2;
}while(s[sp--]!=u);
}
} void dfs(int u) {
if(c[u]) return;
c[u]=-1;
for(int i=g2[u];~i;i=next2[i]) dfs(v2[i]);
} void print(int x) {
printf("%.2d:%.2d %.2d:%.2d\n",a[x]/60,a[x]%60,b[x]/60,b[x]%60);
} void solve() {
for(int i=2;i<=((n<<1)|1);i++) if(!vis[i]) tarjan(i);
for(int i=1;i<=n;i++) {
if(color[i<<1]==color[i<<1|1]) {
printf("NO\n");
return;
}
}
printf("YES\n");
memset(g2,-1,sizeof(g2));
for(int u=2;u<=((n<<1)|1);u++) {
for(int i=g[u];~i;i=next[i]) if(color[u]!=color[v[i]])
addedge2(color[v[i]],color[u]);
} for(int i=1;i<=n;i++) {
op[color[i<<1]]=color[i<<1|1];
op[color[i<<1|1]]=color[i<<1];
} for(int u=1;u<=cid;u++) if(!deg[u]) q[++r]=u; while(r) {
u=q[r--];
if(c[u]) continue;
c[u]=1; dfs(op[u]);
for(int i=g2[u];~i;i=next2[i]) {
deg[v2[i]]--;
if(!deg[v2[i]]) q[++r]=v2[i];
}
}
for(int i=1;i<=n;i++)
if(c[color[i<<1]]==1) print(i<<1);
else print(i<<1|1);
} int main() {
build();
solve();
return 0;
}
poj3683 Priest John's Busiest Day的更多相关文章
- POJ3683 Priest John's Busiest Day(2-SAT)
Time Limit: 2000MS Memory Limit: 65536K Total Submissions: 11049 Accepted: 3767 Special Judge ...
- POJ3683 Priest John's Busiest Day 【2-sat】
题目 John is the only priest in his town. September 1st is the John's busiest day in a year because th ...
- poj3683 Priest John's Busiest Day
2-SAT 输出可行解 找可行解的方案就是: 根据第一次建的图建一个反图..然后求逆拓扑排序,建反图的原因是保持冲突的两个事件肯定会被染成不同的颜色 求逆拓扑排序的原因也是为了对图染的色不会发生冲突, ...
- 【POJ3683】Priest John's Busiest Day
题目 John is the only priest in his town. September 1st is the John's busiest day in a year because th ...
- 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):Priest John's Busiest Day
Priest John's Busiest Day Description John is the only priest in his town. September 1st is the Jo ...
- 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 ...
随机推荐
- Opencv图像与矩阵的操作
#include "stdafx.h" #include <cv.h> #include <cxcore.h> #include <highgui.h ...
- 《Dive into Python》Chapter 4 笔记
自省:Python中万物皆对象,自省是指代码可以查看内存中以对象形式存在的其它模块和函数,获取它们的信息,并对它们进行操作.用这种方法,可以定义没有名称的函数,不按函数声明的参数顺序调用函数,甚至引用 ...
- Python编程规范
打破一条既定规则的两个理由: 应用这个规则将导致代码可读性下降. 为了和周围的代码保持一致. 编码: 所有的 Python 脚本文件都应在文件头标上如下标识或其兼容格式的标识: # -*- codin ...
- C# testJsonAsXMLNodeAttribute - XML& json & Collections - XmlNode, XmlElement, XmlAttribute,Dictionary,List
testJsonAsXMLNodeAttribute using Newtonsoft.Json; using System; using System.Collections.Generic; us ...
- sqlserver mdf ldf文件导入
EXEC sp_attach_db @dbname = '你的数据库名', @filename1 = 'mdf文件路径(包缀名)', @filename2 = 'Ldf文件路径(包缀名 ...
- 1257: [CQOI2007]余数之和sum - BZOJ
Description 给出正整数n和k,计算j(n, k)=k mod 1 + k mod 2 + k mod 3 + … + k mod n的值,其中k mod i表示k除以i的余数.例如j(5, ...
- 一些css效果积累
悬浮效果: ul li a{text-decoration: none;color: black} ul li a:hover{color: blue} 鼠标变小手 span:hover{cur ...
- ${fn:length(worklicenseList)} #表示不在struts堆栈里,没有#表示从struts堆栈里取
${fn:length(worklicenseList)} #表示不在struts堆栈里,没有#表示从struts堆栈里取
- JsRender系列demo(1)-insert-data
<!DOCTYPE html> <html xmlns="http://www.w3.org/1999/xhtml"> <head> <m ...
- http://www.oschina.net/translate/elasticsearch-getting-started?cmp
http://www.oschina.net/translate/elasticsearch-getting-started?cmp