题目链接:http://poj.org/problem?id=3177
本题要求的就是最少添加多少条边可变无桥的连通图,和POJ1236差不多,(度为1的边双连通分量的个数+1)/2。
1 //STATUS:C++_AC_47MS_12568KB 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 using namespace std; 24 //define 25 #define pii pair<int,int> 26 #define mem(a,b) memset(a,b,sizeof(a)) 27 #define lson l,mid,rt<<1 28 #define rson mid+1,r,rt<<1|1 29 #define PI acos(-1.0) 30 //typedef 31 typedef __int64 LL; 32 typedef unsigned __int64 ULL; 33 //const 34 const int N=5010; 35 const int INF=0x3f3f3f3f; 36 const int MOD=100000,STA=8000010; 37 const LL LNF=1LL<<60; 38 const double EPS=1e-8; 39 const double OO=1e15; 40 const int dx[4]={-1,0,1,0}; 41 const int dy[4]={0,1,0,-1}; 42 //Daily Use ... 43 inline int sign(double x){return (x>EPS)-(x<-EPS);} 44 template<class T> T gcd(T a,T b){return b?gcd(b,a%b):a;} 45 template<class T> T lcm(T a,T b){return a/gcd(a,b)*b;} 46 template<class T> inline T Min(T a,T b){return a<b?a:b;} 47 template<class T> inline T Max(T a,T b){return a>b?a:b;} 48 template<class T> inline T Min(T a,T b,T c){return min(min(a, b),c);} 49 template<class T> inline T Max(T a,T b,T c){return max(max(a, b),c);} 50 template<class T> inline T Min(T a,T b,T c,T d){return min(min(a, b),min(c,d));} 51 template<class T> inline T Max(T a,T b,T c,T d){return max(max(a, b),max(c,d));} 52 //End 53 54 struct Edge{ 55 int u,v; 56 }e[N*4]; 57 //bool iscut[N]; 58 bool vis[N*N/2]; 59 int first[N],next[N*4],pre[N],low[N],bccno[N],cnt[N]; 60 int n,m,mt,bcnt,dfs_clock; 61 stack<int> s; 62 63 void adde(int a,int b) 64 { 65 e[mt].u=a;e[mt].v=b; 66 next[mt]=first[a];first[a]=mt++; 67 e[mt].u=b;e[mt].v=a; 68 next[mt]=first[b];first[b]=mt++; 69 } 70 71 void dfs(int u,int fa) 72 { 73 int i,v; 74 pre[u]=low[u]=++dfs_clock; 75 s.push(u); 76 for(i=first[u];i!=-1;i=next[i]){ 77 v=e[i].v; 78 if(!pre[v]){ 79 dfs(v,u); 80 low[u]=Min(low[u],low[v]); 81 } 82 else if(v!=fa && pre[v]<pre[u]){ 83 low[u]=Min(low[u],pre[v]); 84 } 85 } 86 if(low[u]==pre[u]){ 87 int x=-1; 88 bcnt++; 89 while(x!=u){ 90 x=s.top();s.pop(); 91 bccno[x]=bcnt; 92 } 93 } 94 } 95 96 void find_bcc() 97 { 98 int i; 99 bcnt=dfs_clock=0;//mem(iscut,0); 100 mem(pre,0);mem(bccno,0); 101 for(i=1;i<=n;i++){ 102 if(!pre[i])dfs(i,-1); 103 } 104 } 105 106 int main() 107 { 108 // freopen("in.txt","r",stdin); 109 int i,j,a,b,ans,t; 110 while(~scanf("%d%d",&n,&m)) 111 { 112 mt=0; 113 mem(first,-1); 114 mem(vis,0); 115 while(m--){ 116 scanf("%d%d",&a,&b); 117 if(a<b)swap(a,b); 118 t=a*(a-1)/2+b; 119 if(!vis[t]){ 120 vis[t]=true; 121 adde(a,b); 122 } 123 } 124 125 find_bcc(); 126 ans=0; 127 if(bcnt>1){ 128 mem(cnt,0); 129 for(i=0;i<mt;i+=2){ 130 if(bccno[e[i].u]!=bccno[e[i].v]){ 131 cnt[bccno[e[i].u]]++; 132 cnt[bccno[e[i].v]]++; 133 } 134 } 135 for(i=1;i<=bcnt;i++){ 136 if(cnt[i]<=1)ans++; 137 } 138 } 139 140 printf("%d\n",(ans+1)/2); 141 } 142 return 0; 143 }