POJ 3683 Priest John's Busiest Day (算竞进阶习题)
2-SAT
可以把每一次仪式看成变量,0/1的取值分别为开头举行和结尾举行。
转换为2-SAT接受的命题,就是看某一次仪式中有没有重合的时间段,有的话,就按照不冲突的形式连有向边。
然后跑tarjan就行啦,我们把时间全部转成分钟方便处理。。
#include <iostream>
#include <cstdio>
#include <stack>
#include <cstring>
#define INF 0x3f3f3f3f
#define full(a, b) memset(a, b, sizeof a)
using namespace std;
typedef long long ll;
inline int lowbit(int x){ return x & (-x); }
inline int read(){
int X = 0, w = 0; char ch = 0;
while(!isdigit(ch)) { w |= ch == '-'; ch = getchar(); }
while(isdigit(ch)) X = (X << 3) + (X << 1) + (ch ^ 48), ch = getchar();
return w ? -X : X;
}
inline int gcd(int a, int b){ return a % b ? gcd(b, a % b) : b; }
inline int lcm(int a, int b){ return a / gcd(a, b) * b; }
template<typename T>
inline T max(T x, T y, T z){ return max(max(x, y), z); }
template<typename T>
inline T min(T x, T y, T z){ return min(min(x, y), z); }
template<typename A, typename B, typename C>
inline A fpow(A x, B p, C lyd){
A ans = 1;
for(; p; p >>= 1, x = 1LL * x * x % lyd)if(p & 1)ans = 1LL * x * ans % lyd;
return ans;
}
const int N = 5000;
int n, cnt, k, tot, head[N], S[N], T[N], D[N], dfn[N], low[N], scc[N], val[N];
bool ins[N];
struct Edge { int v, next; } edge[N*N];
stack<int> st;
void addEdge(int a, int b){
edge[cnt].v = b, edge[cnt].next = head[a], head[a] = cnt ++;
}
bool overlap(int a, int b, int c, int d){
return (a >= c && a < d) || (b > c && b <= d) || (a <= c && b >= d);
}
void build(){
while(!st.empty()) st.pop();
full(head, -1), full(dfn, 0), full(low, 0);
full(scc, 0), full(val, 0);
cnt = k = tot = 0;
}
void tarjan(int s){
dfn[s] = low[s] = ++k;
ins[s] = true;
st.push(s);
for(int i = head[s]; i != -1; i = edge[i].next){
int u = edge[i].v;
if(!dfn[u]){
tarjan(u);
low[s] = min(low[s], low[u]);
}
else if(ins[u]) low[s] = min(low[s], dfn[u]);
}
if(dfn[s] == low[s]){
tot ++;
int cur;
do{
cur = st.top(); st.pop();
ins[cur] = false;
scc[cur] = tot;
}while(cur != s);
}
}
int main(){
while(~scanf("%d", &n)){
build();
int a, b, c, d;
for(int i = 1; i <= n; i ++){
scanf("%d:%d %d:%d %d", &a, &b, &c, &d, &D[i]);
S[i] = a * 60 + b, T[i] = c * 60 + d;
}
for(int i = 1; i < n; i ++){
for(int j = i + 1; j <= n; j ++){
if(overlap(S[i], S[i] + D[i], S[j], S[j] + D[j]))
addEdge(i, j + n), addEdge(j, i + n);
if(overlap(S[i], S[i] + D[i], T[j] - D[j], T[j]))
addEdge(i, j), addEdge(j + n, i + n);
if(overlap(T[i] - D[i], T[i], S[j], S[j] + D[j]))
addEdge(i + n, j + n), addEdge(j, i);
if(overlap(T[i] - D[i], T[i], T[j] - D[j], T[j]))
addEdge(i + n, j), addEdge(j + n, i);
}
}
for(int i = 1; i <= 2 * n; i ++){
if(!dfn[i]) tarjan(i);
}
bool good = true;
for(int i = 1; i <= n; i ++){
if(scc[i] == scc[i + n]){
good = false;
break;
}
}
if(!good) printf("NO\n");
else{
printf("YES\n");
for(int i = 1; i <= n; i ++){
val[i] = (scc[i] > scc[i + n]);
}
for(int i = 1; i <= n; i ++){
if(!val[i]) printf("%02d:%02d %02d:%02d\n", S[i] / 60, S[i] % 60, (S[i] + D[i]) / 60, (S[i] + D[i]) % 60);
else printf("%02d:%02d %02d:%02d\n", (T[i] - D[i]) / 60, (T[i] - D[i]) % 60, T[i] / 60, T[i] % 60);
}
}
}
return 0;
}