[POJ 3683] Priest John‘s Busiest Day | 2-SAT +Tarjan缩点跑拓扑排序

简介: 题意:给出n个婚礼,每个婚礼有个开始的时间和结束的时间,在婚礼期间,要举行持续时间为D的活动,这个活动只能在婚礼期间的前D时间内举行,或者是在婚礼期间的后D时间内举行,问能否安排一种方式使得能够参与n个婚礼的活动思路:每个活动有两个选择,而且还要满足一定的条件,这显然是一个2-SAT问题方法:2-SAT + Tarjan首先根据两种选择是否首尾冲突进行连边,如果冲突,则连对立的边

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 to Ti). 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


题意:


给出n个婚礼,每个婚礼有个开始的时间和结束的时间,在婚礼期间,要举行持续时间为D的活动,这个活动只能在婚礼期间的前D时间内举行,或者是在婚礼期间的后D时间内举行,问能否安排一种方式使得能够参与n个婚礼的活动


思路:


每个活动有两个选择,而且还要满足一定的条件,这显然是一个2-SAT问题

方法:

2-SAT + Tarjan


首先根据两种选择是否首尾冲突进行连边,如果冲突,则连对立的边

for(int i=1; i<=n; i++) {
    for(int j=1; j<=n; j++) {
      if(i == j) continue;
      if(notFit(i,j)) add(i,j+n);
      if(notFit(i,j+n)) add(i,j);
      if(notFit(i+n,j)) add(i+n,j+n);
      if(notFit(i+n,j+n)) add(i+n,j);
    }
  }


根据建立的图,进行缩点,缩点后可以得到强连通分量的数量以及每个点属于哪一个强连通分量

求完SCC之后,判断两种情况是否冲突(在同一个SCC之内),如果冲突就直接输出“NO”即可

在判断的过程中同时记录对立的点所在的联通块的编号

其余的均为有解(“YES”)的情况


for(int i=1; i<=n; i++) {
    if(pos[i] == pos[i+n]) {
      puts("NO");
      return 0;
    }
    op[pos[i]] = pos[i+n];
    op[pos[i+n]] = pos[i];
  }


然后建立反图统计入度,跑拓扑排序,在拓扑排序的过程当中,如果对于一个没有被标记的点就染色为颜色1,将其对立的点染色为0


void topoSort() {
  int lim = 2*n;
  for(int i=1; i<=lim; i++) {
    for(int j=head[i]; ~j; j=e[j].nex) {
      int u = i,v = e[j].to;
      if(pos[u] == pos[v]) continue;
      edg[pos[v]].push_back(pos[u]);
      ++ indeg[pos[u]];///indeg
    }
  }
  for(int i=1; i<=cntSCC; i++) {
    if(indeg[i] == 0) que.push(i);
  }
  while(que.size()) {
    int u = que.front();
    que.pop();
    if(col[u] == -1) {
      col[u] = 1;
      col[op[u]] = 0;
    }
    for(int i=edg[u].size()-1; i>=0; i--) {
      int v = edg[u][i];
      indeg[v] --;
      if(indeg[v] == 0) {
        que.push(v);
      }
    }
  }
}


最后遍历n个点的染色信息,给出对应的输出即可:


ac_code:


