Get Luffy Out
Time Limit: 2000MS   Memory Limit: 65536K
Total Submissions: 7295   Accepted: 2778

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

 
二分答案走2 -sat
 #include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm>
#include <stack>
#include <vector> using namespace std; const int MAX_N = ( << ) + ;
int N,M;
int low[MAX_N],pre[MAX_N],cmp[MAX_N];
int dfs_clock,scc_cnt;
stack <int > S;
int no[MAX_N],x[],y[];
vector<int> G[MAX_N]; void dfs(int u) {
pre[u] = low[u] = ++dfs_clock;
S.push(u);
for(int i = ; i < G[u].size(); ++i) {
int v = G[u][i];
if(!pre[v]) {
dfs(v);
low[u] = min(low[u],low[v]);
} else if(!cmp[v]) {
low[u] = min(low[u],pre[v]);
}
} if(low[u] == pre[u]) {
++scc_cnt;
for(;;) {
int x = S.top(); S.pop();
cmp[x] = scc_cnt;
if(x == u) break;
}
}
}
void scc() {
dfs_clock = scc_cnt = ;
memset(cmp,,sizeof(cmp));
memset(pre,,sizeof(pre)); for(int i = ; i < * N; ++i) {
if(!pre[i]) dfs(i);
}
}
bool check(int m) {
for(int i = ; i < * N; ++i) {
G[i].clear();
}
for(int i = ; i <= m; ++i) {
int a = x[i],b = y[i];
G[no[a]].push_back(b);
G[no[b]].push_back(a);
}
scc();
for(int i = ; i < * N; ++i) {
if(cmp[i] == ) continue;
if(cmp[i] == cmp[ no[i] ]) {
//printf("i = %d no = %d %d %d\n",i,no[i],cmp[i],cmp[no[i]]);
return false;
}
} return true;
}
void solve() {
int l = ,r = M;
while(l < r) {
int mid = (l + r + ) / ;
if(check(mid)) l = mid;
else r = mid - ;
}
printf("%d\n",l);
}
int main()
{
//freopen("sw.in","r",stdin);
while(~scanf("%d%d",&N,&M)) {
if(N == && M == ) break;
for(int i = ; i <= N; ++i) {
int a,b;
scanf("%d%d",&a,&b);
no[a] = b;
no[b] = a;
}
for(int i = ; i <= M; ++i) {
scanf("%d%d",&x[i],&y[i]);
} solve(); }
return ;
}

poj 2723的更多相关文章

  1. POJ 2723 Get Luffy Out(2-SAT+二分答案)

    Get Luffy Out Time Limit: 2000MS   Memory Limit: 65536K Total Submissions: 8851   Accepted: 3441 Des ...

  2. HDU 1816, POJ 2723 Get Luffy Out(2-sat)

    HDU 1816, POJ 2723 Get Luffy Out pid=1816" target="_blank" style="">题目链接 ...

  3. poj 2723 2-SAT问题

    思路:二分枚举能开的门的数量,将每次枚举转换成2-SAT问题.这里存在的矛盾是假设有门上a,b两个锁,a锁对应于1号钥匙,而一号钥匙的配对是2号钥匙,b锁对应于3号钥匙,3号的配对是4号钥匙.那么2号 ...

  4. poj 2723 Get Luffy Out(2-sat)

    Description Ratish is a young man who always dreams of being a hero. One day his friend Luffy was ca ...

  5. poj 2723 Get Luffy Out 二分+2-sat

    题目链接 给n个钥匙对, 每个钥匙对里有两个钥匙, 并且只能选择一个. 有m扇门, 每个门上有两个锁, 只要打开其中一个就可以通往下一扇门. 问你最多可以打开多少个门. 对于每个钥匙对, 如果选择了其 ...

  6. POJ 2723 HDU 1816 Get Luffy Out

    二分答案 + 2-SAT验证 #include<cstdio> #include<cstring> #include<cmath> #include<stac ...

  7. poj 2723 二分+2-sat判定

    题意:给出n对钥匙,每对钥匙只能选其中一个,在给出每层门需要的两个钥匙,只要一个钥匙就能开门,问最多能到哪层. 思路:了解了2-SAT判定的问题之后主要就是建图的问题了,这里建图就是对于2*n个钥匙, ...

  8. poj 2723 Get Luffy Out 2-SAT

    两个钥匙a,b是一对,隐含矛盾a->!b.b->!a 一个门上的两个钥匙a,b,隐含矛盾!a->b,!b->a(看数据不大,我是直接枚举水的,要打开当前门,没选a的话就一定要选 ...

  9. poj 2723 Get Luffy Out-2-sat问题

    Description Ratish is a young man who always dreams of being a hero. One day his friend Luffy was ca ...

随机推荐

  1. Microsoft Power BI Designer

    1/25/2015年1月25发布的预览版本,可以通过以下地址下载,注意有x64 和x32 版本区别(和上次PowerMap一样,一般也推荐的使用x64版本) http://www.microsoft. ...

  2. C 函数可变参数

    C 函数可变参数 C 语言中用 ... 表示可变参数,例如: void fun(int x ...) 头文件 cstdarg.h 中包含可变参数类型va_list和处理可变参数的三个宏: va_lis ...

  3. java 通过zxing生成二维码

    1.基本类提供二维码生成工具类 package com.green.util; import java.awt.image.BufferedImage; import java.io.ByteArra ...

  4. 关于asp.net和iis的进程/线程问题,假如网站有1000个人访问,会产生多少个进程/线程啊

    详解 ASP.NET异步   超好的文章

  5. keepalived安装配置(nginx)

    环境:centos 6.4 64bit 应用:nginx 目的:keepalived可以让两台服务器处于主备关系,如果主的挂了,备的取得VIP(或者互为主备等关系,文字游戏不纠结), 以实现服务器的高 ...

  6. WCF note1

    Summary of WCF Client to use service use ChannelFactory to create proxy to use service. Client code ...

  7. asp.net解决高并发的方案.

    asp.net解决高并发的方案. Posted on 2012-11-27 22:31 75077027 阅读(3964) 评论(1) 编辑 收藏 最近几天一直在读代震军的博客,他是 Discuz!N ...

  8. mysql卸载注意事项

    由于数据库软件十分的复杂,不管是Mysql还是sqlserver安装都有很多配置要选择. 假若你第一次安装数据库失败,然后又想卸载,又再次安装,这时可能由于你第一次的卸载不完全,会导致你第二次安装时出 ...

  9. ostream类重载的operator<<()函数

    ostream类重载了operator<<()以识别不同的类型,如: int short  long unsigned int unsigned short unsigned long f ...

  10. PSP0表格

    一 项目计划日志 周活动总结表 姓名:陆宇                                                                         日期:3.1 ...