• POJ2723 Get Luffy Out 【2-sat】


    题目

    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?

    输入格式

    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.

    输出格式

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

    输入样例

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

    输出样例

    4

    题解

    看题看了好久系列。。。。
    首先我们先确定是以哪种东西为点
    钥匙二选一,显然钥匙是点
    门是对应的限制关系:每个门对应的两种钥匙至少选一种,就是OR关系

    OR关系:若(a OR b = true),则a'->b,b'->a【因为如果a是假,由于a OR b为真,b一定为真,另一个同理】

    题目要求我们求最多开的门数
    我们知道2-sat最常用的作用就是判定,所以2-sat可以作为二分的判定,进行二分答案

    #include<iostream>
    #include<cmath>
    #include<cstdio>
    #include<cstring>
    #include<algorithm>
    #define LL long long int
    #define REP(i,n) for (int i = 1; i <= (n); i++)
    #define Redge(u) for (int k = h[u],to; k; k = ed[k].nxt)
    #define BUG(s,n) for (int i = 1; i <= (n); i++) cout<<s[i]<<' '; puts("");
    #define cls(x) memset(x,0,sizeof(x))
    using namespace std;
    const int maxn = 3005,maxm = 100005,INF = 1000000000;
    inline int read(){
    	int out = 0,flag = 1; char c = getchar();
    	while (c < 48 || c > 57) {if (c == '-') flag = -1; c = getchar();}
    	while (c >= 48 && c <= 57) {out = (out << 3) + (out << 1) + c - '0'; c = getchar();}
    	return out * flag;
    }
    int h[maxn],ne;
    struct EDGE{int to,nxt;}ed[maxm];
    void build(int u,int v){ed[ne] = (EDGE){v,h[u]}; h[u] = ne++;}
    int dfn[maxn],low[maxn],Scc[maxn],st[maxn],scci,top,cnt;
    void dfs(int u){
    	dfn[u] = low[u] = ++cnt;
    	st[++top] = u;
    	Redge(u){
    		if (!dfn[to = ed[k].to]){
    			dfs(to);
    			low[u] = min(low[u],low[to]);
    		}else if (!Scc[to]) low[u] = min(low[u],dfn[to]);
    	}
    	if (dfn[u] == low[u]){
    		scci++;
    		do{Scc[st[top]] = scci;}while (st[top--] != u);
    	}
    }
    int n,m,op[maxn],A[maxn],B[maxn];
    void init(){
    	cls(dfn); cls(Scc); cls(h); ne = 1; scci = cnt = top = 0;
    }
    bool check(int len){
    	init();
    	REP(i,len){
    		if (op[A[i]] == B[i]) continue;
    		else if (A[i] == B[i]) build(op[A[i]],A[i]);
    		else build(op[A[i]],B[i]),build(op[B[i]],A[i]);
    	}
    	for (int i = 0; i < (n << 1); i++) if (!dfn[i]) dfs(i);
    	for (int i = 0; i < n; i++) if (Scc[i] == Scc[op[i]]) return false;
    	return true;
    }
    int main(){
    	while (~scanf("%d%d",&n,&m) && n){
    		int a,b;
    		REP(i,n) a = read(),b = read(),op[a] = b,op[b] = a;
    		REP(i,m) A[i] = read(),B[i] = read();
    		int L = 1,R = m,mid;
    		while (L < R){
    			mid = L + R + 1 >> 1;
    			if (check(mid)) L = mid;
    			else R = mid - 1;
    		}
    		printf("%d
    ",L);
    	}
    	return 0;
    }
    
    
  • 相关阅读:
    解决VS在高DPI下设计出的Winform程序界面变形问题
    ribbon 收起 最小化 导航条 选项卡 navBarControl 隐藏
    Devexpress Ribbon Add Logo
    DevExpress控件使用小结 z
    Devexpress中统一设置字体样式的方法
    微信小程序回到顶部的两种方式
    PHP房贷计算器代码,等额本息,等额本金
    java 时间转换
    spark 编译
    mvn常用命令
  • 原文地址:https://www.cnblogs.com/Mychael/p/8289197.html
Copyright © 2020-2023  润新知