poj1236 Tarjan Algorithm Template detailed

Source: Internet
Author: User

Thought:

To do the DFS, use Dfn[i] to represent the number I node in the DFS process of the access sequence number (also known as the start time) with Low[i] to represent the I node in the DFS process I of the node below the start time of the earliest node can reach. Initial time Dfn[i]=low[i]

A search tree is formed during the DFS process. The more nodes that are traversed on the search tree, the smaller the value of the DFN is obviously.

In the DFS process, which node is encountered, which node is put into the stack. Nodes in the stack will only be out of the stack if they have all the strong connected components they belong to.

If you find that a node u has an edge attached to node V in the stack in the search tree, the low value of update U is dfn[v] (update to LOW[V).
If a node u has been terminated by DFS access, and its low value equals the DFN value at this point, then all nodes that you can reach will not be reachable to any node that was previously accessed by DFS before u----then the node U is the root of a strongly connected component in the DFS search tree.

When all the nodes in the stack are ejected, including U, a strong connected component is found

Take POJ 1236 Network of schools as an example to illustrate

192K

0MS
#include <string.h>
#include <stdio.h>
#define V 105
#define E 100500

struct Edge
{
int to,next;
}edge[e];
int Head[v], E, n;

int indeg[v], outdeg[v]; The degree of entry and the degrees of the point
int belong[v], low[v], Dfn[v], SCC, cnt;//dfn[]: The time traversing to the U point, the smallest low[]:u of points reachable by Dfn[v]
int s[v], top;
Whether the bool vis[v];//v is in the stack

int Addedge (int u, int v)
{
Edge[e].to =v;
edge[e].next= Head[u];
Head[u] =e++;
Return0;
}
void Tarjan (int u)
{
int V;
Dfn[u] =low[u] = ++cnt;//start Dfn[u] = = Low[u]
s[top++] =u;//no matter 3,721 in the stack
Vis[u] =true;
for (inti=head[u]; i!=-1; i=edge[i].next)
{
V =edge[i].to;
if (dfn[v]== 0)//If the V-point has not been traversed
{
Tarjan (v);//Traverse down
Low[u] =low[u] < Low[v]? LOW[U]: low[v];//ensure low[u] min.
}
else if (Vis[v] && Low[u] >dfn[v])//v in the stack, modify Low[u]
Low[u] =dfn[v];
}
if (dfn[u]== Low[u])//u to traverse the root of the tree in the strongly connected component
{
++SCC;
Do
{
All points to u in the V =s[--top];//stack belong to this strong connected component, and the stack is retired
VIS[V] =false;
BELONG[V] =SCC;
} while (u!= v);
}

}

int Solve ()
{
SCC = Top =cnt = 0;
memset (dfn,0, sizeof (DFN));
memset (vis,false, sizeof (VIS));
for (intu=1; u<=n; ++u)
if (dfn[u]== 0)
Tarjan (U);
RETURNSCC;
}

void Count_deg ()
{
memset (indeg, 0, sizeof (INDEG));
memset (outdeg, 0, sizeof (OUTDEG));
for (intu=1; u<=n; ++u)
for (inti=head[u]; i!=-1; i=edge[i].next)
{
int v =edge[i].to;
if (belong[u]! = Belong[v])
{
indeg[belong[v]]++;
outdeg[belong[u]]++;
}
}
}

int main ()
{
int u, v,i;
while (~SCANF ("%d", &n))
{
e = 0;
memset (head,-1, sizeof (head));
for (u=1;u<=n; ++u)
while (scanf ("%d", &v) &&v! = 0)
Addedge (U,V);
Solve ();
if (SCC ==1)
printf ("1\n0\n");
Else
{
Count_deg ();
int inc = 0,OUTC = 0;
for (i=1;i<=scc; ++i)
{
if (indeg[i]== 0)
inc++;
if (outdeg[i] = = 0)
outc++;
}
printf ("%d\n%d\n", Inc, (inc > OUTC INC:OUTC));
}
}
Return0;
}

poj1236 Tarjan Algorithm Template detailed

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.