给定一张无向图,问现在添加一条边后可以使得图中还存在最少的桥边是多少。
思路:因为图是连通的,我们只需要把图缩点后形成一棵树(这里的边是原图中的桥)。然后求出这棵树的直径(经典问题),最后就是ans = bridge - Radius与0取最大。
特别注意下重边的处理。
/***************************************** Author :Crazy_AC(JamesQi) Time :2016 File Name : *****************************************/ // #pragma comment(linker, "/STACK:1024000000,1024000000") #include <iostream> #include <algorithm> #include <iomanip> #include <sstream> #include <string> #include <stack> #include <queue> #include <deque> #include <vector> #include <map> #include <set> #include <cstdio> #include <cstring> #include <cmath> #include <cstdlib> #include <climits> using namespace std; #define MEM(x,y) memset(x, y,sizeof x) #define pk push_back #define lson rt << 1 #define rson rt << 1 | 1 #define bug cout << "BUG HERE\n" typedef long long LL; typedef unsigned long long ULL; typedef pair<int,int> ii; typedef pair<ii,int> iii; const double eps = 1e-8; const int inf = 1 << 30; const int INF = 0x3f3f3f3f; const int MOD = 1e9 + 7; int nCase = 0; const int maxn = 2e5 + 10; const int maxm = 1e6 + 10; struct Edge { int from, to, nxt; Edge() {} Edge(int from,int to,int nxt) : from(from), to(to), nxt(nxt) {} }edges[maxm*2]; int head[maxn], ecnt; int n, m; int pre[maxn], low[maxn], instack[maxn], times; stack<int> st; int belong[maxn], black; void input() { memset(head, -1,sizeof head), ecnt = 0; memset(pre, -1,sizeof pre); memset(instack, 0,sizeof instack); times = 0; black = 0; int u, v; for (int i = 1;i <= m;++i) { scanf("%d%d",&u,&v); edges[ecnt] = Edge(u, v, head[u]), head[u] = ecnt++; edges[ecnt] = Edge(v, u, head[v]), head[v] = ecnt++; } } set<ii> s; void Tarjan(int u,int fa) { instack[u] = 1; st.push(u); pre[u] = low[u] = ++times; bool first = true; for (int i = head[u];~i;i = edges[i].nxt) { Edge& e = edges[i]; if (e.to == fa && first) {//处理回边 first = false; continue; } if (pre[e.to] == -1) { Tarjan(e.to, u); low[u] = min(low[u], low[e.to]); }else if (pre[e.to] > 0 && instack[e.to]) low[u] = min(low[u], pre[e.to]); } if (pre[u] == low[u]) { black++; while(true) { int x = st.top(); st.pop(); belong[x] = black; instack[x] = 0; if (u == x) break; } } } vector<int> G[maxn], vec; int Maxdep; void dfs(int u,int depth) { instack[u] = 0; Maxdep = max(Maxdep, depth); for (int i = 0;i < G[u].size();++i) { int v = G[u][i]; if (!instack[v]) continue; dfs(v, depth + 1); } } void solve() { s.clear(); for (int i = 1;i <= n;++i) if (pre[i] == -1) Tarjan(i, -1); // for (int i = 1;i <= n;++i) // printf("%4d", belong[i]); // puts(""); for (int i = 0;i < 2*m;i += 2) { int u = belong[edges[i].from];//hash 后的编号 int v = belong[edges[i].to]; if (u != v) { // printf("%d %d\n", u, v); if (u < v) swap(u, v); s.insert(ii(u, v)); } } if (s.size() == 0 || s.size() == 1) puts("0"); else { // bug; for (int i = 1;i <= black;++i) G[i].clear(); vec.clear(); int u, v; set<ii> :: iterator it; for (it = s.begin();it != s.end();++it) {//桥边 u = it->first; v = it->second; G[u].push_back(v); G[v].push_back(u); } memset(instack, 0,sizeof instack); queue<int> que; que.push(1); while(!que.empty()) { u = que.front(), que.pop(); vec.push_back(u); instack[u] = 1; for (int i = 0;i < G[u].size();++i) { v = G[u][i]; if (instack[v]) continue; que.push(v); } } // printf("u = %d\n", u); u = vec[vec.size() - 1]; Maxdep = 0; dfs(u, 0); // printf("Maxdep = %d\n", Maxdep); cout << s.size() - Maxdep << endl; } } int main(int argc, const char * argv[]) { // freopen("in.txt","r",stdin); // freopen("out.txt","w",stdout); while(scanf("%d%d",&n,&m) == 2 && (n + m)) { input(); solve(); } return 0; }