poj 3683 Priest John's Busiest Day

原创 2012年03月27日 10:42:54

类型: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;
}





POJ3683——Priest John's Busiest Day

Description John is the only priest in his town. September 1st is the John's busiest day in a y...

POJ 3683 Priest John's Busiest Day (2-SAT+输出可行解)

题目地址:POJ 3683 第一次做需要输出可行解的题目。。。大体思路是先用强连通来判断是否有可行解,然后用逆序建图,用拓扑排序来进行染色,然后输出可行解。具体思路见传送门 因为判断的时候少写了一...

【POJ 3683】Priest John's Busiest Day

【POJ 3683】Priest John's Busiest Day 2-SAT可行性判断+方案输出模板题~ UPD:对2—SAT的理解以及代码完善~...

【POJ】3683 Priest John's Busiest Day 2-sat

传送门: 题目分析:2-satbing

poj 3683 Priest John's Busiest Day 【2-sat 经典建图输出一组解】【有向图tarjan + 反向拓扑 + 染色】

Priest John's Busiest Day Time Limit: 2000MS   Memory Limit: 65536K Total Submissions: 8652   ...

2-SAT——5.0(poj3683 Priest John's Busiest Day)

poj3683 Priest John's Busiest Day 题意:一个小镇里面只有一个牧师,现在有些新人要结婚,需要牧师分别去主持一个仪式,给出每对新人婚礼的开始时间 s 和结束时间 t ,...

POJ--3683[Priest John's Busiest Day] 2-SAT第四题

题目描述:有n个婚礼,每个婚礼有起始时间si,结束时间ti,还有一个主持时间ti,ti必须安排在婚礼的开始或者结束,主持由祭祀来做,但是只有一个祭祀,所以各个婚礼的主持时间不能重复,问你有没有可能正常...

poj3683Priest John's Busiest Day【2-sat二选一输出】

Description John is the only priest in his town. September 1st is the John's busiest day in a y...

POJ 3683 - Priest John's Busiest Day(2-SAT)

题目; http://poj.org/problem?id=3683 当模版. AC. #include #include #include #include #include ...

POJ3683 Priest John's Busiest Day(神父约翰的忙日)题解(2-SAT及布尔方程运用)

挑战程序设计竞赛p326例题 poj3683 (奶牛呢) 如果你还不了解布尔方程和蕴含式,请先移步蕴含式是什么和布尔方程简介,运算法则 有些很巧妙的地方和技巧、想要总结一下。 首先这题我们考...
  • DareXK
  • DareXK
  • 2017年03月11日 00:11
  • 329
内容举报
返回顶部
收藏助手
不良信息举报
您举报文章:poj 3683 Priest John's Busiest Day
举报原因:
原因补充:

(最多只允许输入30个字)