题面

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

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

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

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

  4. poj 3686 Priest John's Busiest Day

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

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

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

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

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

  7. Priest John's Busiest Day(POJ 3683)

    原题如下: Priest John's Busiest Day Time Limit: 2000MS   Memory Limit: 65536K Total Submissions: 12162   ...

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

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

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

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

随机推荐

  1. day39-Spring 05-Spring的AOP:不带有切点的切面

    Spring底层的代理的实现: 不带切点的切面是对类里面的所有的方法都进行拦截. 做Spring AOP的开发需要两个包:一个是AOP的包,一个是AOP联盟的包(因为规范是由AOP联盟提出来的). 用 ...

  2. 用预编译包安装zabbix-agent

    如果主机无法上网,安装rpm又缺少依赖时,可以通过预编译包进行安装zabbix-agent,下载地址 https://www.zabbix.com/download 下载后,执行如下命令: wget ...

  3. oracle显示和设置环境变量

    概述:可以用来控制输出的各种格式. (1)linesize  用于控制每行显示多少个字符,默认80个字符. 基本用法:set linesize 字符数 aql>show linesize sql ...

  4. Creating a Pulsing Circle Animation

    原文 https://www.kirupa.com/animations/creating_pulsing_circle_animation.htm Outside of transitions th ...

  5. @atcoder - AGC035E@ Develop

    目录 @description@ @solution@ @accepted code@ @details@ @description@ 给定初始集合为 1 ~ N 的全集,并给定一个 K. 每次对于当 ...

  6. 15-8 pymysql的使用

    一 安装pymysql模块 1 pycharm安装 file-setting如图:然后点加号,搜索pymsql即可,点击安装 2 pip 安装 pip3 install pymysql 二  连接数据 ...

  7. etcd 在超大规模数据场景下的性能优化

    作者 | 阿里云智能事业部高级开发工程师 陈星宇(宇慕) 概述 etcd是一个开源的分布式的kv存储系统, 最近刚被cncf列为沙箱孵化项目.etcd的应用场景很广,很多地方都用到了它,例如kuber ...

  8. 解决pip is configured with locations that require TLS/SSL问题

    python3.7安装, 解决pip is configured with locations that require TLS/SSL问题1.安装相关依赖 yum install gcc libff ...

  9. Libev源码分析01:Libev中的监视器结构(C结构体实现继承)

    在Libev的源码中,用到了一种用C实现类似C++中继承的技巧,主要是用宏和结构体实现. 在Libev中,最关键的数据结构就是各种监视器,比如IO监视器,信号监视器等等.这些监视器的多数成员都是一样的 ...

  10. hdu 1532 Drainage Ditches(最大流模板题)

    Drainage Ditches Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others) ...