POJ 2186 Tarjan to find strong connected components

Source: Internet
Author: User

The example that sister has told. After playing for two days to remember to do seemingly province after the game really become lazy ... You can go to Beijing to play in two days!

By the way, take a look at this. Algorithm for finding strong connected components

1 Only one time DFS relies on the stack to implement the Tarjan algorithm every time you go to a point to press it into the stack each time the point connected to the point processing complete judgment whether low[u]==dfn[u] if the stack top element equals u before exiting (when the stack top element equals u also need pop) Each time the point of the stack is the same strong connected component storage graph can be used as a chain forward star can also be used in the adjacency matrix more can be used by the vector sister Rachel said it will not time out I believe

2 two times Dfs Kosara need to get a reverse graph in the input graph so that the second DFS uses to start a DFS every time a point is left it pushes it into the stack. When all points are traversed and searched from the top of the stack, the searched point belongs to the same strong connected component. .. Also need to use the stack ...

It is important to note that if you use Tarjan when you judge Dfn[v], you need to have the IF condition in else (!id[v])

#include <stdio.h> #include <string.h> #include <algorithm> #include <map> #include <math.h > #include <iostream> #include <stack> #include <vector>using namespace Std;int n,m;int id[10050]; int dfn[10050];int low[10050];int ans[10050];int cd[10050];int point[10050];int num;int cnt;int tot;vector<int >q    [10050];stack<int >s;void Init () {for (int i=1;i<=n;i++) {id[i]=0;    dfn[i]=0;    low[i]=0;    ans[i]=0;    cd[i]=0;    Point[i]=-1; Q[i].clear ();} Num=cnt=tot=0;while (!s.empty ()) S.pop ();}    void Dfs (int u) {dfn[u]=low[u]=++cnt;    S.push (U);    int V;    int siz=q[u].size ();        for (int i=0;i<siz;i++) {v=q[u][i];            if (!dfn[v]) {DFS (v);        Low[u]=min (Low[u],low[v]);        } else if (!id[v]) {low[u]=min (low[u],dfn[v]);        }} if (Low[u]==dfn[u]) {int temp=0;        num++;            while (!s.empty ()) {int v=s.top (); S.poP ();            temp++;            Id[v]=num;        if (v==u) break;    } ans[num]=temp; }}struct node{int V,nex;};    Node a[10050];void Add (int u,int v) {a[tot].v=v;    A[tot].nex=point[u];    Point[u]=tot; tot++;}    void JT () {for (int i=1;i<=n;i++) {int siz=q[i].size ();        for (int k=0;k<siz;k++) {int v=q[i][k];        if (Id[v]!=id[i]) {cd[id[i]]++;        }}}}void did () {int can=0;int p=-1;for (int i=1;i<=num;i++) {if (cd[i]==0) {can++;    P=i; }}if (can==1) {printf ("%d\n", Ans[p]);} else printf ("0\n");}    int main () {while (cin>>n>>m) {init ();        for (int i=1;i<=m;i++) {int u,v;        cin>>u>>v;    Q[u].push_back (v);    } for (int i=1;i<=n;i++) {if (!dfn[i]) DFS (i);    } JT (); Did ();}}

  

POJ 2186 Tarjan to find strong connected components

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.