HDU 2460 Network(橋+LCA)

來源:互聯網
上載者:User

標籤:tor   ack   stack   can   pac   dash   mat   clock   add   

http://acm.hdu.edu.cn/showproblem.php?pid=2460

題意:
給出圖,求每次增加一條邊後圖中橋的數量。

 

思路:

先用tarjan演算法找出圖中所有的橋,如果lowv>pre[u],那麼u—v就是橋,此時可以標記一下v。

之後就是利用LCA,找到兩個節點的公用祖先,在這條路徑上的橋就不再是橋了。(此時就相當於這些橋組成的樹,可以在腦海中縮點)

  1 #include<iostream>  2 #include<algorithm>  3 #include<cstring>  4 #include<cstdio>  5 #include<sstream>  6 #include<vector>  7 #include<stack>  8 #include<queue>  9 #include<cmath> 10 #include<map> 11 #include<set> 12 using namespace std; 13 typedef long long ll; 14 typedef pair<int,int> pll; 15 const int INF = 0x3f3f3f3f; 16 const int maxn=100000+5; 17  18 int n, m; 19 int tot; 20 int cnt; 21 int head[maxn]; 22 int dfs_clock; 23 int pre[maxn]; 24 int low[maxn]; 25 int isbridge[maxn]; 26 int p[maxn]; 27  28 struct node 29 { 30     int v; 31     int next; 32 }e[400000+5]; 33  34 void addEdge(int u, int v) 35 { 36     e[tot].v=v; 37     e[tot].next=head[u]; 38     head[u]=tot++; 39 } 40  41 void init() 42 { 43     cnt=0; 44     dfs_clock=0; 45     memset(pre,0,sizeof(pre)); 46     memset(isbridge,0,sizeof(isbridge)); 47     for(int i=1;i<=n;i++)  p[i]=i; 48 } 49  50 int dfs(int u, int fa) 51 { 52     int lowu=pre[u]=++dfs_clock; 53     for(int i=head[u];i!=-1;i=e[i].next) 54     { 55         int v=e[i].v; 56         if(!pre[v]) 57         { 58             p[v]=u; 59             int lowv=dfs(v,u); 60             lowu=min(lowu,lowv); 61             if(lowv>pre[u])   {cnt++;isbridge[v]=1;} 62         } 63         else if(pre[v]<pre[u] && v!=fa) 64             lowu=min(lowu,pre[v]); 65     } 66     return low[u]=lowu; 67 } 68  69 int LCA(int u, int v) 70 { 71     while(low[u]>low[v]) 72     { 73         if(isbridge[u])  {cnt--;isbridge[u]=0;} 74         u=p[u]; 75     } 76     while(low[v]>low[u]) 77     { 78         if(isbridge[v])  {cnt--;isbridge[v]=0;} 79         v=p[v]; 80     } 81     while(u!=v) 82     { 83         if(isbridge[u])  {cnt--;isbridge[u]=0;} 84         if(isbridge[v])  {cnt--;isbridge[v]=0;} 85         u=p[u]; 86         v=p[v]; 87     } 88 } 89  90 int main() 91 { 92     //freopen("in.txt","r",stdin); 93     int kase=0; 94     while(~scanf("%d%d",&n,&m)) 95     { 96         if(n==0 && m==0)  break; 97         tot=0; 98         memset(head,-1,sizeof(head)); 99         while(m--)100         {101             int u,v;102             scanf("%d%d",&u,&v);103             addEdge(u,v);104             addEdge(v,u);105         }106         init();107         dfs(1,-1);108         int q;109         scanf("%d",&q);110         printf("Case %d:\n",++kase);111         while(q--)112         {113             int u,v;114             scanf("%d%d",&u,&v);115             LCA(u,v);116             printf("%d\n",cnt);117         }118         printf("\n");119     }120     return 0;121 }

 

HDU 2460 Network(橋+LCA)

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在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.