[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
**/
目录
相关文章
|
9月前
|
存储 索引
题目----LeeCode热题100--1 两数之和
题目----LeeCode热题100--1 两数之和
56 1
|
9月前
|
Java
hdu-2112-HDU Today(dijkstra + map)
hdu-2112-HDU Today(dijkstra + map)
33 0
|
网络架构
POJ 3250 Bad Hair Day、POJ 2796 Feel Good(单调栈)
POJ 3250 Bad Hair Day、POJ 2796 Feel Good(单调栈)
UVa302 - John's trip(并查集、欧拉回路、DFS)
UVa302 - John's trip(并查集、欧拉回路、DFS)
77 0
|
人工智能 Java
HDU-敌兵布阵(线段树 || 树状数组)
HDU-敌兵布阵(线段树 || 树状数组)
104 0
洛谷P2871-[USACO07DEC]Charm Bracelet S(01背包模板题)
洛谷P2871-[USACO07DEC]Charm Bracelet S(01背包模板题)
洛谷P2871-[USACO07DEC]Charm Bracelet S(01背包模板题)
洛谷P2722-[USACO3.1]总分 Score Inflation(完全背包)
洛谷P2722-[USACO3.1]总分 Score Inflation(完全背包)
洛谷P2722-[USACO3.1]总分 Score Inflation(完全背包)
模板题 + KMP + 求最小循环节 --- HDU 3746 Cyclic Nacklace
Cyclic Nacklace  Problem's Link:  http://acm.hdu.edu.cn/showproblem.php?pid=3746   Mean:  给你一个字符串,让你在后面加尽量少的字符,使得这个字符串成为一个重复串。
1248 0

热门文章

最新文章