HDU 1816 get Luffy out *

Source: Internet
Author: User

Get Luffy out *

Time Limit: 6000/3000 MS (Java/others) memory limit: 32768/32768 K (Java/Others)
Total submission (s): 570 accepted submission (s): 225


Problem descriptionratish 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 cocould 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 should t 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 made N pairs, one key may be appear in some 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 ready doors as possible. can you help him to choose n keys to open the maximum number of doors?

 

Inputthere are several test cases. every test case starts with a line containing two positive integers n (1 <= n <= 2 ^ 10) and M (1 <= m <= 2 ^ 11) 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 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 shoshould 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 shoshould not be processed.

 

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

 

Sample input3 60 31 24 50 10 24 14 23 52 20 0

 

Sample output4 Hint Questions changed!

 

Sourcebeijing 2005 classic two-sat + 2 points. It requires a bit of skill to make a graph .. I used reverse side construction. For condition 1, construct a 2 * U-2 * V undirected edge to represent 2 * u, 2 * V cannot coexist. A 2 * u ^ 1-2 * V ^ 1 undirected edge is constructed for the binary depth to represent 2 * U, 2 * V at least one space that exists and is connected to the linked list to open N * n is safer, and N must be larger, otherwise it is easy to report wa ~
#include <iostream>#include <cstdio>#include <cstring>#include <stack>#include <map>#include <cmath>#include <vector>using namespace std;const int N = (1<<13);int  n , m , st[N<<1] ,top;int eh[N] , et[N*N] , nxt[N*N] , tot ;bool mark[N<<2];struct node{    int x , y ;}  key[N<<1] , door[N<<1];void init(){    tot = 0 ;    memset( eh , -1 , sizeof eh );    memset( mark ,false , sizeof mark );}void addedge( int u , int v ){    et[tot] = v , nxt[tot] = eh[u] , eh[u] = tot++ ;    et[tot] = u , nxt[tot] = eh[v] , eh[v] = tot++ ;}bool dfs( int u ){    if( mark[u] ) return true;    if( mark[u^1] ) return false ;    mark[u] = true ;    st[top++] = u ;    for( int i = eh[u] ; ~i ; i = nxt[i] ){        int v = et[i];        if( !dfs(v^1) )  return false;    }    return true;}bool solve(){    for( int i = 0 ; i < 4 * n ; i += 2 ){        if( !mark[i] && !mark[i+1] ){            top = 0 ;            if( !dfs(i) ){                while( top > 0 ) mark[ st[--top] ] = false ;                if( !dfs(i+1) ) return false;            }        }    }    return true;}bool test( int dep ){    init();    for( int i = 0 ; i < n ; ++i ){        addedge( 2*key[i].x , 2*key[i].y );    }    for( int i = 0 ; i < dep ; ++i ){        addedge(2*door[i].x^1,2*door[i].y^1);    }    return solve();}void run(){    int x , y ;//    cout << N <<endl;    for( int i = 0 ; i < n ; ++i ){        scanf("%d%d",&key[i].x,&key[i].y);    }    for( int i = 0 ; i < m ; ++i ){        scanf("%d%d",&door[i].x,&door[i].y);    }    int l = 0 , r = m , ans = 0 ;    while( l <= r )    {        int mid = ( l+r )>>1;        if( test(mid) ) ans = mid , l = mid + 1 ;        else r = mid - 1;    }    printf("%d\n",ans);}int main(){    #ifdef LOCAL        freopen("in.txt","r",stdin);    #endif // LOCAL    while( scanf("%d%d",&n,&m) ){        if( !n && !m ) break;        run();    }}

 

 

HDU 1816 get Luffy out *

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.