zoukankan      html  css  js  c++  java
  • poj2723Get Luffy Out

    Get Luffy Out
    Time Limit: 2000MS   Memory Limit: 65536K
         

    Description

    Ratish is a young man who always dreams of being a hero. One day his friend Luffy was caught by Pirate Arlong. Ratish set off at once to Arlong's island. When he got there, he found the secret place where his friend was kept, but he could not go straight in. He saw a large door in front of him and two locks in the door. Beside the large door, he found a strange rock, on which there were some odd words. The sentences were encrypted. But that was easy for Ratish, an amateur cryptographer. After decrypting all the sentences, Ratish knew the following facts: 

    Behind the large door, there is a nesting prison, which consists of M floors. Each floor except the deepest one has a door leading to the next floor, and there are two locks in each of these doors. Ratish can pass through a door if he opens either of the two locks in it. There are 2N different types of locks in all. The same type of locks may appear in different doors, and a door may have two locks of the same type. There is only one key that can unlock one type of lock, so there are 2N keys for all the 2N types of locks. These 2N keys were divided into N pairs, and once one key in a pair is used, the other key will disappear and never show up again. 

    Later, Ratish found N pairs of keys under the rock and a piece of paper recording exactly what kinds of locks are in the M doors. But Ratish doesn't know which floor Luffy is held, so he has to open as many doors as possible. Can you help him to choose N keys to open the maximum number of doors?

    Input

    There are several test cases. Every test case starts with a line containing two positive integers N (1 <= N <= 210) and M (1 <= M <= 211) separated by a space, the first integer represents the number of types of keys and the second integer represents the number of doors. The 2N keys are numbered 0, 1, 2, ..., 2N - 1. Each of the following N lines contains two different integers, which are the numbers of two keys in a pair. After that, each of the following M lines contains two integers, which are the numbers of two keys corresponding to the two locks in a door. You should note that the doors are given in the same order that Ratish will meet. A test case with N = M = 0 ends the input, and should not be processed.

    Output

    For each test case, output one line containing an integer, which is the maximum number of doors Ratish can open.

    Sample Input

    3 6
    0 3
    1 2
    4 5
    0 1
    0 2
    4 1
    4 2
    3 5
    2 2
    0 0
    

    Sample Output

    4

    题意:你有N对钥匙,对应着2*N把不同种类的锁。对于每一对钥匙a和b,若你使用了a,那么b消失不会出现,若你使用了b则a消失。给你M个门,每个门上有两个锁
    (可能一样,也可能不一样),你只要打开其中一把锁就可以打开这个门。现在要求必须按输入数据的顺序来依次打开门,问你用这N对钥匙最多能打开多少门。
    题解:2SAT, 二分查找区间1-M,每次重建图tarjan求SCC并判断可行性。
    对于每扇门上的钥匙a和b,1,若a,b不同,则有!Aa -> Ab 和 !Ab -> Aa;2,若a,b相同,则有!Aa -> Aa即必选a。

    开始我是直接用 key1 +n 表示!key1, 结果发现时N对, 所以后面用i*2+1表示

    #include<cstdio>
    #include<iostream>
    #include<cstdlib>
    #include<cstring>
    #include<queue>
    using namespace std;
    // 0--n-1 1   n -- 2n-1 0
    const int maxn = 1<<12, maxm = 1<<13;
    int tot,h[maxn],t[maxn],top,dfn[maxn],low[maxn],idx,place[maxn],scccnt;
    int n,m,a[maxn],b[maxn],q[maxn];
    bool ins[maxn];
    struct edge{
        int u,to,nxt;
    }G[maxm];
    void add(int u,int v){G[++tot].nxt = h[u];G[tot].u = u;G[tot].to = v;h[u] = tot;}
    
    void tarjan(int x){
        dfn[x] = low[x] = ++idx;
        t[++top] = x, ins[x] = 1;
        for(int i = h[x]; i; i = G[i].nxt){
            int v = G[i].to;
            if(!dfn[v]){
                tarjan(v);
                low[x] = min(low[x], low[v]);
            }
            else if(ins[v]) low[x] = min(low[x], dfn[v]);
            
        }
        
        if(dfn[x] == low[x]){
            scccnt++;
            while(1){
                place[t[top]] = scccnt;
                ins[t[top--]] = 0;
                if(t[top+1] == x)break;
            }
        }
    }
    void init(){
        tot = 0;
        scccnt = 0;
        memset(G, 0, sizeof(G));
        memset(h, 0, sizeof(h));
        memset(dfn, 0, sizeof(dfn));
        memset(low, 0, sizeof(low));
        memset(place, 0, sizeof(place));
    }
    bool sat(int k){
        init();
        for(int i = 1; i <= k; i++)
            if(a[i] == b[i])add(q[a[i]]^1, q[a[i]]);
            else {
                add(q[b[i]]^1, q[a[i]]);
                add(q[a[i]]^1, q[b[i]]);
            }
        for(int i = 0; i <= 2*n; i++)
            if(!dfn[i])tarjan(i);
        for(int i = 0; i <= 2*n; i+=2)
            if(place[i] == place[i^1])return 0;
        return 1;
    }
    int main(){
        while(scanf("%d%d",&n,&m)==2){
            if(!n && !m)break;
            int ans;
            for(int i = 0; i < n; i++){
                int u, v;
                scanf("%d%d",&u,&v);
                q[u] = i*2; q[v] = q[u]^1;
            }
                
            for(int i = 1; i <= m; i++)
                scanf("%d%d",&a[i],&b[i]);
            int lf = 0, rg = m;
            while(lf <= rg){
                int mid = (lf + rg) >> 1;
                if(sat(mid))lf=mid+1, ans = mid;
                else rg=mid-1;
            }
            printf("%d
    ",ans);    
            
        }
        
    }
    View Code
  • 相关阅读:
    linux修改host文件
    SpringBoot RocketMQ 整合使用和监控
    Linux的五个查找命令
    github 在线创建文件和创建文件夹
    nginx 配置
    JetBrains 里不为人知的秘密(3)--快捷键篇
    java.lang.NoClassDefFoundError: Lorg/springframework/beans/factory/access/BeanFactoryReference;
    【数论】P1029 最大公约数和最小公倍数问题
    ST表学习笔记
    树状数组学习笔记
  • 原文地址:https://www.cnblogs.com/EdSheeran/p/8995339.html
Copyright © 2011-2022 走看看