POJ 2553 The Bottom of a Graph "SCC Tarjan"

Source: Internet
Author: User

The strong connecting review of graph theory begins--

The main topic: give you a map, you want to find such a point set: From this point to get to the point, must be able to return to this point

Idea: The strong connected components in the obvious can get to each other, then consider, after the contraction point if a point has an edge, must not be in the point set, because the shrinking point is a dag, no ring, so must not go back to the original point, so find out the degree of 0 points can be

#include <cstdio>

#include <string.h>

#include <math.h>

#include <algorithm>

#include <iostream>

#include <queue>

#define MAXN 90000

#define INF 0x3f3f3f3f

using namespace Std;

int head[maxn],next[maxn],point[maxn],now=0;

int DFN[MAXN],LOW[MAXN],TIME,COL,STACK[MAXN];

int TOP,BELONG[MAXN],OUT[MAXN];

BOOL INSTACK[MAXN];

void Add (int x,int y)

{

NEXT[++NOW]=HEAD[X];

Head[x]=now;

Point[now]=y;

}

void Tarjan (int k)

{

int u;

Dfn[k]=low[k]=++time;

Instack[k]=1;

Stack[++top]=k;

for (int i=head[k];i;i=next[i])

{

U=point[i];

if (dfn[u]==0)

{

Tarjan (U);

Low[k]=min (Low[u],low[k]);

}

else if (Instack[u]) low[k]=min (Low[k],low[u]);

}

if (Low[k]==dfn[k])

{

++col;

Do

{

u=stack[top--];

instack[u]=0;

Belong[u]=col;

}while (U!=K);

}

}

int main ()

{

int n,m,x,y;

while (1)

{

scanf ("%d", &n);

if (n==0) break;

scanf ("%d", &m);

Now=0;memset (head,0,sizeof (head));

Top=0;memset (instack,0,sizeof (instack));

Memset (out,0,sizeof (out));

memset (dfn,0,sizeof (DFN));

for (int i=1;i<=m;i++)

{

scanf ("%d%d", &x,&y);

Add (x, y);

}

for (int i=1;i<=n;i++) if (dfn[i]==0) Tarjan (i);

for (int i=1;i<=n;i++)

{

for (int j=head[i];j;j=next[j])

{

int U=POINT[J];

if (Belong[i]!=belong[u]) out[belong[i]]++;

}

}

int flag=1;

for (int i=1;i<=n;i++)

{

if (flag && out[belong[i]]==0)

{

printf ("%d", I);

Flag^=flag;

}

else if (out[belong[i]]==0) printf ("%d", I);

}

printf ("\ n");

}

return 0;

}

POJ 2553 The Bottom of a Graph "SCC Tarjan"

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.