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的更多相关文章

  1. POJ3683 Priest John's Busiest Day(2-SAT)

    Time Limit: 2000MS   Memory Limit: 65536K Total Submissions: 11049   Accepted: 3767   Special Judge ...

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

  3. poj3683 Priest John's Busiest Day

    2-SAT 输出可行解 找可行解的方案就是: 根据第一次建的图建一个反图..然后求逆拓扑排序,建反图的原因是保持冲突的两个事件肯定会被染成不同的颜色 求逆拓扑排序的原因也是为了对图染的色不会发生冲突, ...

  4. 【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 ...

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

  6. 图论(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 ...

  7. poj 3686 Priest John's Busiest Day

    http://poj.org/problem?id=3683 2-sat 问题判定,输出一组可行解 http://www.cnblogs.com/TheRoadToTheGold/p/8436948. ...

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

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

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

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

随机推荐

  1. WPF解析Fnt字体

    偶遇需要再 WPF中加载Fnt字体,此做... using System; using System.Collections.Generic; using System.Drawing; using ...

  2. 在Visual Studio中快速启动调试Web应用程序

    原文:http://blog.csdn.net/effun/article/details/2638535 到2005,Visual Studio在启动调试的功能上进行了一些改善,不过因为只是简单的一 ...

  3. 《C++Primer》复习——with C++11 [2]

    1.数组引用形参,C++允许将变量定义成数组的引用,给予同样的道理,形参也可以是数组的引用,此时引用形参绑定到对应的实参上,也就是绑定到数组上 ]) { for (auto elem : arr) c ...

  4. java 连接池的简单实现

    最近一个项目中需要自己写个连接池, 写了一个下午,挺辛苦的,但不知道会不会出问题, 所以,贴到博客上,欢迎各路大神指点 1. 配置信息: /** * */ package cn.mjorcen.db. ...

  5. shell脚本积累

    统计当前目录下文件夹的大小 for d in $(ls) do du -sh ./$d done 获取之前日期date  +"%Y%m%d" -d  "-n days&q ...

  6. CentOS7 安装 PostGIS方法(适合国内网络

    安装Postgresql 9.4 yum install http://yum.postgresql.org/9.4/redhat/rhel-6-x86_64/pgdg-redhat94-9.4-1. ...

  7. MVC3 Model Binding验证方式

    1.使用ModelState在Action中进行验证 [HttpPost] public ViewResult MakeBooking(Appointment appt) { if (string.I ...

  8. jQuery1.9.1源码分析--Ajax模块

    //Serialize an array of form elements or a set of //key/values into a query string // 将数组形式的表单元素或者哈希 ...

  9. Code::Blocks生成的EXE文件执行错误解决:The program can't start because libgcc_s_dw2-1.dll is missing

    想用C++弄个简单东东,看有没有可行性, 开发软件,微软的太大太肿,就选用了Code::Blocks. 测试HELLO时,在工程环境中没问题的,但生成的EXE执行有问题, 报什么 libgcc_s_d ...

  10. lintcode : 空格替换

    题目: 空格替换 设计一种方法,将一个字符串中的所有空格替换成 %20 .你可以假设该字符串有足够的空间来加入新的字符,且你得到的是“真实的”字符长度. 样例 对于字符串"Mr John S ...