poj3683 2-sat Priest John's Busiest Day
Description
John is the only priest in his town. September 1st is the John's busiest day in a year because there is an old legend in the town that the couple who get married on that day will be forever blessed by the God of Love. This year N couples plan to get married on the blessed day. The i-th couple plan to hold their wedding from time Si to time Ti. According to the traditions in the town, there must be a special ceremony on which the couple stand before the priest and accept blessings. The i-th couple need Di minutes to finish this ceremony. Moreover, this ceremony must be either at the beginning or the ending of the wedding (i.e. it must be either from Si to Si + Di, or from Ti - Di toTi). Could you tell John how to arrange his schedule so that he can present at every special ceremonies of the weddings.
Note that John can not be present at two weddings simultaneously.
Input
The first line contains a integer N ( 1 ≤ N ≤ 1000).
The next N lines contain the Si, Ti and Di. Si and Ti are in the format of hh:mm.
Output
The first line of output contains "YES" or "NO" indicating whether John can be present at every special ceremony. If it is "YES", output another N lines describing the staring time and finishing time of all the ceremonies.
Sample Input
2
08:00 09:00 30
08:15 09:00 20
Sample Output
YES
08:00 08:30
08:40 09:00
Source
这个2-sat做的一路蒙蔽
判断个人觉得很简单,
输出解就要命了
有一个小镇上只有一个牧师。这个小镇上有一个传说,
在九月一日结婚的人会受到爱神的保佑,但是要牧师举办一个仪式。
这个仪式要么在婚礼刚刚开始的时候举行,要么举行完婚礼正好结束。
现在已知有n场婚礼,告诉你每一场的开始和结束时间,
以及举行仪式所需要的时间。问牧师能否参加所有的婚礼,
如果能则输出一种方案。
这题输出解的方法
构建包含2n个点的有向图,如果有a+b则在a和!b b和!a间连接一条边。
如果a和!a在一个强连通分量中,则无解。要求解集,
只需要将原图缩点后反向建图,然后染色,
具体染色方法是将遇到的第一个没有颜色的点染成红色,与它矛盾的点染成蓝色,
如此循环,所有的红色的点的集合就是解集。
多看点书还是有好处的 ,
这是大佬讲的,理解不了就记忆吧 ,也许这就是弱鸡吧
求大佬给出证明
这题的建图非常简单就没必要讲了
难受啊!!!!
#include <iostream>
#include <cstdio>
#include <cstring>
#include <queue>
#include <vector> using namespace std;
const int maxn = 4e5 + ;
struct w {
int s, e;
void disp() {
printf("%02d:%02d %02d:%02d\n", s / , s % , e / , e % );
}
} wed[maxn];
struct node {
int u, v, next;
} edge[maxn];
int dfn[maxn], s[maxn], instack[maxn];
int head[maxn], low[maxn], belong[maxn];
int tot, flag, cnt, top, n, m;
void add(int u, int v) {
edge[tot].u = u;
edge[tot].v = v;
edge[tot].next = head[u];
head[u] = tot++;
}
void init() {
tot = flag = top = cnt = ;
memset(dfn, , sizeof(dfn));
memset(head, -, sizeof(head));
memset(instack, , sizeof(head));
memset(s, , sizeof(s));
}
void tarjan(int v) {
low[v] = dfn[v] = ++flag;
instack[v] = ;
s[top++] = v;
for (int i = head[v] ; ~i ; i = edge[i].next ) {
int j = edge[i].v;
if (!dfn[j]) {
tarjan(j);
low[v] = min(low[v], low[j]);
} else if (instack[j]) low[v] = min(low[v], dfn[j]);
}
if (dfn[v] == low[v]) {
cnt++;
int t;
do {
t = s[--top];
instack[t] = ;
belong[t] = cnt;
} while(t != v);
}
} int check(int i, int j) {
if (wed[i].s >= wed[j].e || wed[i].e <= wed[j].s ) return ;
return ;
}
void build(int i, int j) {
if (check( * i, * j)) add( * i, * j + );
if (check( * i, * j + )) add( * i, * j);
if (check( * i + , * j)) add( * i + , * j + );
if (check( * i + , * j + )) add( * i + , * j);
}
int in[maxn];
queue<int>q;
vector<int>tu[maxn];
vector<int>ha[maxn];
int color[maxn];
void maketu() {
int v;
for (int u = ; u < * n ; u++) {
for (int i = head[u] ; ~i ; i = edge[i].next) {
v = edge[i].v;
if (belong[u] != belong[v]) {
tu[belong[v]].push_back(belong[u]);
in[belong[u]]++;
}
}
}
} void topsort() {
for (int i = ; i <= cnt ; i++)
if (!in[i]) q.push(i);
int u, v;
while(!q.empty()) {
u = q.front();
q.pop();
if (!color[u]) {
color[u] = ;
for (int i = ; i < ha[u].size() ; i++)
color[ha[u][i]] = ;
}
for (int i = ; i < tu[u].size() ; i++) {
v = tu[u][i];
in[v]--;
if (!in[v]) q.push(v);
}
}
}
void solve() {
for (int i = ; i < n ; i++) {
if (belong[i << ] == belong[i << | ]) {
printf("NO\n");
return ;
} else {
ha[belong[i << ]].push_back(belong[i << | ]);
ha[belong[i << | ]].push_back(belong[i << ]);
}
}
printf("YES\n");
maketu();
topsort();
for (int i = ; i < n ; i++) {
if (color[belong[i << ]] == ) wed[i << ].disp();
else wed[i << | ].disp();
}
} int main() {
// freopen("DATA.txt", "r", stdin);
scanf("%d", &n);
init();
int x, y, x1, y1, d;
for (int i = ; i < n ; i++) {
scanf("%d:%d %d:%d %d", &x, &y, &x1, &y1, &d);
wed[i << ].s = x * + y;
wed[i << ].e = wed[i << ].s + d;
wed[i << | ].e = x1 * + y1;
wed[i << | ].s = wed[i << | ].e-d;
}
for (int i = ; i < n ; i++)
for (int j = ; j < n ; j++)
if (i != j) build(i, j);
for (int i = ; i < * n ; i++)
if (!dfn[i]) tarjan(i);
solve();
return ;
}
poj3683 2-sat Priest John's Busiest Day的更多相关文章
- 【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 ...
- Priest John's Busiest Day(POJ 3683)
原题如下: Priest John's Busiest Day Time Limit: 2000MS Memory Limit: 65536K Total Submissions: 12162 ...
- 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 ...
随机推荐
- rails中如何在a标签中添加其他标签
最近在用rails写一个项目练练手,然后遇到了一个问题,就是用 <% link_to("首页", root_path) %> 生成一个a标签,之后就在想我怎么在这个a标 ...
- 001---Python简介
编程语言: 机器语言 最底层,更容易被计算机识别,执行速度最快 复杂,开发效率低 汇编语言 比较底层,执行速度较快 同样复杂 高级语言 编译型语言:先编译,后执行.生成独立的可执行文件.是计算机可以理 ...
- System.Speech使用
使用微软语音库 使用微软语音库可以很快速的制作一个小应用,比如一个唐诗的朗诵工具.本示例也是使用微软语音库,制作了一个唐诗宋词朗诵的应用,仅供加深学习印象 首先是要引入System.Speech库 然 ...
- @Transactional spring 事务失效(转载)
原文地址:http://hwak.iteye.com/blog/1611970 1. 在需要事务管理的地方加@Transactional 注解.@Transactional 注解可以被应用于接口定义和 ...
- VS中的一些标记
1.//ToDO:此标记运行时会显示在任务列表窗口中.
- 通过transpose和flip实现图像旋转90/180/270度
在fbc_cv库中,提供了对图像进行任意角度旋转的函数rotate,其实内部也是调用了仿射变换函数warpAffine.如果图像仅是进行90度倍数的旋转,是没有必要用warpAffine函数的.这里通 ...
- LeetCode:11. ContainerWithWater(Medium)
原题链接:https://leetcode.com/problems/container-with-most-water/description/ 题目要求:给定n个非负整数a1,a2,...,an ...
- shell eval命令使用
eval命令将会首先扫描命令行进行所有的置换,然后再执行该命令. 该命令适用于那些一次扫描无法实现其功能的变量.该命令对变量进行两次扫描. 这些需要进行两次扫描的变量有时被称为复杂变量.不过这些变量本 ...
- Jmeter学习(三)
Apache JMeter是Apache组织开发的基于Java的压力测试工具.用于对软件做压力测试,它最初被设计用于Web应用测试,但后来扩展到其他测试.(来自百度) jmeter的特点: 开源免费. ...
- VC中结构体的内存布局
看了 VC++中内存对齐 这篇文章,感觉说复杂了,根据我的总结,要算出结构体的内存大小和偏移量,只要清楚结构体各成员的内存布局就行了,下面介绍一下我总结的规则,有不对之处,欢迎回复. 1.实际PACK ...