[Kuangbin take you to fly] thematic nine connected graph D - Network POJ - 3694

This question is also very simple, but in the case of seeking the cutting edge of their number, every entered, adding new edges, and calculate the number of cutting edge again.

We tarjan can find the original and the number of bridges, of course, we can not add violent side, and then to solve, then ask how it? ? ?

In fact, very simple, we can solve LCA, we add a new edge between a and b point two points, then the equivalent of connectivity the a, b, then the original a, b, and its bridge on the LCA, not become bridge, why? ? ? Very simple

After I joined this new side, so these two points, the LCA form a ring, we know that the ring is not above a certain line bridge, so we can by looking for LCA, we calculate the reduction of the bridge.

 

#include<iostream>
#include<string.h>
#include<algorithm>
#include<stdio.h>
using namespace std;
const int SIZE = 400010;
int head[SIZE],ver[SIZE*2],Next[SIZE*2];
int dfn[SIZE],low[SIZE],fa[SIZE],depth[SIZE],n,m,tot,num;
bool brige[SIZE*2];
int cnt;
void add(int x,int y){
   ver[++tot]=y,Next[tot]=head[x],head[x]=tot;
}
void tarjan(int x,int pre){
     dfn[x]=low[x]=++num;
     depth[x]=depth[pre]+1;
     for (int i=head[x];i;i=Next[i]){
        int y=ver[i];
        if (y==pre)continue;
        if (!dfn[y]){
            fa[y]=x;
            tarjan(y,x);
            low[x]=min(low[x],low[y]);
            if (low[y]>dfn[x]){
                cnt++; 
                Brige [Y] = . 1 ; 
            } 
        } the else Low [X] = min (Low [X], DFN [Y]); 
     } 
} 
void the LCA ( int U, int V) {
     / * 
    
    * / 
   the while (depth [U ] < depth [v]) {
         / * 
        If u v a height lower than the height of the case, the continuously adjusted upwards u 
        * / 
       IF (brige [v]) { 
          brige [v] = 0 ; 
          CNT - ; 
       } 
       v = FA [V]; 
   } 
   the while (depth [U]>depth [V]) {
         / * 
        contrary 
        * / 
        IF (brige [U]) { 
            brige [U] = 0 ; 
            CNT - ; 
        } 
        U = FA [U]; 
   } 
   / * 
   If both of the same height, we simultaneously adjust them, adjust them up, until to the LCA 
   * / 
   the while (= U! V) {
        IF (brige [U]) { 
          brige [U] = 0 ; 
          CNT - ; 
       } 
       IF (brige [V ]) { 
          brige [V] = 0 ; 
          CNT -;
       }
       u=fa[u];
       v=fa[v];
   }
}
void init(){
   memset(head,0,sizeof(head));
   memset(ver,0,sizeof(ver));
   memset(Next,0,sizeof(Next));
   memset(dfn,0,sizeof(dfn));
   memset(low,0,sizeof(low));
   for (int i=1;i<=n;i++){
     fa[i]=i;
   }
   cnt=0;
   tot=1;
}
int main(){
  int n,q,m;
  int u,v;
  int ca=1;
  while(~scanf("%d%d",&n,&m)){
     if (n==0 && m==0)break;
      init();
      for (int i=1;i<=m;i++){
        scanf("%d%d",&u,&v);
        add(u,v);
        add(v,u);
      }
      tarjan(1,0);
      scanf("%d",&q);
      printf("Case %d:\n",ca++);
      while(q--){
        scanf("%d%d",&u,&v);
        LCA(u,v);
        printf("%d\n",cnt);
      }

  }
  return 0;
}

 

Guess you like

Origin www.cnblogs.com/bluefly-hrbust/p/11229005.html