题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=4612
简单图论题,先求图的边双连通分量,注意,此题有重边(admin还逗比的说没有重边),在用targan算法求的时候,处理反向边需要标记边,然后缩点,在树上求最长链。。
此题在比赛的时候,我的模板数组开小,WA一下午,sd。。。。
//STATUS:C++_AC_734MS_37312KB
#include <functional>
#include <algorithm>
#include <iostream>
//#include <ext/rope>
#include <fstream>
#include <sstream>
#include <iomanip>
#include <numeric>
#include <cstring>
#include <cassert>
#include <cstdio>
#include <string>
#include <vector>
#include <bitset>
#include <queue>
#include <stack>
#include <cmath>
#include <ctime>
#include <list>
#include <set>
#include <map>
#pragma comment(linker,"/STACK:102400000,102400000")
using namespace std;
//using namespace __gnu_cxx;
//define
#define pii pair<int,int>
#define mem(a,b) memset(a,b,sizeof(a))
#define lson l,mid,rt<<1
#define rson mid+1,r,rt<<1|1
#define PI acos(-1.0)
//typedef
typedef __int64 LL;
typedef unsigned __int64 ULL;
//const
const int N=,M=;
const int INF=0x3f3f3f3f;
const int MOD=,STA=;
const LL LNF=1LL<<;
const double EPS=1e-;
const double OO=1e15;
const int dx[]={-,,,};
const int dy[]={,,,-};
const int day[]={,,,,,,,,,,,,};
//Daily Use ...
inline int sign(double x){return (x>EPS)-(x<-EPS);}
template<class T> T gcd(T a,T b){return b?gcd(b,a%b):a;}
template<class T> T lcm(T a,T b){return a/gcd(a,b)*b;}
template<class T> inline T lcm(T a,T b,T d){return a/d*b;}
template<class T> inline T Min(T a,T b){return a<b?a:b;}
template<class T> inline T Max(T a,T b){return a>b?a:b;}
template<class T> inline T Min(T a,T b,T c){return min(min(a, b),c);}
template<class T> inline T Max(T a,T b,T c){return max(max(a, b),c);}
template<class T> inline T Min(T a,T b,T c,T d){return min(min(a, b),min(c,d));}
template<class T> inline T Max(T a,T b,T c,T d){return max(max(a, b),max(c,d));}
//End struct Edge{
int u,v;
}e[M],e2[M];
int first2[N],next2[M],mt2;
//bool iscut[M];
int first[N],next[M],pre[N],low[N],bccno[N];
int n,m,mt,bcnt,dfs_clock;
stack<int> s; int T;
int vis[N]; void adde(int a,int b)
{
e[mt].u=a;e[mt].v=b;
next[mt]=first[a];first[a]=mt++;
e[mt].u=b;e[mt].v=a;
next[mt]=first[b];first[b]=mt++;
}
void adde2(int a,int b)
{
e2[mt2].u=a;e2[mt2].v=b;
next2[mt2]=first2[a];first2[a]=mt2++;
e2[mt2].u=b;e2[mt2].v=a;
next2[mt2]=first2[b];first2[b]=mt2++;
} void dfs(int u,int fa)
{
int i,v;
pre[u]=low[u]=++dfs_clock;
s.push(u);
int cnt=;
for(i=first[u];i!=-;i=next[i]){
v=e[i].v;
if(!pre[v]){
dfs(v,u);
low[u]=Min(low[u],low[v]);
// if(low[v]>pre[u])iscut[i]=true; //存在割边
}
else if(fa==v){ //反向边更新
if(cnt)low[u]=Min(low[u],pre[v]);
cnt++;
}
else low[u]=Min(low[u],pre[v]);
}
if(low[u]==pre[u]){ //充分必要条件
int x=-;
bcnt++;
while(x!=u){
x=s.top();s.pop();
bccno[x]=bcnt;
}
}
} void find_bcc()
{
int i;
bcnt=dfs_clock=;//mem(iscut,0);
mem(pre,);mem(bccno,);
for(i=;i<=n;i++){
if(!pre[i])dfs(i,-);
}
} int hig;
int dfs2(int u,int p)
{
int max1=,max2=;
for (int i=first2[u];i!=-;i=next2[i])
{
int v=e2[i].v;
if (v==p) continue;
int tmp=dfs2(v,u)+;
if (max1<tmp) max2=max1,max1=tmp;
else if (max2<tmp) max2=tmp;
}
hig=Max(hig,max1+max2);
return max1;
} int main()
{
// freopen("in.txt","r",stdin);
int i,j,a,b,tot;
while(~scanf("%d%d",&n,&m) && (n || m))
{
mt=;mem(first,-);
for(i=;i<m;i++){
scanf("%d%d",&a,&b);
adde(a,b);
} find_bcc();
mem(first2,-);mt2=;
for(i=;i<mt;i+=){
if(bccno[e[i].u]!=bccno[e[i].v]){
adde2(bccno[e[i].u],bccno[e[i].v]);
}
}
hig=;
dfs2(,-); printf("%d\n",bcnt--hig);
}
return ;
}