题意
给出 n 对钥匙,每对只能挑一把使用,每把只能用一次,当一对钥匙中的一把被使用后,另一把也就不能再用了;然后给出 m 道门,每个门都有两把钥匙可以打开,问最多能开几道门(按给出的顺序开)。
Sol
这不就是\(HNOI\)超级英雄吗?
上次写的二分图匹配 这次写个\(2-SAT\) 二分答案+\(2-SAT\)判定 注意不要漏# include# include # include # include # include # include # define RG register# define IL inline# define Fill(a, b) memset(a, b, sizeof(a))using namespace std;typedef long long ll;const int _(8050);IL int Input(){ RG int x = 0, z = 1; RG char c = getchar(); for(; c < '0' || c > '9'; c = getchar()) z = c == '-' ? -1 : 1; for(; c >= '0' && c <= '9'; c = getchar()) x = (x << 1) + (x << 3) + (c ^ 48); return x * z;}int n, m, tmp, first[_], cnt, num, x[_], y[_];int S[_], vis[_], dfn[_], low[_], Index, col[_];struct Link{ int u, v;} link[_];struct Edge{ int to, next;} edge[_ << 1];IL void Add(RG int u, RG int v){ edge[cnt] = (Edge){v, first[u]}; first[u] = cnt++;}IL void Tarjan(RG int u){ vis[u] = 1, dfn[u] = low[u] = ++Index, S[++S[0]] = u; for(RG int e = first[u]; e != -1; e = edge[e].next){ RG int v = edge[e].to; if(!dfn[v]) Tarjan(v), low[u] = min(low[u], low[v]); else if(vis[v]) low[u] = min(low[u], dfn[v]); } if(dfn[u] != low[u]) return; RG int v = S[S[0]--]; col[v] = ++num, vis[v] = 0; while(v != u) v = S[S[0]--], col[v] = num, vis[v] = 0;}IL int Check(RG int mid){ Fill(first, -1), Fill(dfn, 0), Fill(col, 0), cnt = num = 0; for(RG int i = 0; i < n; ++i) Add(x[i], y[i] + tmp), Add(y[i], x[i] + tmp); for(RG int i = 1; i <= mid; ++i) Add(link[i].u + tmp, link[i].v), Add(link[i].v + tmp, link[i].u); for(RG int i = 0, t = tmp << 1; i < t; ++i) if(!dfn[i]) Tarjan(i); for(RG int i = 0; i < tmp; ++i) if(col[i] == col[i + tmp]) return 0; return 1;}int main(RG int argc, RG char* argv[]){ while(233){ n = Input(), m = Input(); if(!(n + m)) break; tmp = n << 1; for(RG int i = 0; i < n; ++i) x[i] = Input(), y[i] = Input(); for(RG int i = 1; i <= m; ++i) link[i] = (Link){Input(), Input()}; RG int l = 0, r = m, ans = 0; while(l <= r){ RG int mid = (l + r) >> 1; if(Check(mid)) ans = mid, l = mid + 1; else r = mid - 1; } printf("%d\n", ans); } return 0;}