Test instructions: For an undirected graph, there are a maximum of several connected components after removing two different points.
Idea: Enumerate each point, assuming that the point is removed, and then the graph to cut points after the number of connected components, update the largest. The algorithm is relatively simple, but pay attention to several details:
1: The original image may not be connected.
2: Some connected components have only one point, when the point of the time, connected components-1;
Review to find a good problem of cutting points!
#include <iostream> #include <cstdio> #include <vector>using namespace Std;int n,m;vector<vector <int> >e (10010); int dfn[5010];int low[5010];int vis[5010];int times=0;int subset[5010];int root=0;int rf=0; int son=0;void tarjan (int u,int FA)//No map Tarjan record father {if (U==RF) return; Is the point of enumeration, which is omitted. dfn[u]=low[u]=times++; for (int i=0;i<e[u].size (); i++) {int v=e[u][i]; if (V==RF) continue; Notice here that the point of the shed is not the if (!vis[v]) {vis[v]=1; Tarjan (V,u); if (Low[v]<low[u]) low[u]=low[v]; if (u==root)//Cut point is the case of the root {son++; } else//other case {if (Dfn[u]<=low[v])//subset "U" +1 records the connection formed after the cut point of U The number of subset[u]++ components; }} else if (V!=FA)//Condition Note {if (Dfn[v]<low[u]) low[u]=dfn[v]; }} return; int main () {while (~scanf ("%d%d", &n,&m)) {for (int i=0;i<=n;i++) {Dfn[i]=low[i]=subse t[i]=vis[i]=0; E[i].clear (); } int TA,TB; for (int i=0;i<m;i++) {scanf ("%d%d", &TA,&TB); E[ta].push_back (TB); E[tb].push_back (TA); } int maxx=0; for (int i=0;i<n;i++)//Enumerate each point {rf=i; I shed to vis[rf]=1; int scc=0; int maxson=0; for (int iii=0;iii<n;iii++)//Consider the original is not connected! {if (!VIS[III]) {vis[iii]=1; ROOT=III; Tarjan (iii,-1); scc++; The number of connected components if (Son>maxson) Maxson=son; Find the largest son son=0 of the root of each connected component; Each connected component is updated with son} if (E[i].size () ==0) scc--; Watch your point!!! : The connected component has only oneA little bit! If you don't, you'll be gone. for (int ii=0;ii<n;ii++)//Take the largest {if (Scc+subset[ii]+1-1>maxx) maxx=scc+subset[ii]+1- 1; } if (Scc+maxson-1>maxx) maxx=maxson+scc-1; for (int j=0;j<n;j++)//Do not forget to update! {dfn[j]=low[j]=subset[j]=vis[j]=0; } son=times=0; } cout<<maxx<<endl; } return 0;}