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 ...
随机推荐
- PrestaShop 网站漏洞修复如何修复
PrestaShop网站的漏洞越来越多,该网站系统是很多外贸网站在使用的一个开源系统,从之前的1.0初始版本到现在的1.7版本,经历了多次的升级,系统使用的人也越来越多,国内使用该系统的外贸公司也很多 ...
- Java学习笔记十三:Java中的类和对象
Java中的类和对象 一:什么是对象: 总的来说就是"万物皆对象",客观存在的事物皆为对象.是计算机所关注的具体信息. 对象(object)是一件事.一个物体.一个名词,或可以获得 ...
- Python3爬虫(五)解析库的使用之XPath
Infi-chu: http://www.cnblogs.com/Infi-chu/ XPath: 全称是 XML Path Language,XML路径语言,它是一门在XML文档中和HTML文档中查 ...
- POJ 3210 : Coins
参考:https://blog.csdn.net/u010885899/article/details/46636523 http://kqwd.blog.163.com/blog/static/41 ...
- SGU 101 Domino 题解
鉴于SGU题目难度较大,AC后便给出算法并发布博文,代码则写得较满意后再补上.——icedream61 题目简述:暂略 AC人数:3609(2015年7月20日) 算法: 这题就是一笔画,最多只有7个 ...
- ES6 语法糖
重新认识ES6中的语法糖:https://segmentfault.com/a/1190000010159725
- (原创)不过如此的 DFS 深度优先遍历
DFS 深度优先遍历 DFS算法用于遍历图结构,旨在遍历每一个结点,顾名思义,这种方法把遍历的重点放在深度上,什么意思呢?就是在访问过的结点做标记的前提下,一条路走到天黑,我们都知道当每一个结点都有很 ...
- Linux下的命令行
一.文件传输(两种方式) 1. 使用CRT传输 1. 一定要修改编码为UTF-8类型 1. 按住alt + p 切换成传输文件的窗口,然后拖拽文件进来即可 2. 使用类似xftp这种软件传输 这种软件 ...
- 【转】The best career advice I’ve received
原文地址:http://www.nczonline.net/blog/2013/10/15/the-best-career-advice-ive-received/ I recently had an ...
- 软工实践Beta冲刺(4/7)
队名:起床一起肝活队 组长博客:博客链接 作业博客:班级博客本次作业的链接 组员情况 组员1(队长):白晨曦 过去两天完成了哪些任务 描述: 1.界面的修改与完善 展示GitHub当日代码/文档签入记 ...