Question: N points (0 ~ N-1), m undirected edges, and ask the maximum number of connected parts after removing the two points.
First, remove a vertex to find each cut point, in the DFS process, find out how many connected parts are removed from the cut point (Change iscut [u] = true to iscut [u] ++ ),
In this way, you can find the most connected parts for the second time.
#include<iostream>#include<stdio.h>#include<string.h>#include<string>#include<map>#include<stack>#include<queue>#include<vector>#include<stdlib.h>using namespace std;#define M 5005int pre[M],dfs_clock,iscut[M],low[M],bcc_cnt,bccno[M];vector<int>G[M],bcc[M];struct Edge{ int from,to;}edge[M*2];int edge_num,head[M];void add_edge(int u,int v){ edge[edge_num].from=v; edge[edge_num].to=head[u]; head[u]=edge_num++;}int del;int dfs(int u,int fa){ int lowu=pre[u]=++dfs_clock; for(int i=head[u];i!=-1;i=edge[i].to) { int v=edge[i].from; if(v==del) continue; if(!pre[v]) { int lowv=dfs(v,u); lowu=min(lowv,lowu); if(lowv>=pre[u]) iscut[u]++; } else if(pre[v]<pre[u]&&v!=fa) lowu=min(lowu,pre[v]); } if(fa<0) iscut[u]--; return lowu;}void init(){ dfs_clock=0; memset(pre,0,sizeof(pre)); memset(iscut,0,sizeof(iscut)); memset(bccno,0,sizeof(bccno));}void find_bcc(int n){ int ans=0; for(int i=0;i<n;i++) { init(); del=i; int sum=0; for(int j=0;j<n;j++) { if(j!=i&&!pre[j]) { sum++; dfs(j,-1); } } int maxcut=-1; for(int j=0;j<n;j++) { if(j!=i) maxcut=max(maxcut,iscut[j]); } ans=max(ans,sum+maxcut); } printf("%d\n",ans);}int main(){ int n,m; while(~scanf("%d%d",&n,&m)) { int a,b; edge_num=0; memset(head,-1,sizeof(head)); while(m--) { scanf("%d%d",&a,&b); add_edge(a,b); add_edge(b,a); } find_bcc(n); } return 0;}