Poj 2723(two-sat)

Get Luffy Out
Time Limit: 2000MS Memory Limit: 65536K
Total Submissions: 6924 Accepted: 2645

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 <= 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 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

有n对钥匙(2*n种钥匙),分别一一对应(2*n种门),每对钥匙只能选择其中一个钥匙保留,现在有m层地牢,每层地牢有两扇门,只有至少打开其中一扇,才能进入下一层,问现在最多能走几层。因为门和钥匙一一对应,所以干脆就把它们看成一个东西,然后每种钥匙选或不选,拆点,2-sat就看出来了。条件就是每对钥匙只能选一个,和每扇门至少选一个为真(不能贪心的想成两个恰只有一个为真,因为两扇门可能相同),然后一一添加每层地牢的条件,每次跑一遍twosat,找到最大的可行层即可。不过二分似乎更快。
#include<cstdio>
#include<cstring>
#include<queue>
#include<vector>
#include<algorithm>
#include<iostream>
using namespace std;

const int maxn = 5000 + 5;

struct TwoSAT {
  int n;
  vector<int> G[maxn*2];
  bool mark[maxn*2];
  int S[maxn*2], c;

  bool dfs(int x) {
    if (mark[x^1]) return false;
    if (mark[x]) return true;
    mark[x] = true;
    S[c++] = x;
    for (int i = 0; i < G[x].size(); i++)
      if (!dfs(G[x][i])) return false;
    return true;
  }

  void init(int n) {
    this->n = n;
    for (int i = 0; i < n*2; i++) G[i].clear();
  }
    //kind=0 -> &&;kind=1 -> ||;kind=2 -> ^;
  void add_edge(int a,int b,int c,int kind){
    if(kind == 0){
        if(c == 0){
            G[2*a].push_back(2*b+1);
            G[2*b].push_back(2*a+1);
        }
        else{
            G[2*a+1].push_back(2*a);
            G[2*b+1].push_back(2*b);
        }
    }
    else if(kind == 1){
        if(c == 0){
            G[2*a].push_back(2*a+1);
            G[2*b].push_back(2*b+1);
        }
        else{
            G[2*a+1].push_back(2*b);
            G[2*b+1].push_back(2*a);
        }
    }
    else{
        if(c == 0){
            G[2*a].push_back(2*b);
            G[2*a+1].push_back(2*b+1);
            G[2*b].push_back(2*a);
            G[2*b+1].push_back(2*a+1);
        }
        else{
            G[2*a].push_back(2*b+1);
            G[2*a+1].push_back(2*b);
            G[2*b].push_back(2*a+1);
            G[2*b+1].push_back(2*a);
        }
    }
  }

  bool solve() {
    memset(mark, 0, sizeof(mark));
    for(int i = 0; i < n*2; i += 2){
      if(!mark[i] && !mark[i+1]) {
        c = 0;
        if(!dfs(i)) {
          while(c > 0) mark[S[--c]] = false;
          if(!dfs(i+1)) return false;
        }
      }
    }
    return true;
  }
};

TwoSAT solver;

int main(){
    int n,m;
    while(scanf("%d%d",&n,&m)){
        if(n == 0 && m == 0) break;
        solver.init(2*n);
        for(int i = 0;i < n;i++){
            int x,y;
            scanf("%d%d",&x,&y);
            solver.G[x*2].push_back(y*2+1);
            solver.G[y*2].push_back(x*2+1);
        }
        int ans = 0;
        for(int i = 1;i <= m;i++){
            int x,y;
            scanf("%d%d",&x,&y);
            solver.add_edge(x,y,1,1);
            if(solver.solve()) ans = i;
        }
        printf("%d\n",ans);
    }
    return 0;
}


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值