关闭

poj 2723 Get Luffy Out

标签: conflictstructc
128人阅读 评论(0) 收藏 举报
分类:

类型:2-sat

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

来源:Beijing 2005

思路:2-sat判定问题。将一把钥匙看做两个点,不取为i, 取为i + 2n。

(1)对于每一对钥匙(u, v),二者最多只能选一个,故有选择v + 2n, u必选,选择u + 2n,v必选。

(2)对于每一个们上的锁(u, v),二者至少选一个,固有选择u,v + 2n必选,选择v,u + 2n必选。

构造好图后,判断解的存在性。

二分答案

// poj 2723 Get Luffy Out
// re ac 484K 47MS
#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 = 5010;
const int M = 40010;

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];
int cnt1, cnt2;
int head1[N];
int door[N][2], key[N][2];
int top[N], cnt;
struct node {
    int s, e;
    int len;
}c[N];
struct edge1 {
    int v, nxt;
}E1[M];

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

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 solve() {
    int i, j, mid, l = 1, r = m;
    int tmp_cnt = cnt1;
    while(l <= r) {
        mid = (l + r) >> 1;
        cnt1 = tmp_cnt;
        cnt1 = step = t = 0;
        CLR(head1, -1);
        CLR(vis, 0);
        CLR(inStack, 0);
        FORE(i, 1, n) {
            addedge1(key[i][0] + 2 * n, key[i][1]);
            addedge1(key[i][1] + 2 * n, key[i][0]);
        }
        FORE(i, 1, mid) {
            addedge1(door[i][0], door[i][1] + 2 * n);
            addedge1(door[i][1], door[i][0] + 2 * n);
        }
        while(!st.empty())
            st.pop();
        FORE(i, 1, 4 * n)
            if(!vis[i])
                tarjan(i);
        bool sign = false;
        FORE(i, 1, 2 * n)
            if(belong[i] == belong[i + 2 * n]) {
                sign = true;
                break;
            }
        if(sign)
            r = mid - 1;
        else
            l = mid + 1;
    }
    printf("%d\n", l - 1);
}

int main() {
    int u, v, i, j;
    while(scanf("%d %d", &n, &m) != EOF, n || m) {
        FORE(i, 1, n) {
            scanf("%d %d", &key[i][0], &key[i][1]);
            ++key[i][0], ++key[i][1];
        }
        FORE(i, 1, m) {
            scanf("%d %d", &door[i][0], &door[i][1]);
            ++door[i][0], ++door[i][1];
        }
        solve();
    }
    return 0;
}




0
0

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