A
B warm up
无向图有重边求桥树上DFS遍历找最长路模板。。。
Code #pragma comment(linker,"/STACK:102400000,102400000") #include <iostream> #include <cstdio> #include <cstring> #include <vector> #define PII make_pair #define edge vector< pair<int,int> > #define fill(x,y) memset(x,y,sizeof(x)); using namespace std; const int maxn=200000+10,maxm=1000000+10; edge G[maxn]; int DFN[maxn],Low[maxn]; bool bridge[maxm]; int Index,ans,all,n,m; void Tarjan(int u,int id) { DFN[u]=Low[u]=++Index; for(edge::iterator i=G[u].begin();i!=G[u].end();i++){ int v=i->first,t=i->second; if(t==id) continue; if(!DFN[v]){ Tarjan(v,t); Low[u]=min(Low[v],Low[u]); if(Low[v]==DFN[v]){ all+=(int)(!bridge[t]); bridge[t]=1; } } else Low[u]=min(DFN[v],Low[u]); } } void dfs(int u) { DFN[u]=1; for(edge::iterator i=G[u].begin();i!=G[u].end();i++) if(Low[i->first]<0){ Low[i->first]=Low[u]+(int)bridge[i->second]; dfs(i->first); } } int main() { while(cin>>n>>m){ if((n==0)&&(m==0))break; for(int i=1;i<=n;i++)G[i].clear(); fill(Low,0); fill(DFN,0); fill(bridge,0); Index=0;ans=0;all=0; for(int i=1;i<=m;i++){ int x,y; cin>>x>>y; G[x].push_back(PII(y,i)); G[y].push_back(PII(x,i)); } for(int i=1;i<=n;i++) if(!DFN[i])Tarjan(i,0); fill(DFN,0); for(int i=1;i<=n;i++) if(!DFN[i]){ fill(Low,255);Low[i]=0;dfs(i); int langest=0,s=0; for(int i=1;i<=n;i++) if(langest<Low[i]){ langest=Low[i]; s=i; } fill(Low,255);Low[s]=0;dfs(s); for(int i=1;i<=n;i++) ans=max(ans,Low[i]); } cout<<all-ans<<endl; } return 0; }