程序師世界是廣大編程愛好者互助、分享、學習的平台,程序師世界有你更精彩!
首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫知識
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
 程式師世界 >> 編程語言 >> C語言 >> C++ >> 關於C++ >> Get Luffy Out (poj 2723 二分+2

Get Luffy Out (poj 2723 二分+2

編輯:關於C++

 

Language:
Get Luffy Out
Time Limit: 2000MS   Memory Limit: 65536K Total Submissions: 7969   Accepted: 3061

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

Source

Beijing 2005

 

題意:有N對鑰匙,M扇門,每對鑰匙要是用了其中一個另外一個就會馬上消失,每扇門上有兩把鎖,只要打開其中一把鎖門就打開了。開門順序是輸入的順序,問最多能開幾扇門。

思路:因為是按遇到門的順序開門,很自然想到二分門的數量mid,然後用2-SAT判斷mid時候符合條件。對於每對鑰匙a1和a2,a1->~a2(選了a1就不能選a2),a2->a1(選了a2就不能選a1),對於每扇門b1和b2,b1 OR b2=1,~b1->b2, ~b2->b1.

代碼:

 

#include 
#include 
#include 
#include 
#include 
#include 
#include 
#include
#include 
#include 
#include 
#include 
#pragma comment (linker,/STACK:102400000,102400000)
#define pi acos(-1.0)
#define eps 1e-6
#define lson rt<<1,l,mid
#define rson rt<<1|1,mid+1,r
#define FRE(i,a,b)  for(i = a; i <= b; i++)
#define FREE(i,a,b) for(i = a; i >= b; i--)
#define FRL(i,a,b)  for(i = a; i < b; i++)
#define FRLL(i,a,b) for(i = a; i > b; i--)
#define mem(t, v)   memset ((t) , v, sizeof(t))
#define sf(n)       scanf(%d, &n)
#define sff(a,b)    scanf(%d %d, &a, &b)
#define sfff(a,b,c) scanf(%d %d %d, &a, &b, &c)
#define pf          printf
#define DBG         pf(Hi
)
typedef long long ll;
using namespace std;

#define INF 0x3f3f3f3f
#define mod 1000000009
const int maxn = 10005;
const int MAXN = 5005;
const int MAXM = 200010;
const int N = 10005;

struct Edge
{
    int to,next;
}edge[MAXM];

int tot,head[MAXN];
int Low[MAXN],DFN[MAXN],Stack[MAXN],Belong[MAXN];
bool Instack[MAXN];
int top,scc,Index;
int a[MAXN][2],b[MAXN][2],m;

void init()
{
    tot=0;
    memset(head,-1,sizeof(head));
}

void addedge(int u,int v)
{
    edge[tot].to=v;
    edge[tot].next=head[u];
    head[u]=tot++;
}

void Tarjan(int u)
{
    int v;
    Low[u]=DFN[u]=Index++;
    Instack[u]=true;
    Stack[top++]=u;
    for (int i=head[u];~i;i=edge[i].next)
    {
        int v=edge[i].to;
        if (!DFN[v])
        {
            Tarjan(v);
            if (Low[u]>Low[v]) Low[u]=Low[v];
        }
        else if (Instack[v]&&Low[u]>DFN[v])
            Low[u]=DFN[v];
    }
    if (Low[u]==DFN[u])
    {
        scc++;
        do{
            v=Stack[--top];
            Instack[v]=false;
            Belong[v]=scc;
        }while (v!=u);
    }
    return ;
}

bool solvable(int n)
{
    memset(DFN,0,sizeof(DFN));
    memset(Instack,false,sizeof(Instack));
    top=scc=Index=0;
    for (int i=0;i>1;
        if (isok(mid,n))
        {
            ans=mid;
            l=mid+1;
        }
        else r=mid-1;
    }
    printf(%d
,ans);
}

int main()
{
#ifndef ONLINE_JUDGE
    freopen(C:/Users/lyf/Desktop/IN.txt,r,stdin);
#endif
    int i,j,n;
    while (scanf(%d%d,&n,&m))
    {
        if (n==0&&m==0) break;
        for (i=0;i

 

 

 

 

  1. 上一頁:
  2. 下一頁:
Copyright © 程式師世界 All Rights Reserved