hdu2460-Network:邊的雙連通分量

來源:互聯網
上載者:User

標籤:style   blog   color   io   os   ar   for   sp   div   

題目大意:給出一個無向圖以及Q次詢問,每次詢問增加一條無向邊,要求輸出增加這條邊後剩餘的橋的數目。

演算法:類似於求割點的方法,先做一次dfs求出所有的橋,並且維護這棵dfs樹,當一次詢問加入一條邊(a,b)之後,會在dfs上形成一個環,在這個環上的橋都變為非橋,這個環肯定經過a和b的LCA,此時我們只需在求LCA的過程中把經過的為橋的樹邊標記為非橋,同時cnt_bridge--再輸出即可。

需要注意的是樹邊的編號是用樹邊指向的那個節點的編號來表示的,例如樹邊<u,v>用編號v表示。

還有就是加入一個#pragma預先處理指令可以防止爆棧。

 

  1 #include <iostream>  2 #include <stdio.h>  3 #include <cstring>  4 #include <vector>  5 using namespace std;  6 #pragma comment(linker,"/STACk:10240000,10240000")  7   8 const int maxn = 100000 + 10;  9 int low[maxn],pre[maxn], iscut[maxn], dfs_clock=0; 10 bool isbridge[maxn]; 11 vector<int> G[maxn]; 12 int cnt_bridge; 13 int father[maxn]; 14  15 int dfs(int u, int fa) 16 { 17     father[u]=fa; 18     int lowu = pre[u] = ++dfs_clock; 19     int child = 0; 20     for(int i = 0; i < G[u].size(); i++) 21     { 22         int v = G[u][i]; 23         if(!pre[v])   // 沒有訪問過v 24         { 25             child++; 26             int lowv = dfs(v, u); 27             lowu = min(lowu, lowv); // 用後代的low函數更新自己 28             if(lowv > pre[u]) // 判斷邊(u,v)是否為橋 29             { 30                 isbridge[v]=true; 31                 cnt_bridge++; 32             } 33         } 34         else if(pre[v] < pre[u] && v != fa) 35         { 36             lowu = min(lowu, pre[v]); // 用反向邊更新自己 37         } 38     } 39     if(fa < 0 && child == 1) 40         iscut[u] = 0; 41     return low[u]=lowu; 42 } 43  44 void init(int n) 45 { 46     memset(isbridge,false,sizeof isbridge); 47     memset(pre,0,sizeof pre); 48     cnt_bridge=dfs_clock=0; 49     for(int i=0; i<n; i++) 50     { 51         G[i].clear(); 52     } 53 } 54  55 void LCA(int a,int b) 56 { 57     while(pre[a]>pre[b]) 58     { 59         if(isbridge[a]) 60         { 61             isbridge[a]=false; 62             cnt_bridge--; 63         } 64         a=father[a]; 65     } 66     while(pre[b]>pre[a]) 67     { 68         if(isbridge[b]) 69         { 70             isbridge[b]=false; 71             cnt_bridge--; 72         } 73         b=father[b]; 74     } 75     if(a!=b) 76     { 77         while(pre[a]>pre[b]) 78         { 79             if(isbridge[a]) 80             { 81                 isbridge[a]=false; 82                 cnt_bridge--; 83             } 84             a=father[a]; 85         } 86         while(pre[b]>pre[a]) 87         { 88             if(isbridge[b]) 89             { 90                 isbridge[b]=false; 91                 cnt_bridge--; 92             } 93             b=father[b]; 94         } 95     } 96 } 97  98 int main() 99 {100 #ifndef ONLINE_JUDGE101     freopen("in.txt","r",stdin);102 #endif103 104     int n,m,Case=1;105     while(scanf("%d %d",&n,&m),!(n==0 && m==0))106     {107         init(n);108         for(int i=0; i<m; i++)109         {110             int a,b;111             scanf("%d %d",&a,&b);112             a--;113             b--;114             G[a].push_back(b);115             G[b].push_back(a);116         }117 118         // 第一次dfs找出所有的橋119         dfs(0,-1);120         int Q;121         cin>>Q;122         printf("Case %d:\n",Case++);123         while(Q--)124         {125             int a,b;126             scanf("%d %d",&a,&b);127             a--;128             b--;129             LCA(a,b);130             printf("%d\n",cnt_bridge);131             if(Q==0)132                 printf("\n");133         }134     }135 136     return 0;137 }

 

hdu2460-Network:邊的雙連通分量

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.