题意:
就是求桥最多的一条路
解析:
先求连通分量的个数 然后缩点建图 求直径即可
#include <bits/stdc++.h> #define mem(a, b) memset(a, b, sizeof(a)) using namespace std; const int maxn = 1e6+10, INF = 0x7fffffff; vector<int> G[maxn<<1]; int pre[maxn<<1], lowlink[maxn<<1], sccno[maxn<<1], dfs_clock, scc_cnt, d[maxn<<1], vis[maxn<<1]; stack<int> S; int n, m, maxway, pos; map<int, int> w[maxn<<1]; int head[maxn<<1], cnt, ans; struct node { int u, v, next; }Node[maxn<<1]; void add_(int u, int v) { Node[cnt].u = u; Node[cnt].v = v; Node[cnt].next = head[u]; head[u] = cnt++; } void add(int u, int v) { add_(u, v); add_(v, u); } void dfs(int u, int fa) { pre[u] = lowlink[u] = ++dfs_clock; S.push(u); for(int i=0; i<G[u].size(); i++) { int v = G[u][i]; if(v == fa) continue; if(!pre[v]) { dfs(v, u); lowlink[u] = min(lowlink[u], lowlink[v]); } else if(!sccno[v]) lowlink[u] = min(lowlink[u], pre[v]); } if(lowlink[u] == pre[u]) { scc_cnt++; for(;;) { int x = S.top(); S.pop(); sccno[x] = scc_cnt; if(x == u) break; } } } void init() { dfs_clock = scc_cnt = cnt = 0; mem(sccno, 0); mem(pre, 0); mem(head, -1); } void bfs(int u) { mem(vis, 0); mem(d, 0); queue<int> Q; Q.push(u); vis[u] = 1; d[u] = 0; maxway = 0, pos = u, ans = 0; while(!Q.empty()) { int u = Q.front(); Q.pop(); for(int i=head[u]; i!=-1; i=Node[i].next) { int v = Node[i].v; if(!vis[v]) { vis[v] = 1; d[v] = d[u] + 1; if(d[v] > maxway) maxway = d[v], pos = v; Q.push(v); } } } } int main() { init(); cin>> n >> m; int u, v; for(int i=0; i<m; i++) { cin>> u >> v; G[u].push_back(v); G[v].push_back(u); } dfs(1, -1); // cout<< scc_cnt <<endl; for(int i=1; i<=n; i++) { for(int j=0; j<G[i].size(); j++) { int v = G[i][j]; if(sccno[i] != sccno[v]) { // cout<< i << " " << v <<endl; // w[i][v] = w[v][i] = 1; if(!w[sccno[i]][sccno[v]]) w[sccno[i]][sccno[v]] = w[sccno[i]][sccno[v]] = 1, add(sccno[i], sccno[v]); } } } bfs(1); bfs(pos); cout<< maxway <<endl; return 0; }