The strong connected component and the-tarjan algorithm of the contraction point in the review of graph theory

Source: Internet
Author: User

the strong connected component and the-tarjan algorithm of the contraction point in the review of graph theoryby Rtpyh------------------------------------------------------------------------------------------------"strongly connected components and connected sub-graphs"#define #

In a graph's sub-diagram, any two points can reach each other, that is, there is an interoperability path, then the sub-graph is a strong connected component. (if any two points of an undirected graph can reach each other, then the graph is called a strong connected graph).

Nature

If u is the root of a strongly connected component, then:

(1)u does not exist where the path can be returned to its ancestors .

(2) the subtree of U does not exist and the path can be returned to the ancestor of U.

"Algorithm description"

(1) We first judge each vertex, if it has been run, then do not move, otherwise expand

(2) For each point, we set 2 values to indicate (DFN and low) {Low[i] = the DFN value of the root node of node I},DFN is the timestamp of node I

(3) in the first sequential traversal, dfn[u]=low[u]=++cnt, initializes the sequence

(4) Then the traversed point is pressed into the stack and placed

(5) Enumerate an edge (from,to) in the entire graph with the right end of U, then determine if the left endpoint is already in the stack, and if it is already in the stack, modify Low[u]: Low[u]=max (LOW[U],DFN (To)), if it is in the stack, it is recursively processed, return Low[u]=min (Low[u],low[to])

(6) When the appearance of Dfn[u]==low[u], it indicates that a strong Unicom component has appeared, followed by pop-up and elimination of the mark, at this time cnt++, then the role is the number of statistical unicom blocks

Summary

Time complexity O (n+m) of the above algorithm

Examples

codevs2822 "Love in the Heart"

Title Description Description

"Everyone has a dream, even if they are not the same, can share with you, regardless of success will be touched. Love because in the heart, ordinary but not mediocre, the world is like a maze, but let us now meet our Home. ”

There are n people in the Kingdom of love, in their hearts there is a list of love, which records the person he loves (no self-love situation). Love is transitive, that is, if a love b,b love C, then a also love C.
If there is such a part of people, they love each other, they are beyond all limits, with the collective love Incarnate into a loving angel.
Now, we want to know how many loving angels there will be in this kingdom of love. And if a loving Angel is loved by all others or angels of love, please output this angel of love who is made up of, otherwise output-1.

Enter a description input Description

Line 1th, two number n, M, representing the Kingdom of love there are N people, love relations have M.
2nd to m+1, two numbers A, B, each line represents a love B.

outputs description output Description

The 1th line, a number, represents the love of the country there are many love angels.
Line 2nd, if a loving Angel is loved by all others and loving angels, please output This love Angel is made up of who (from small to large sort), otherwise output-1.

sample input to sample

Sample Input 1:

6 7
1 2
2 3
3 2
4 2
4 5
5 6
6 4


Sample Input 2:

3 3
1 2
2 1
2 3

Sample output Sample outputs

Sample Output 1:

2
2 3

Sample Output 2:

1
-1

Obviously, for the first question of the topic, we just need to run the diagram out of the bare Tarjan.

However, the second question is obviously not so simple, the topic asks us to find the capacity for n-1 of the Unicom block, here to note:

cannot be calculated directly, because your diagram has been destroyed by the first question. So we need to compose again.

Then count the "Papa" at each point and the number of strong unicom components for each point.

Then look for n, then output

Time complexity O (4n+m)

Code:

#include <iostream> #include <cstdio> #include <cstring>using namespace Std;const int Maxn=1001;int s[ Maxn],top;int n,m;int ans;//unicom block number int dfn[maxn],low[maxn];bool vis[maxn],inq[maxn];int belong[maxn];int Head[MaxN], Hav[maxn],h[maxn];struct graph{int To,next;} G[maxn],r[maxn];int cnt1=0,cnt2=0;inline int minx (int a,int b) {return a<b?a:b;} void Addedge (int u,int v) {g[++cnt1].to=v; G[cnt1].next=head[u];head[u]=cnt1;} void Tarjan (int u) {dfn[u]=low[u]=++cnt2;s[++top]=u;vis[u]=inq[u]=true;for (int i=head[u];i!=0;i=g[i].next) {if (!DFN [G[i].to])    {Tarjan (g[i].to);    Low[u]=minx (low[u],low[g[i].to]);  } else if (Inq[g[i].to]) Low[u]=minx (low[u],dfn[g[i].to]); }if (Dfn[u]==low[u]) {ans++;int j=0;while (j!=u) {J=s[top--];inq[j]=false;belong[j]=ans;++hav[ans];}}}  void rebuild () {int i,j,cnt=0;for (i=1;i<=n;i++) for (J=head[i];j!=0;j=g[j].next) if (Belong[g[j].to]!=belong[i]) {  R[++CNT].TO=BELONG[G[J].TO];  R[cnt].next=h[belong[i]];    h[belong[i]]=cnt; }}void work () { int i,ans1=0;  for (i=1;i<=n;i++) {if (!vis[i]) Tarjan (i);  } rebuild ();  for (i=1;i<=ans;i++) if (hav[i]>1) ans1++;  printf ("%d\n", ans1);  Ans1=-1;    for (i=1;i<=ans;i++) if (!h[i]) {if (Ans1!=-1 | | hav[i]==1) {ans1=-1;break;}    else ans1=i;    } if (ans1==-1) printf ("%d", ans1);    else {for (i=1;i<=n;i++) if (belong[i]==ans1) printf ("%d", I);  }}int Main () {scanf ("%d%d", &n,&m);  int i,u,v;   memset (vis,false,sizeof (VIS));  for (i=1;i<=m;i++) {scanf ("%d%d", &u,&v);  Addedge (U,V);  } work (); return 0;}

-----------------------------------------Thanks for watching------------------------------------------

The strong connected component and the-tarjan algorithm of the contraction point in the review of graph theory

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.