zoukankan      html  css  js  c++  java
  • Tarjan 模板

    感谢 键盘里的青春 前辈详细讲解

    先开坑

     1 #include<iostream>
     2 #include<cstdio>
     3 #include<cstring>
     4 using namespace std;
     5 
     6 const int MAXN=1000+5;
     7 struct Edge
     8 {
     9     int to,next;
    10 }E[MAXN];
    11 int node,head[MAXN];
    12 int DFN[MAXN],LOW[MAXN],stack[MAXN];
    13 int vis[MAXN],index,top;
    14 
    15 void insert(int u,int v)
    16 {
    17     E[++node]=(Edge){v,head[u]};head[u]=node;
    18 }
    19 
    20 void tarjan(int x)
    21 {
    22     DFN[x]=LOW[x]=++index;
    23     stack[++top]=x;
    24     vis[x]=1;
    25     for(int i=head[x];i;i=E[i].next)
    26     {
    27         if(!DFN[E[i].to])
    28         {
    29             tarjan(E[i].to);
    30             LOW[x]=min(LOW[x],LOW[E[i].to]);
    31         }
    32         else if(vis[E[i].to])
    33             LOW[x]=min(LOW[x],DFN[E[i].to]);
    34     }
    35     int now;
    36     if(LOW[x]==DFN[x])
    37     {
    38         do
    39         {
    40             now=stack[top--];
    41             printf("%d ",now);
    42             vis[now]=0;
    43         }while(x!=now);
    44         printf("
    ");
    45     }
    46 }
    47 
    48 int main()
    49 {
    50     int n,m;
    51     scanf("%d %d",&n,&m);
    52     for(int i=1;i<=m;i++)
    53     {
    54         int x,y;
    55         scanf("%d %d",&x,&y);
    56         insert(x,y);
    57     }
    58     for(int i=1;i<=n;i++)
    59         if(!DFN[i]) tarjan(i);
    60     return 0;
    61 }
  • 相关阅读:
    Effective C++第三遍
    SQL基础教程
    hibernate 数据关联多对多
    hibernate 数据关联一对一
    hibernate 数据关联一对多
    hibernate Criteria查询
    hibernate HQL查询
    hibernate 持久化对象的生命周期
    hibernate的配置
    微博登录
  • 原文地址:https://www.cnblogs.com/InWILL/p/9343655.html
Copyright © 2011-2022 走看看