链接:
http://poj.org/problem?id=3694
http://acm.hust.edu.cn/vjudge/contest/view.action?cid=82833#problem/D
这部分学的不是很好,还需要努力!
代码:
1 #include<cstdio> 2 #include<cstdlib> 3 #include<cmath> 4 #include<iostream> 5 #include<algorithm> 6 #include<cstring> 7 #include<vector> 8 using namespace std; 9 #define N 100005 10 11 struct Edage{int v, next, used;} e[N<<2]; 12 13 int low[N], dfn[N], Head[N], fa[N], Time, cnt; 14 int IsBridge[N], nbridge; 15 16 void Init(int n) 17 { 18 nbridge = Time = cnt = 0; 19 20 for(int i=0; i<=n; i++) 21 { 22 Head[i] = -1; 23 dfn[i] = 0; 24 IsBridge[i] = 0; 25 } 26 } 27 void Add(int u, int v) 28 { 29 e[cnt].v = v; 30 e[cnt].next = Head[u]; 31 e[cnt].used = false; 32 Head[u] = cnt++; 33 } 34 void Tarjan(int u, int father) 35 { 36 int v; 37 38 fa[u] = father; 39 low[u] = dfn[u] = ++Time; 40 41 for(int j=Head[u]; j!=-1; j=e[j].next) 42 { 43 if(!e[j].used) 44 { 45 e[j].used = e[j^1].used = true; 46 v = e[j].v; 47 if(!dfn[v]) 48 { 49 Tarjan(v, u); 50 low[u] = min(low[u], low[v]); 51 52 if(low[v]>dfn[u]) 53 { 54 IsBridge[v] = true; 55 nbridge++; 56 } 57 } 58 else 59 low[u] = min(low[u], dfn[v]); 60 } 61 } 62 } 63 void LCA(int u, int v) 64 { 65 if(dfn[u]<dfn[v]) 66 swap(u, v); 67 68 while(dfn[u] > dfn[v]) 69 { 70 if(IsBridge[u]) nbridge--; 71 IsBridge[u] = false; 72 u = fa[u]; 73 } 74 75 while(u!=v) 76 { 77 if(IsBridge[u]) nbridge--; 78 if(IsBridge[v]) nbridge--; 79 IsBridge[u] = IsBridge[v] = false; 80 u = fa[u], v = fa[v]; 81 } 82 } 83 84 int main() 85 { 86 int n, m, k=1; 87 while(scanf("%d%d", &n, &m), n+m) 88 { 89 int i, u, v; 90 91 Init(n); 92 for(i=0; i<m; i++) 93 { 94 scanf("%d%d", &u, &v); 95 Add(u, v); 96 Add(v, u); 97 } 98 99 Tarjan(1, 1); 100 101 printf("Case %d: ", k++); 102 scanf("%d", &m); 103 for(i=0; i<m; i++) 104 { 105 scanf("%d%d", &u, &v); 106 LCA(u, v); 107 printf("%d ", nbridge); 108 } 109 printf(" "); 110 } 111 return 0; 112 }
代码:
1 #include <iostream> 2 #include <cstdlib> 3 #include <cstdio> 4 #include <algorithm> 5 #include <vector> 6 #include <queue> 7 #include <cmath> 8 #include <stack> 9 #include <cstring> 10 usingnamespace std; 11 #define INF 0xfffffff 12 #define min(a,b) (a<b?a:b) 13 #define maxn 100005 14 int m, n, Time, ans; 15 int dfn[maxn], low[maxn], Father[maxn], bridge[maxn]; 16 vector<int> G[maxn]; 17 18 void init() 19 { 20 memset(dfn, 0, sizeof(dfn)); 21 memset(low, 0, sizeof(low)); 22 memset(bridge, 0, sizeof(bridge)); 23 memset(Father, 0, sizeof(Father)); 24 Time = ans = 0; 25 26 for(int i=0; i<=n; i++) 27 G[i].clear(); 28 } 29 30 void Tarjan(int u,int fa) 31 { 32 dfn[u] = low[u] = ++Time; 33 Father[u] = fa; 34 int len = G[u].size(), v; 35 36 for(int i=0; i<len; i++) 37 { 38 v = G[u][i]; 39 40 if( !low[v] ) 41 { 42 Tarjan(v, u); 43 low[u] = min(low[u], low[v]); 44 45 if(dfn[u] < low[v]) 46 { 47 bridge[v] ++; 48 ans ++; 49 } 50 } 51 elseif(v != fa) 52 { 53 low[u] = min(low[u], dfn[v]); 54 55 if(dfn[u] < low[v]) 56 { 57 bridge[v] ++; 58 ans --; 59 } 60 } 61 62 } 63 } 64 void Lca(int a,int b) 65 { 66 if(a == b) 67 return ; 68 69 if(dfn[a] > dfn[b]) 70 { 71 if( bridge[a] == 1) 72 { 73 bridge[a] = 0; 74 ans --; 75 } 76 Lca(Father[a], b); 77 } 78 else 79 { 80 if(bridge[b] == 1) 81 { 82 bridge[b] = 0; 83 ans --; 84 } 85 Lca(a, Father[b]); 86 } 87 } 88 89 int main() 90 { 91 int cas = 1; 92 while(scanf("%d %d",&n, &m), m+n) 93 { 94 int Q, a, b; 95 init(); 96 while(m --) 97 { 98 scanf("%d %d",&a, &b); 99 G[a].push_back(b); 100 G[b].push_back(a); 101 } 102 103 scanf("%d", &Q); 104 Tarjan(1, 0); 105 // printf("%d ", ans); 106 printf("Case %d: ", cas ++); 107 while(Q --) 108 { 109 scanf("%d %d",&a, &b); 110 Lca(a, b); 111 printf("%d ", ans); 112 } 113 } 114 return0; 115 } 116 /* 117 118 4 4 119 1 2 120 2 1 121 2 3 122 1 4 123 2 124 1 2 125 3 4 126 127 */