题面描述
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?
题意
给你 n 对钥匙,每对若用了其中一把,另一把就不能用。有 m 扇门,每扇门可以由给定的两把钥匙中的任意一把打开,问最多能打开多少扇门。
思路
其实可以不用二分,但二分跑的快些。
二分答案,对于每对钥匙 (a) 和 (b),(a) 用了 (b) 就不能用((a ightarrow eg b)),(b) 用了 (a) 就不能用((b ightarrow eg a))。
对于每扇门的 (a) 和 (b),不用 (a) 打开就必须用 (b) 打开(( eg a ightarrow b)),不用 (b) 打开就必须用 (a) 打开(( eg b ightarrow a))。
所以建个图,跑个 2-SAT 就好啦
/************************************************
*Author : lrj124
*Created Time : 2019.11.10.20:21
*Mail : 1584634848@qq.com
*Problem : poj2723
************************************************/
#include <algorithm>
#include <cstdio>
#include <vector>
#include <stack>
using namespace std;
const int maxn = 10000 + 10;
int n,m,low[maxn],dfn[maxn],scc[maxn],scccnt,ind;
pair<int,int> key[maxn],door[maxn];
vector<int> edge[maxn];
bool vis[maxn];
stack<int> s;
inline void tarjan(int now) {
dfn[now] = low[now] = ++ind;
vis[now] = true,s.push(now);
for (size_t i = 0;i < edge[now].size();i++) {
int to = edge[now][i];
if (!dfn[to]) {
tarjan(to);
low[now] = min(low[now],low[to]);
} else if (vis[to]) low[now] = min(low[now],dfn[to]);
}
if (dfn[now] == low[now]) {
scc[now] = ++scccnt;
for (;s.top() ^ now;vis[s.top()] = false,s.pop()) scc[s.top()] = scccnt;
vis[now] = false,s.pop();
}
}
inline bool check(int mid) {
ind = scccnt = 0;
for (int i = 1;i <= 4*n;i++) edge[i].clear(),low[i] = dfn[i] = 0;
for (int i = 1;i <= n;i++) {
edge[key[i].first].push_back(key[i].second+2*n);
edge[key[i].second].push_back(key[i].first+2*n);
}
for (int i = 1;i <= mid;i++) {
edge[door[i].first+2*n].push_back(door[i].second);
edge[door[i].second+2*n].push_back(door[i].first);
}
for (int i = 1;i <= 4*n;i++) if (!dfn[i]) tarjan(i);
for (int i = 1;i <= 2*n;i++) if (scc[i] == scc[i+2*n]) return false;
return true;
}
int main() {
// freopen("poj2723.in","r",stdin);
// freopen("poj2723.out","w",stdout);
for (;scanf("%d%d",&n,&m),n && m;) {
for (int i = 1,x,y;i <= n;i++) {
scanf("%d%d",&x,&y); x++,y++;
key[i] = make_pair(x,y);
}
for (int i = 1,x,y;i <= m;i++) {
scanf("%d%d",&x,&y); x++,y++;
door[i] = make_pair(x,y);
}
int l = 0,r = m,ans;
for (int mid;l <= r;check(mid = l+r>>1) ? l = mid+1,ans = mid : r = mid-1);
printf("%d
",ans);
}
return 0;
}