zoukankan      html  css  js  c++  java
  • Tarjan-SCC-NOIP2015message

    This article is made by Jason-Cow.
    Welcome to reprint.
    But please post the writer's address.

    http://www.cnblogs.com/JasonCow/

    连临街表都不用,每个节点的出度为1,数组搞定访问

    #include <cstdio>
    #include <algorithm>
    using namespace std;
    #define N 200010
    int GI(){
      int x=0,c=getchar(),f=0;
      while(c<'0'||c>'9'){if(c=='-')f=1;c=getchar();}
      while(c>='0'&&c<='9')x=x*10+c-'0',c=getchar();
      return f?-x:x;
    }
    int dfn[N],low[N],s[N],in[N],idx,top,ans=1<<30,to[N];
    void tarjan(int u){
        dfn[u]=low[u]=++idx;
        s[++top]=u,in[u]=1;
        int v=to[u];
        if(!dfn[v])tarjan(v),low[u]=min(low[u],low[v]);
        else if(in[v])       low[u]=min(low[u],dfn[v]);
        if(low[u]==dfn[u]){
            int sz=0;
            do++sz,in[top]=0;
            while(s[top--]!=u);
            if(sz>1)ans=min(ans,sz);
        }
    }
    int main(){
        int n=GI(),u,v;
        for(v=1;v<=n;v++)to[v]=GI();
        for(u=1;u<=n;u++)tarjan(u);
        return printf("%d",ans),0;
    }



    ~~Jason_liu O(∩_∩)O
  • 相关阅读:
    LINUX和git
    drf [Django REST Framework]
    python用法小技巧
    爬虫
    django入门到精通
    前端框架
    mysql数据库
    网络编程和并发编程
    面向对象
    python基础
  • 原文地址:https://www.cnblogs.com/JasonCow/p/6814579.html
Copyright © 2011-2022 走看看