Advertisement
dipBRUR

Bridge Tree

Feb 2nd, 2018
118
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.77 KB | None | 0 0
  1. // Bridge Tree
  2. // O(n)
  3.  
  4. struct NODE {
  5.     int v, e;
  6. };
  7. vector <NODE> G[MAXN];
  8. bool isBridge[MAXN],
  9.      vis[MAXN];
  10. int  timer,
  11.      dis[MAXN],
  12.      low[MAXN];
  13.  
  14. void FIND_BRIDGE(int u, int p = -1) {
  15.     vis[u] = 1;
  16.     dis[u] = low[u] = timer++;
  17.     for (NODE E : G[u]) {
  18.         int v = E.v;
  19.         int e = E.e;
  20.         if (v == p)
  21.             continue;
  22.         if (!vis[v]) {
  23.             FIND_BRIDGE(v, u);
  24.             low[u] = min(low[u], low[v]);
  25.             if (dis[u] < low[v])
  26.                 isBridge[e] = 1;
  27.         }
  28.         else {
  29.             low[u] = min(low[u], dis[v]);
  30.         }
  31.     }
  32. }
  33.  
  34. int compo;
  35. bool used[MAXN];
  36. int compo_num[MAXN];
  37. vector <int> TREE[MAXN];
  38.  
  39. void BRIDGE_TREE(int src) {
  40.     used[src] = 1;
  41.     int cur_compo = compo;
  42.     compo_num[src] = cur_compo;
  43.     queue <int> PQ; // for BFS
  44.     PQ.push(src);
  45.     while (!PQ.empty()) {
  46.         int u = PQ.front();
  47.                 PQ.pop();
  48.         for (NODE E : G[u]) {
  49.             int v = E.v;
  50.             int e = E.e;
  51.             if (used[v])
  52.                 continue;
  53.  
  54.            
  55.  
  56.             if (isBridge[e]) {
  57.                 compo++;
  58.                 TREE[cur_compo].push_back(compo);
  59.                 TREE[compo].push_back(cur_compo);
  60.  
  61.                 BRIDGE_TREE(v);
  62.             }
  63.  
  64.             else {
  65.                 PQ.push(v);
  66.  
  67.                 used[v] = 1;
  68.                 compo_num[v] = cur_compo; // v's component number
  69.             }
  70.         }
  71.     }
  72. }
  73.  
  74. int main() {
  75.     // Take input
  76.     for (int i = 0; i < tNode; i++) {  // 0 <= node_num < tNode
  77.         if (!vis[i])
  78.             FIND_BRIDGE(i);
  79.     }
  80.     for (int i = 0; i < tNode; i++) {
  81.         if (!used[i]) {
  82.             BRIDGE_TREE(i);
  83.             compo++;
  84.         }
  85.     }
  86.     // Output
  87. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement