题意:
英语限制了我的行动力。。。。就是两个钥匙不能同时用,两个锁至少开一个
建个图 二分就好了。。。emm。。。。dfs 开头low 写成sccno 然后生活失去希望。。。
#include <iostream> #include <cstdio> #include <sstream> #include <cstring> #include <map> #include <cctype> #include <set> #include <vector> #include <stack> #include <queue> #include <algorithm> #include <cmath> #include <bitset> #define rap(i, a, n) for(int i=a; i<=n; i++) #define rep(i, a, n) for(int i=a; i<n; i++) #define lap(i, a, n) for(int i=n; i>=a; i--) #define lep(i, a, n) for(int i=n; i>a; i--) #define rd(a) scanf("%d", &a) #define rlld(a) scanf("%lld", &a) #define rc(a) scanf("%c", &a) #define rs(a) scanf("%s", a) #define pd(a) printf("%d ", a); #define plld(a) printf("%lld ", a); #define pc(a) printf("%c ", a); #define ps(a) printf("%s ", a); #define MOD 2018 #define LL long long #define ULL unsigned long long #define Pair pair<int, int> #define mem(a, b) memset(a, b, sizeof(a)) #define _ ios_base::sync_with_stdio(0),cin.tie(0) //freopen("1.txt", "r", stdin); using namespace std; const int maxn = 10010, INF = 0x7fffffff, LL_INF = 0x7fffffffffffffff; int n, m; vector<int> G[maxn]; int sccno[maxn], vis[maxn], low[maxn], scc_cnt, scc_clock; stack<int> S; struct node { int x, y; }Node[maxn], Edge[maxn]; void init() { mem(sccno, 0); mem(vis, 0); mem(low, 0); scc_clock = scc_cnt = 0; for(int i = 0; i < maxn; i++) G[i].clear(); } void dfs(int u) { vis[u] = low[u] = ++scc_clock; S.push(u); for(int i = 0; i < G[u].size(); i++) { int v = G[u][i]; if(!vis[v]) { dfs(v); low[u] = min(low[v], low[u]); } else if(!sccno[v]) low[u] = min(low[u], vis[v]); } if(vis[u] == low[u]) { scc_cnt++; for(;;) { int x = S.top(); S.pop(); sccno[x] = scc_cnt; if(x == u) break; } } } bool check() { for(int i = 0; i <= n * 4 - 2; i += 2) if(sccno[i] == sccno[i + 1]) { return false; } return true; } void build(int mid) { for(int i = 0; i < n; i++) { int u = Edge[i].x, v = Edge[i].y; G[u << 1 | 1].push_back(v << 1); G[v << 1 | 1].push_back(u << 1); } for(int i = 0; i < mid; i++) { int u = Node[i].x, v = Node[i].y; G[u << 1].push_back(v << 1 | 1); G[v << 1].push_back(u << 1 | 1); } } int main() { int u, v; while(cin >> n >> m && n + m) { init(); for(int i = 0; i < n; i++) { cin >> Edge[i].x >> Edge[i].y; } for(int i = 0; i < m; i++) { cin >> Node[i].x >> Node[i].y; } int l = 1, r = m, ans; while(l <= r) { init(); int mid = (l + r) / 2; build(mid); for(int i = 0; i <= n * 4 - 2; i++) if(!vis[i]) dfs(i); if(check()) l = mid + 1; else r = mid - 1; } cout << r << endl; } return 0; }