Collection of Tarjan templates in the garbage collection Series

Source: Internet
Author: User

Collection of Tarjan templates in the garbage collection Series

Returned by the madman

 

I. Strongly Connected subgraph

 

1 # include <iostream> 2 # include <cstdio> 3 # include <cstring> 4 # include <cmath> 5 # include <algorithm> 6 # include <climits> 7 # include <vector> 8 # include <queue> 9 # include <cstdlib> 10 # include <string> 11 # include <set> 12 # include <stack> 13 # define ll long long14 # define PII pair <int, int> 15 # define INF 0x3f3f3f16 using namespace STD; 17 const int maxn = 20100; 18 int dfn [maxn], low [maxn], belong [m AXN]; 19 bool instack [maxn]; 20 vector <int> G [maxn]; 21 stack <int> STK; 22 int n, m, CNT, SCC; 23 void Tarjan (int u) {24 dfn [u] = low [u] = ++ CNT; 25 STK. push (U); 26 instack [u] = true; 27 for (INT I = 0; I <G [u]. size (); I ++) {28 If (! Dfn [G [u] [I]) {29 Tarjan (G [u] [I]); 30 low [u] = min (low [u], low [G [u] [I]); 31} else if (instack [G [u] [I]) low [u] = min (low [u], dfn [G [u] [I]); 32} 33 If (dfn [u] = low [u]) {34 SCC ++; 35 int V; 36 do {37 v = STK. top (); 38 instack [v] = false; 39 belong [v] = SCC; 40 STK. pop (); 41} while (V! = U); // The de point in each strongly connected block, and the scc42} 43} 44 int main () {45 int I, j, U, V,, b; 46 While (~ Scanf ("% d", & N, & M) {47 for (I = 0; I <= N; I ++) {48 dfn [I] = belong [I] = 0; 49 instack [I] = false; 50g [I]. clear (); 51} 52 CNT = SCC = 0; 53 While (! STK. empty () STK. pop (); 54 for (I = 1; I <= m; I ++) {55 scanf ("% d", & U, & V ); 56G [u]. push_back (V); 57} 58 for (I = 1; I <= N; I ++) 59 If (! Dfn [I]) Tarjan (I); 60} 61 Return 0; 62}
View code

 

 

 

Ii. Cut Point

3. Cut Edge/Bridge

Iv. Find the LCA

 

Collection of Tarjan templates in the garbage collection Series

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

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.