关闭

poj 3683 Priest John's Busiest Day

144人阅读 评论(0) 收藏 举报

类型:2-sat【模板】

题目:http://poj.org/problem?id=3683

来源:POJ Founder Monthly Contest – 2008.08.31, Dagger and Facer

思路:本题为需要输出结果的2-sat问题。令i和i + n表示节点i,对于有向边的构造,如果i和j有冲突,那么选择i,j + n必选,选择j, i + n必选,构造两条有向边。其他类似。

然后就是Tarjan求强连通分量,缩点,判断i,i + n是否属于同一强连通分量,如果属于,那么结果为“NO”。

对于存在解的情况,缩点后,构造新图【反向建图】,同时记录相互矛盾的节点,对新图topo排序,记录topo序列,然后染色,对于没有冲突的点染为红色,其他为蓝色。

记录染色情况,最后打印输出。

// poj 3683 Priest John's Busiest Day
// re re ac 12196K 125MS
#include <iostream>
#include <string>
#include <queue>
#include <stack>
#include <algorithm>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <cstring>
using namespace std;

#define FOR(i,a,b) for(i = (a); i < (b); ++i)
#define FORE(i,a,b) for(i = (a); i <= (b); ++i)
#define FORD(i,a,b) for(i = (a); i > (b); --i)
#define FORDE(i,a,b) for(i = (a); i >= (b); --i)
#define CLR(a,b) memset(a,b,sizeof(a))
#define PB(x) push_back(x)

const int N = 5000;
const int M = 4000010;

int low[N], dfn[N];
int belong[N];
bool inStack[N], vis[N];
stack<int> st;
int n, m;
int step, t;
int conflict[N];//缩点后有矛盾的点
int du[N];//入度
int color[N];//1为红色,-1为蓝色,红色输出
int cnt1, cnt2;
int head1[N], head2[N];
int top[N], cnt;
struct node {
    int s, e;
    int len;
}c[N];
struct edge1 {
    int v, nxt;
}E1[M];
struct edge2 {
    int v, nxt;
}E2[M];

void addedge1(int u, int v) {
    E1[cnt1].v = v;
    E1[cnt1].nxt = head1[u];
    head1[u] = cnt1++;
}

void addedge2(int u, int v) {
    E2[cnt2].v = v;
    E2[cnt2].nxt = head2[u];
    head2[u] = cnt2++;
}

void tarjan(int u) {
    int i;
    step++;
    st.push(u);
    low[u] = dfn[u] = step;
    vis[u] = 1;
    inStack[u] = 1;
    for(i = head1[u]; i != -1; i = E1[i].nxt) {
        int x = E1[i].v;
        if(!vis[x]) {
            tarjan(x);
            low[u] = min(low[u], low[x]);
        }
        else if(inStack[x])
            low[u]=min(low[u], dfn[x]);
    }
    if(low[u] == dfn[u]) {
        t++;
        while(1) {
            int x = st.top();
            st.pop();
            belong[x] = t;
            inStack[x] = 0;
            if(x == u)
                break;
        }
    }
}

void init() {
    cnt1 = cnt2 = step = t = 0;
    CLR(head1, -1);
    CLR(head2, -1);
    CLR(du, 0);
    CLR(color, 0);
    CLR(vis, 0);
    CLR(inStack, 0);
}

bool isConflict(int a, int b, int c, int d) {
    if(a >= d || b <= c)
        return false;
    return true;
}

void Build() {
    int i,j;
    init();
    FORE(i, 1, n - 1)
    FORE(j, i + 1, n) {
        if(isConflict(c[i].s, c[i].s + c[i].len, c[j].s, c[j].s + c[j].len)) {
            addedge1(i, j + n);
            addedge1(j, i + n);
        }
        if(isConflict(c[i].s, c[i].s + c[i].len, c[j].e - c[j].len, c[j].e)) {
            addedge1(i, j);
            addedge1(j + n, i + n);
        }
        if(isConflict(c[i].e - c[i].len, c[i].e, c[j].e - c[j].len, c[j].e)) {
            addedge1(i + n, j);
            addedge1(j + n, i);
        }
        if(isConflict(c[i].e - c[i].len, c[i].e, c[j].s, c[j].s + c[j].len)) {
            addedge1(i + n, j + n);
            addedge1(j, i);
        }
    }
}

void Rebuild(){//逆图
    int i, j;

    FORE(i, 1, 2 * n) {
        for(j = head1[i]; j != -1; j = E1[j].nxt) {
            int a = belong[i], b = belong[E1[j].v];
            if(a != b) {
                addedge2(b, a);
                du[a]++;
            }
        }
    }
    FORE(i, 1, n) {
        int a = belong[i], b = belong[i + n];
        conflict[a] = b;//缩点后有矛盾的点
        conflict[b] = a;
    }
}

void topsort() {
    int i,j;

    queue<int> q;
    FORE(i, 1, t)
        if(du[i] == 0)
            q.push(i);
    cnt = 0;
    while(!q.empty()) {
        int x = q.front();
        top[++cnt] = x;
        q.pop();
        for(i = head2[x]; i != -1; i = E2[i].nxt) {
            int tmp = E2[i].v;
            du[tmp]--;
            if(du[tmp] == 0)
                q.push(tmp);
        }
    }
}

void dfs_Blue(int u) {
    int i;
    color[u] = -1;
    for(i = head2[u]; i != -1; i = E2[i].nxt) {
        int x = E2[i].v;
        if(color[x] == 0)
            dfs_Blue(x);
    }
}

void dfs_Red() {
    int i, j;

    FORE(i, 1, cnt) {
        int x = top[i];
        if(color[x] == 0) {
            color[x] = 1;//Red
            dfs_Blue(conflict[x]);//把所有与x有矛盾的点及其子孙染蓝色
        }
    }
}

void output() {
    int i, j, t1, t2;

    printf("YES\n");
    FORE(i, 1, n) {
        int x = belong[i], y = belong[i + n];
        if(color[x] == 1) {
            t1 = c[i].s;
            t2 = c[i].s + c[i].len;
        }
        if(color[y] == 1) {
            t1 = c[i].e - c[i].len;
            t2 = c[i].e;
        }
        printf("%02d:%02d %02d:%02d\n", t1 / 60, t1 % 60, t2 / 60, t2 % 60);
    }
}

void solve() {
    int i, j;
    FORE(i, 1, 2 * n)
        if(!vis[i])
            tarjan(i);
    FORE(i, 1, n)
        if(belong[i] == belong[i + n]) {
            printf("NO\n");
            return ;
        }
    Rebuild();
    topsort();
    dfs_Red();
    output();
}

int main() {
    while(scanf("%d", &n) != EOF) {
        int i, j;
        FORE(i, 1, n) {
            int h1, m1, h2, m2;
            scanf("%d:%d %d:%d %d", &h1, &m1, &h2, &m2, &c[i].len);
            c[i].s = h1 * 60 + m1;
            c[i].e = h2 * 60 + m2;
        }
        Build();
        solve();
    }
    return 0;
}





0
0

查看评论
* 以上用户言论只代表其个人观点,不代表CSDN网站的观点或立场
    个人资料
    • 访问:54224次
    • 积分:1744
    • 等级:
    • 排名:千里之外
    • 原创:128篇
    • 转载:3篇
    • 译文:0篇
    • 评论:1条
    文章分类
    最新评论