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. 1562: [NOI2009]变换序列 - BZOJ

    Description Input Output Sample Input 5 1 1 2 2 1 Sample Output 1 2 4 0 3 HINT 30%的数据中N≤50:60%的数据中N≤ ...

  2. execvp使用实例

    问题描述:           本程序实现模拟shell功能,用户输入命令,返回相应的结果 问题解决: 注:        以上指出了execvp函数的使用,使用时第一个参数是文件名,第二个参数是一个 ...

  3. Codeforces Round #241 (Div. 2)->B. Art Union

    B. Art Union time limit per test 1 second memory limit per test 256 megabytes input standard input o ...

  4. Eclipse plugin插件开发 NoClassDefFoundError

    Eclipse的每一个plugin都有属于自己的类加载器,这是OSGI架构的基础,每一个plugin项目都是一个bundle,独立运行在各自的运行环境里面,这就造成了开发时和运行时的不同. Eclip ...

  5. Linux操作系统下软件的安装方法大全

    一.rpm包安装方式步骤: 1.找到相应的软件包,比如soft.version.rpm,下载到本机某个目录: 2.打开一个终端,su -成root用户: 3.cd soft.version.rpm所在 ...

  6. 【锋利的JQuery-学习笔记】切换网页皮肤-且保存于Cookie

    切换网页皮肤: html片段: <head> <link rel="stylesheet" href="styles/skin/skin_0.css&q ...

  7. uva 10912

    dp 记忆化搜索 #include <cstdio> #include <cstdlib> #include <cmath> #include <map> ...

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

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

  9. StringBuffer 和 StringBuilder

    如果你读过<Think in Java>,而且对里面描述HashTable和HashMap区别的那部分章节比较熟悉的话,你一定也明白了原因所在.对,就是支持线程同步保证线程安全而导致性能下 ...

  10. Asp.Net缓存(2)

    缓存页的多个版本 ASP.NET 允许在输出缓存中缓存同一页的多个版本.输出缓存可能会因下列因素而异: 初始请求 (HTTP GET) 中的查询字符串. 回发时传递的控制值(HTTP POST 值). ...