您的位置:首页 > 其它

poj 2723 Get Luffy Out(图论-2-sat)

2014-08-13 11:37 211 查看
Get Luffy Out

Time Limit: 2000MSMemory Limit: 65536K
Total Submissions: 7512Accepted: 2855
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

每把钥匙分成两个节点,取和不取。

#include <iostream>
#include <cstdio>
#include <stack>
#include <vector>
using namespace std;

const int maxn = 5000;
const int maxe = 10*maxn;
struct Node{
int key1 ,key2;
Node(int a = 0 , int b = 0){
key1 = a , key2 = b;
}
};
int N , M;
vector<Node> key_pair , door;

/////////////////////////////tarjan
struct edge{
int u , v , next;
edge(int a = 0 , int b = 0 , int c = 0){
u = a , v = b , next = c;
}
}tarjan_e[maxe];
int tarjan_low[maxn] , tarjan_dfn[maxn] , tarjan_vis[maxn] , tarjan_block[maxn] , tarjan_head[maxn];
int tarjan_order , tarjan_b , tarjan_cnt;
stack<int> tarjan_stack;

void tarjan_add(int u , int v){
tarjan_e[tarjan_cnt] = edge(u , v , tarjan_head[u]);
tarjan_head[u] = tarjan_cnt++;
}

void tarjan_init(){
for(int i = 0; i < maxn; i++){
tarjan_low[i] = 0;
tarjan_dfn[i] = 0;
tarjan_vis[i] = 0;
tarjan_block[i] = 0;
tarjan_head[i] = -1;
}
tarjan_b = 1;
tarjan_order = 1;
tarjan_cnt = 0;
while(!tarjan_stack.empty()) tarjan_stack.pop();
}

void tarjan(int u){
tarjan_low[u] = tarjan_order;
tarjan_dfn[u] = tarjan_order++;
tarjan_vis[u] = 1;
tarjan_stack.push(u);
for(int i = tarjan_head[u]; i != -1; i = tarjan_e[i].next){
int v = tarjan_e[i].v;
if(!tarjan_dfn[v]){
tarjan(v);
tarjan_low[u] = min(tarjan_low[u] , tarjan_low[v]);
}else if(tarjan_vis[v]) tarjan_low[u] = min(tarjan_low[u] , tarjan_dfn[v]);
}
if(tarjan_low[u] == tarjan_dfn[u]){
int top;
do{
top = tarjan_stack.top();
tarjan_stack.pop();
tarjan_vis[top] = 0;
tarjan_block[top] = tarjan_b;
}while(top != u);
tarjan_b++;
}
}
/////////////////////////////////2-SAT

void tarjan_build(int m){
for(int i = 1; i <= N; i++){
tarjan_add(key_pair[i].key1 , key_pair[i].key2+2*N);
tarjan_add(key_pair[i].key2 , key_pair[i].key1+2*N);
}
for(int i = 1; i <= m; i++){
tarjan_add(door[i].key1+2*N , door[i].key2);
tarjan_add(door[i].key2+2*N , door[i].key1);
}
}

bool Tow_sat(int m){
tarjan_init();
tarjan_build(m);
for(int i = 0; i < 4*N; i++){
if(!tarjan_dfn[i]){
tarjan(i);
}
}
for(int i = 0; i < 2*N; i++){
if(tarjan_block[i] == tarjan_block[i+2*N]) return false;
}
return true;
}

///////////////////////////////////////////

void initial(){
key_pair.clear();
door.clear();
}

void readcase(){
int key1 , key2;
key_pair.push_back(Node(0 , 0));
for(int i = 0; i < N; i++){
scanf("%d%d" , &key1 , &key2);
key_pair.push_back(Node(key1 , key2));
}
door.push_back(Node(0 ,0));
for(int i = 0; i < M; i++){
scanf("%d%d" , &key1 , &key2);
door.push_back(Node(key1 , key2));
}
}

void computing(){
int l = 1 , r = M+1;
while(l < r){
int mid = (l+r)/2;
//cout << "l=" << l << " r=" << r << " mid=" << mid << endl;
if(Tow_sat(mid)) l = mid+1;
else r = mid;
}
//cout <<"sdf : "<< door[l].key1 << " " << door[l].key2 << endl;
printf("%d\n" , l-1);
}

int main(){
while(scanf("%d%d" , &N , &M) && (N != 0 || M != 0)){
initial();
readcase();
computing();
}
return 0;
}


[/code]
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: