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 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 to Ti). Could you tell John how to arrange his schedule so that he can present at every special ceremonies of the weddings.
题意
挑战P326
思路
记\(x_i\)为第\(i\)对新人在开始时举行仪式
记\(\overline{x_i}\)为第\(i\)对新人在结束时举行仪式
如果\(x_i\)与\(x_j\)冲突,那么\(x_i\)与\(x_j\)不能同时取到,所以连边\(x_i\)->\(\overline{x_j}\),\(x_j\)->\(\overline{x_i}\)
其他三种情况同理
注意,结束时间可以和另外一个开始时间相同
#include<iostream>
#include<algorithm>
#include<vector>
#include<stack>
#include<queue>
#include<map>
#include<set>
#include<cstdio>
#include<cstring>
#include<cmath>
#include<ctime>
#define fuck(x) cerr<<#x<<" = "<<x<<endl;
#define debug(a, x) cerr<<#a<<"["<<x<<"] = "<<a[x]<<endl;
#define lson l,mid,ls
#define rson mid+1,r,rs
#define ls (rt<<1)
#define rs ((rt<<1)|1)
using namespace std;
typedef long long ll;
typedef unsigned long long ull;
const int loveisblue = 486;
const int maxn = 1000086;
const int maxm = 2000086;
const int inf = 0x3f3f3f3f;
const ll Inf = 999999999999999999;
const int mod = 1000000007;
const double eps = 1e-6;
const double pi = acos(-1);
int n;
int point1(int x){
return x;
}
int point2(int x){
return x+n;
}
char s[10];
struct node{
int st,ed,d;
}a[maxn];
int cal(){
int tmp1 = (s[0]-48)*10 + s[1]-48;
int tmp2 = (s[3]-48)*10 + s[4]-48;
return tmp1*60 + tmp2;
}
int check(int st1,int st2,int d1,int d2){
int ed1 = st1+d1;
int ed2 = st2+d2;
if(ed1>st2&&ed1<=ed2){
return true;
}else return ed2 > st1 && ed2 <= ed1;
}
int Head[maxn],cnt;
struct edge{
int Next,v;
}e[maxm],et[maxm];
void add_edge(int u,int v){
// cerr<<u<<" "<<v<<endl;
e[cnt].Next=Head[u];
e[cnt].v=v;
Head[u]=cnt++;
}
int Headt[maxn],cntt;
void add_edget(int u,int v){
// cerr<<u<<" "<<v<<endl;
et[cntt].Next=Headt[u];
et[cntt].v=v;
Headt[u]=cntt++;
}
int dfn[maxn],low[maxn],color[maxn];
int Index,sig;//只有这两个变量和dfn需要初始化
bool vis[maxn];
stack<int>sta;
void Tarjan(int u)
{
dfn[u]=low[u]=++Index;
sta.push(u);
vis[u]=true;
for(int k=Head[u];k!=-1;k=e[k].Next){
int v = e[k].v;
if(!dfn[v]){
Tarjan(v);
low[u]=min(low[u],low[v]);
}else if(vis[v]){low[u]=min(low[u],low[v]);}
}
if(dfn[u]==low[u]){
sig++;
while(true){
int cur=sta.top();
sta.pop();
color[cur]=sig;
vis[cur]=false;
if(cur==u){break;}
}
}
}
queue<int>q;
int du[maxn];
int num[maxn];
void top_sort(){
int cur = 0;
while (!q.empty()){
int tmp = q.front();
// fuck(tmp)
q.pop();
num[tmp]=++cur;
for(int k=Headt[tmp];~k;k=et[k].Next){
// fuck(k)
du[et[k].v]--;
if(du[et[k].v]==0){
q.push(et[k].v);
}
}
}
}
int main() {
ios::sync_with_stdio(true);
#ifndef ONLINE_JUDGE
freopen("in.txt", "r", stdin);
#endif
memset(Head,-1,sizeof(Head));
memset(Headt,-1,sizeof(Headt));
scanf("%d",&n);
for(int i=1;i<=n;i++){
scanf("%s",s);
a[i].st = cal();
scanf("%s",s);
a[i].ed = cal();
scanf("%d",&a[i].d);
for(int j=1;j<i;j++){
//开始与开始相冲突
if(check(a[i].st,a[j].st,a[i].d,a[j].d)){
add_edge(point1(i),point2(j));
add_edge(point1(j),point2(i));
}if(check(a[i].st,a[j].ed-a[j].d,a[i].d,a[j].d)){
add_edge(point1(i),point1(j));
add_edge(point2(j),point2(i));
}if(check(a[i].ed-a[i].d,a[j].st,a[i].d,a[j].d)){
add_edge(point1(j),point1(i));
add_edge(point2(i),point2(j));
}if(check(a[i].ed-a[i].d,a[j].ed-a[j].d,a[i].d,a[j].d)){
add_edge(point2(i),point1(j));
add_edge(point2(j),point1(i));
}
}
}
for(int i=1;i<=n*2;i++){
if(!dfn[i])Tarjan(i);
}
bool flag=true;
for(int i=1;i<=n;i++){
if(color[point1(i)]==color[point2(i)]){
flag=false;
}
}
if(flag){
printf("YES\n");
for(int i=1;i<=2*n;i++){
for(int k=Head[i];~k;k=e[k].Next){
if(color[i]!=color[e[k].v]){
add_edget(color[i],color[e[k].v]);
du[color[e[k].v]]++;
}
}
}
for(int i=1;i<=sig;i++){
if(du[i]==0){
q.push(i);
}
}
top_sort();
for(int i=1;i<=n;i++){
if(num[color[point1(i)]]>num[color[point2(i)]]){
printf("%02d:%02d ",a[i].st/60,a[i].st%60);
a[i].st+=a[i].d;
printf("%02d:%02d\n",a[i].st/60,a[i].st%60);
}else{
a[i].ed-=a[i].d;
printf("%02d:%02d ",a[i].ed/60,a[i].ed%60);
a[i].ed+=a[i].d;
printf("%02d:%02d\n",a[i].ed/60,a[i].ed%60);
}
}
}else{
printf("NO\n");
}
return 0;
}
Priest John's Busiest Day (2-sat)的更多相关文章
- 图论(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 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 ...
- 【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 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 ...
- POJ 3683 Priest John's Busiest Day(2-SAT 并输出解)
Description John is the only priest in his town. September 1st is the John's busiest day in a year b ...
- POJ3683 Priest John's Busiest Day(2-SAT)
Time Limit: 2000MS Memory Limit: 65536K Total Submissions: 11049 Accepted: 3767 Special Judge ...
- HDU 2491 Priest John's Busiest Day(贪心)(2008 Asia Regional Beijing)
Description John is the only priest in his town. October 26th is the John's busiest day in a year be ...
随机推荐
- FastAdmin 自学教程 - 目录(持续更新)(2019-10-11)
FastAdmin 自学教程 - 目录 本自学教程将不定期更新. 了解 FastAdmin FastAdmin 开发第 1 天:了解 FastAdmin 框架 FastAdmin 开发第 2 天:安装 ...
- 【C++】判断一个图是否有环 无向图 有向图(转载)
没有找到原文出处,请参考一下链接: http://www.cnblogs.com/hiside/archive/2010/12/01/1893878.html http://topic.csdn.ne ...
- 【JZOJ4824】【NOIP2016提高A组集训第1场10.29】配对游戏
题目描述 流行的跳棋游戏是在一个有m*n个方格的长方形棋盘上玩的.棋盘起初全部被动物或障碍物占满了.在一个方格中,'X'表示一个障碍物,一个'0'-'9'的个位数字表示一个不同种类的动物,相同的个位数 ...
- 智能算法之Matlab实现(1)——遗传算法(1)
遗传算法的过程在这里先不介绍了,可能在接下来的几篇文章会介绍,这里介绍些实用的. (1)Sheffield遗传算法工具箱的安装 我共享了下修改过文件名和后缀名的原版工具箱,地址为:http://pan ...
- ios7.1安装提示"无法安装应用程序 由于证书无效"的解决方式二(dropbox被封项目转移到Appharbor上)
6月18日起dropbox被天朝封了(这个真是无力吐槽),而ios7.1要求使用ssl安全连接,则须要又一次找到一个支持https的免费server. Appharbor是个不错的选择,操作简单.此外 ...
- vs code python保存时pylint提示"Unable to import 'flask'"
在配置vscode python开发环境时,编写如下代码并保存时,会提示Unable to import 'flask' from flask import Flask app = Flask(__n ...
- js日期拓展方法
最近项目中使用了大量关于日期的操作遂将其整理如下: /** * 格式化日期 * @param {String} fmt [日期类型 默认为年月日(yyyy-MM-dd)] */ Date.protot ...
- C++第5次作业
运算符重载 定义 - 运算符重载是对已有的运算符赋予多重含义,使同一个运算符作用于不同类型的数据时导致不同行为. 运算符重载规则 - C++运算符除了少数几个,全都可以重载,且只能重载C++已有的运算 ...
- logging.basicConfig函数
在UI自动化应用中,经常会出错,打log就是一个很重要的环节,python的logging.basicConfig函数 真是既方便,又简单,每次粘贴到用例前,就可以打log了. logging模块是 ...
- hdu 3374 String Problem (字符串最小最大表示 + KMP求循环节)
Problem - 3374 KMP求循环节. http://www.cnblogs.com/wuyiqi/archive/2012/01/06/2314078.html 循环节推导的证明相当 ...