题意
n对夫妻要结婚,第i对夫妻结婚的婚礼持续时间为[Si, Ti],他们会举行一个仪式,仪式时间为Di,这个仪式只能举行在开头或者结尾举行,要么[Si, Si+Di],要么[Ti-Di, Ti],然而举行仪式的牧师只有一个,问牧师能否举行完所有仪式
按输入顺序输出方案
手动翻译
Sol
\(2-SAT\)输出一组可行解
这个很烦
\(Tarjan\)缩点成\(DAG\)后再拓扑排序+染色
只传递不选的标记
# include <iostream>
# include <stdio.h>
# include <stdlib.h>
# include <string.h>
# include <math.h>
# include <algorithm>
# include <queue>
# define RG register
# define IL inline
# define Fill(a, b) memset(a, b, sizeof(a))
using namespace std;
typedef long long ll;
const int _(2005);
const int __(2e6 + 5);
IL int Input(){
RG int x = 0, z = 1; RG char c = getchar();
for(; c < '0' || c > '9'; c = getchar()) z = c == '-' ? -1 : 1;
for(; c >= '0' && c <= '9'; c = getchar()) x = (x << 1) + (x << 3) + (c ^ 48);
return x * z;
}
int n, first[_], head[_], cnt, num, s[_], t[_], d[_], cho[_];
int S[_], vis[_], dfn[_], low[_], Index, col[_], deg[_], oth[_];
struct Edge{
int to, next;
} edge[__], dag[__];
queue <int> Q;
IL void Add(RG int u, RG int v){
edge[cnt] = (Edge){v, first[u]}, first[u] = cnt++;
}
IL void Add_DAG(RG int u, RG int v){
dag[cnt] = (Edge){v, head[u]}, head[u] = cnt++, ++deg[v];
}
IL void Tarjan(RG int u){
vis[u] = 1, dfn[u] = low[u] = ++Index, S[++S[0]] = u;
for(RG int e = first[u]; e != -1; e = edge[e].next){
RG int v = edge[e].to;
if(!dfn[v]) Tarjan(v), low[u] = min(low[u], low[v]);
else if(vis[v]) low[u] = min(low[u], dfn[v]);
}
if(dfn[u] != low[u]) return;
RG int v = S[S[0]--]; col[v] = ++num, vis[v] = 0;
while(v != u) v = S[S[0]--], col[v] = num, vis[v] = 0;
}
IL void Dfs(RG int u){
if(cho[u] != -1) return;
cho[u] = 0;
for(RG int e = head[u]; e != -1; e = dag[e].next) Dfs(dag[e].to);
}
IL int GetTime(){
return Input() * 60 + Input();
}
IL void OutTime(RG int x){
printf("%.2d:%.2d ", x / 60, x % 60);
}
IL int Cross(RG int l1, RG int r1, RG int l2, RG int r2){
if(l1 > l2) swap(l1, l2), swap(r1, r2);
return r1 > l2;
}
int main(RG int argc, RG char* argv[]){
n = Input(), Fill(first, -1), Fill(head, -1), Fill(cho, -1);
for(RG int i = 1; i <= n; ++i)
s[i] = GetTime(), t[i] = GetTime(), d[i] = Input();
for(RG int i = 1; i < n; ++i)
for(RG int j = i + 1; j <= n; ++j){
if(Cross(s[i], s[i] + d[i], s[j], s[j] + d[j])) Add(i, j + n), Add(j, i + n);
if(Cross(s[i], s[i] + d[i], t[j] - d[j], t[j])) Add(i, j), Add(j + n, i + n);
if(Cross(t[i] - d[i], t[i], s[j], s[j] + d[j])) Add(i + n, j + n), Add(j, i);
if(Cross(t[i] - d[i], t[i], t[j] - d[j], t[j])) Add(i + n, j), Add(j + n, i);
}
RG int tmp = n << 1; cnt = 0;
for(RG int i = 1; i <= tmp; ++i) if(!dfn[i]) Tarjan(i);
for(RG int i = 1; i <= n; ++i){
if(col[i] == col[i + n]) return puts("NO"), 0;
oth[col[i]] = col[i + n], oth[col[i + n]] = col[i];
}
puts("YES");
for(RG int i = 1; i <= tmp; ++i)
for(RG int e = first[i]; e != -1; e = edge[e].next)
if(col[i] != col[edge[e].to]) Add_DAG(col[edge[e].to], col[i]);
for(RG int i = 1; i <= num; ++i) if(!deg[i]) Q.push(i);
while(!Q.empty()){
RG int u = Q.front(); Q.pop();
if(cho[u] != -1) continue;
cho[u] = 1, Dfs(oth[u]);
for(RG int e = head[u]; e != -1; e = dag[e].next)
if(!--deg[dag[e].to]) Q.push(dag[e].to);
}
for(RG int i = 1; i <= n; ++i){
if(cho[col[i]]) OutTime(s[i]), OutTime(s[i] + d[i]);
else OutTime(t[i] - d[i]), OutTime(t[i]);
puts("");
}
return 0;
}
Poj3683:Priest John's Busiest Day
原文地址:https://www.cnblogs.com/cjoieryl/p/8463664.html
时间: 2024-10-26 09:39:41