int n,m;
int cnt,head[maxn << 1];
struct node {
  int to,nex;
} e[2000007];
void init() {
  cnt = 0;
  Clear(head,-1);
}
void add(int u,int v) {
  e[cnt].to = v;
  e[cnt].nex = head[u];
  head[u] = cnt ++;
}
int dfn[maxn<<1],low[maxn<<1],dfc,cntSCC,pos[maxn<<1];
stack<int> stk;
bool inStk[maxn<<1];
void Tarjan(int u) {
  dfn[u] = low[u] = ++dfc;
  stk.push(u);
  inStk[u] = true;
  for(int i=head[u]; ~i; i=e[i].nex) {
    int to = e[i].to;
    if(!dfn[to]) {
      Tarjan(to);
      low[u] = min(low[u],low[to]);
    } else if(inStk[to]) {
      low[u] = min(low[u],dfn[to]);
    }
  }
  if(low[u] == dfn[u]) {
    int tp;
    ++ cntSCC;
    do {
      tp = stk.top();
      stk.pop();
      pos[tp] = cntSCC;
      inStk[tp] = false;
    } while(tp != u);
  }
}
int a[maxn<<1],b[maxn<<1],op[maxn<<1];
int sth,stm,edh,edm,l;
bool notFit(int x,int y) {
  if(b[x] <= a[y] || b[y] <= a[x]) return false;
  return true;
}
queue<int> que;
vector<int> edg[maxn<<1];
int indeg[maxn<<1],col[maxn<<1];
void topoSort() {
  int lim = 2*n;
  for(int i=1; i<=lim; i++) {
    for(int j=head[i]; ~j; j=e[j].nex) {
      int u = i,v = e[j].to;
      if(pos[u] == pos[v]) continue;
      edg[pos[v]].push_back(pos[u]);
      ++ indeg[pos[u]];///indeg
    }
  }
  for(int i=1; i<=cntSCC; i++) {
    if(indeg[i] == 0) que.push(i);
  }
  while(que.size()) {
    int u = que.front();
    que.pop();
    if(col[u] == -1) {
      col[u] = 1;
      col[op[u]] = 0;
    }
    for(int i=edg[u].size()-1; i>=0; i--) {
      int v = edg[u][i];
      indeg[v] --;
      if(indeg[v] == 0) {
        que.push(v);
      }
    }
  }
}
int main() {
  init();
  n = read;
  for(int i=1; i<=n; i++) {
    scanf("%d:%d %d:%d %d",&sth,&stm,&edh,&edm,&l);
    a[i] = sth * 60 + stm;
    b[i] = a[i] + l;
    b[i+n] = edh * 60 + edm;
    a[i+n] = b[i+n] - l;
  }
  for(int i=1; i<=n; i++) {
    for(int j=1; j<=n; j++) {
      if(i == j) continue;
      if(notFit(i,j)) add(i,j+n);
      if(notFit(i,j+n)) add(i,j);
      if(notFit(i+n,j)) add(i+n,j+n);
      if(notFit(i+n,j+n)) add(i+n,j);
    }
  }
  Clear(dfn,0);
  Clear(low,0);
  for(int i=1; i<=2*n; i++) {
    if(!dfn[i]) Tarjan(i);
  }///
  for(int i=1; i<=n; i++) {
    if(pos[i] == pos[i+n]) {
      puts("NO");
      return 0;
    }
    op[pos[i]] = pos[i+n];
    op[pos[i+n]] = pos[i];
  }
  Clear(col,-1);
  Clear(indeg,0);
  topoSort();
  puts("YES");
  for(int i=1; i<=n; i++) {
    if(col[pos[i]]) {
      printf("%02d:%02d %02d:%02d\n",a[i]/60,a[i]%60,b[i]/60,b[i]%60);
    } else printf("%02d:%02d %02d:%02d\n",a[i+n]/60,a[i+n]%60,b[i+n]/60,b[i+n]%60);
  }
  return 0;
}
/**
2
08:00 09:00 30
08:15 09:00 20
**/
目录
相关文章
|
网络协议 安全 Linux
linux系统安全及应用——端口扫描
linux系统安全及应用——端口扫描
214 0
|
人工智能 IDE Java
AI 代码工具大揭秘:提高编程效率的必备神器!
【10月更文挑战第1天】近年来,人工智能得到了迅猛的发展,并在各行各业都得到了广泛应用。尤其是近两年来,AI开发工具逐渐成为开发者们的新宠,其中 GitHub Copilot 更是引发了无限可能性的探索。
988 9
AI 代码工具大揭秘:提高编程效率的必备神器!
|
机器学习/深度学习 人工智能 算法
为什么大模型训练需要GPU,以及适合训练大模型的GPU介绍
为什么大模型训练需要GPU,以及适合训练大模型的GPU介绍
1588 1
|
数据可视化 算法
R语言主成分分析(PCA)葡萄酒可视化:主成分得分散点图和载荷图
R语言主成分分析(PCA)葡萄酒可视化:主成分得分散点图和载荷图
|
数据采集 监控 Java
|
弹性计算 大数据 Linux
阿里云服务体验
我是一名电子信息专业的学生,目前研一。在假期的时候通过朋友推荐了解到阿里云的飞天加速计划能够领取免费的学生服务器进行学习,初次试用时间为两周。领取前我在阿里云上完成了学生认证和新手预备训练营,并且学习使用了阿里云的镜像快照等功能。
|
Java 应用服务中间件 Spring
SpringBoot自定义banner,配置端口号和项目路径(二)
本文主要讲解点:1. 自定义banner2. 配置Tomcat服务器的端口号和路径3. 配置jetty服务器4. 配置文件的优化级
577 0
SpringBoot自定义banner,配置端口号和项目路径(二)
|
4天前
|
人工智能 自然语言处理 JavaScript
2026年Windows+Ollama本地部署OpenClaw保姆级教程:本地AI Agent+阿里云上快速搭建
2026年OpenClaw凭借本地部署、私有化运行的特性,成为打造个人智能体的核心工具,而Ollama作为轻量级本地大模型管理工具,能让OpenClaw摆脱对云端大模型的依赖,实现**本地推理、数据不泄露、全流程私有化**的智能体验。本文基于Windows 11系统,从硬件环境准备、Ollama安装与模型定制、OpenClaw部署配置、技能扩展到常见问题排查,打造保姆级本地部署教程,同时补充阿里云OpenClaw(Clawdbot)快速部署步骤,兼顾本地私有化需求与云端7×24小时运行需求,文中所有代码命令均可直接复制执行,确保零基础用户也能快速搭建属于自己的本地智能体。
5937 32

热门文章

最新文章