题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=4612
简单图论题,先求图的边双连通分量,注意,此题有重边(admin还逗比的说没有重边),在用targan算法求的时候,处理反向边需要标记边,然后缩点,在树上求最长链。。
此题在比赛的时候,我的模板数组开小,WA一下午,sd。。。。
1 //STATUS:C++_AC_734MS_37312KB 2 #include <functional> 3 #include <algorithm> 4 #include <iostream> 5 //#include <ext/rope> 6 #include <fstream> 7 #include <sstream> 8 #include <iomanip> 9 #include <numeric> 10 #include <cstring> 11 #include <cassert> 12 #include <cstdio> 13 #include <string> 14 #include <vector> 15 #include <bitset> 16 #include <queue> 17 #include <stack> 18 #include <cmath> 19 #include <ctime> 20 #include <list> 21 #include <set> 22 #include <map> 23 #pragma comment(linker,"/STACK:102400000,102400000") 24 using namespace std; 25 //using namespace __gnu_cxx; 26 //define 27 #define pii pair<int,int> 28 #define mem(a,b) memset(a,b,sizeof(a)) 29 #define lson l,mid,rt<<1 30 #define rson mid+1,r,rt<<1|1 31 #define PI acos(-1.0) 32 //typedef 33 typedef __int64 LL; 34 typedef unsigned __int64 ULL; 35 //const 36 const int N=200010,M=2000010; 37 const int INF=0x3f3f3f3f; 38 const int MOD=100000,STA=8000010; 39 const LL LNF=1LL<<60; 40 const double EPS=1e-8; 41 const double OO=1e15; 42 const int dx[4]={-1,0,1,0}; 43 const int dy[4]={0,1,0,-1}; 44 const int day[13]={0,31,28,31,30,31,30,31,31,30,31,30,31}; 45 //Daily Use ... 46 inline int sign(double x){return (x>EPS)-(x<-EPS);} 47 template<class T> T gcd(T a,T b){return b?gcd(b,a%b):a;} 48 template<class T> T lcm(T a,T b){return a/gcd(a,b)*b;} 49 template<class T> inline T lcm(T a,T b,T d){return a/d*b;} 50 template<class T> inline T Min(T a,T b){return a<b?a:b;} 51 template<class T> inline T Max(T a,T b){return a>b?a:b;} 52 template<class T> inline T Min(T a,T b,T c){return min(min(a, b),c);} 53 template<class T> inline T Max(T a,T b,T c){return max(max(a, b),c);} 54 template<class T> inline T Min(T a,T b,T c,T d){return min(min(a, b),min(c,d));} 55 template<class T> inline T Max(T a,T b,T c,T d){return max(max(a, b),max(c,d));} 56 //End 57 58 struct Edge{ 59 int u,v; 60 }e[M],e2[M]; 61 int first2[N],next2[M],mt2; 62 //bool iscut[M]; 63 int first[N],next[M],pre[N],low[N],bccno[N]; 64 int n,m,mt,bcnt,dfs_clock; 65 stack<int> s; 66 67 int T; 68 int vis[N]; 69 70 void adde(int a,int b) 71 { 72 e[mt].u=a;e[mt].v=b; 73 next[mt]=first[a];first[a]=mt++; 74 e[mt].u=b;e[mt].v=a; 75 next[mt]=first[b];first[b]=mt++; 76 } 77 void adde2(int a,int b) 78 { 79 e2[mt2].u=a;e2[mt2].v=b; 80 next2[mt2]=first2[a];first2[a]=mt2++; 81 e2[mt2].u=b;e2[mt2].v=a; 82 next2[mt2]=first2[b];first2[b]=mt2++; 83 } 84 85 void dfs(int u,int fa) 86 { 87 int i,v; 88 pre[u]=low[u]=++dfs_clock; 89 s.push(u); 90 int cnt=0; 91 for(i=first[u];i!=-1;i=next[i]){ 92 v=e[i].v; 93 if(!pre[v]){ 94 dfs(v,u); 95 low[u]=Min(low[u],low[v]); 96 // if(low[v]>pre[u])iscut[i]=true; //存在割边 97 } 98 else if(fa==v){ //反向边更新 99 if(cnt)low[u]=Min(low[u],pre[v]); 100 cnt++; 101 } 102 else low[u]=Min(low[u],pre[v]); 103 } 104 if(low[u]==pre[u]){ //充分必要条件 105 int x=-1; 106 bcnt++; 107 while(x!=u){ 108 x=s.top();s.pop(); 109 bccno[x]=bcnt; 110 } 111 } 112 } 113 114 void find_bcc() 115 { 116 int i; 117 bcnt=dfs_clock=0;//mem(iscut,0); 118 mem(pre,0);mem(bccno,0); 119 for(i=1;i<=n;i++){ 120 if(!pre[i])dfs(i,-1); 121 } 122 } 123 124 int hig; 125 int dfs2(int u,int p) 126 { 127 int max1=0,max2=0; 128 for (int i=first2[u];i!=-1;i=next2[i]) 129 { 130 int v=e2[i].v; 131 if (v==p) continue; 132 int tmp=dfs2(v,u)+1; 133 if (max1<tmp) max2=max1,max1=tmp; 134 else if (max2<tmp) max2=tmp; 135 } 136 hig=Max(hig,max1+max2); 137 return max1; 138 } 139 140 int main() 141 { 142 // freopen("in.txt","r",stdin); 143 int i,j,a,b,tot; 144 while(~scanf("%d%d",&n,&m) && (n || m)) 145 { 146 mt=0;mem(first,-1); 147 for(i=0;i<m;i++){ 148 scanf("%d%d",&a,&b); 149 adde(a,b); 150 } 151 152 find_bcc(); 153 mem(first2,-1);mt2=0; 154 for(i=0;i<mt;i+=2){ 155 if(bccno[e[i].u]!=bccno[e[i].v]){ 156 adde2(bccno[e[i].u],bccno[e[i].v]); 157 } 158 } 159 hig=0; 160 dfs2(1,-1); 161 162 printf("%d\n",bcnt-1-hig); 163 } 164 return 0; 165 }