zoukankan      html  css  js  c++  java
  • tarjan缩点

    整理了下模板。。。

     1 #include<iostream>
     2 #include<cstdio>
     3 #include<cmath>
     4 #include<algorithm>
     5 #include<queue>
     6 #include<cstring>
     7 #define PAU putchar(' ')
     8 #define ENT putchar('
    ')
     9 using namespace std;
    10 const int maxn=100000+10,maxm=500000+10;
    11 int low[maxn],dfn[maxn],s[maxn],beg[maxn],top,scc,cz;bool ins[maxn];
    12 struct ted{int x,y;ted*nxt;}adj[maxm],*fch[maxn],*ms=adj;
    13 void add(int x,int y){*ms=(ted){x,y,fch[x]};fch[x]=ms++;return;}
    14 void tarjan(int u){
    15     low[u]=dfn[u]=++cz;ins[u]=true;s[++top]=u;
    16     for(ted*e=fch[u];e;e=e->nxt){
    17         int v=e->y;if(!dfn[v])tarjan(v),low[u]=min(low[u],low[v]);
    18         else if(ins[v])low[u]=min(low[u],dfn[v]);
    19     }if(low[u]==dfn[u]){
    20         scc++;int t=-1;while(t!=u)beg[t=s[top--]]=scc,ins[t]=false;
    21     }return;
    22 }
    23 inline int read(){
    24     int x=0,sig=1;char ch=getchar();
    25     while(!isdigit(ch)){if(ch=='-')sig=-1;ch=getchar();}
    26     while(isdigit(ch))x=10*x+ch-'0',ch=getchar();
    27     return x*=sig;
    28 }
    29 inline void write(int x){
    30     if(x==0){putchar('0');return;}if(x<0)putchar('-'),x=-x;
    31     int len=0,buf[15];while(x)buf[len++]=x%10,x/=10;
    32     for(int i=len-1;i>=0;i--)putchar(buf[i]+'0');return;
    33 }
    34 int n,m;
    35 void init(){
    36     n=read();m=read();int x,y;
    37     while(m--){x=read();y=read();add(x,y);}
    38     for(int i=1;i<=n;i++) if(!dfn[i]) tarjan(i);
    39     for(int i=1;i<=n;i++){
    40         printf("%d:%d
    ",i,beg[i]);
    41     }
    42     return;
    43 }
    44 void work(){
    45     return;
    46 }
    47 void print(){
    48     return;
    49 }
    50 int main(){init();work();print();return 0;}
    51 /*
    52 5 5
    53 1 2
    54 2 3
    55 3 4
    56 4 5
    57 5 1
    58 */
  • 相关阅读:
    leetcode 268. Missing Number
    DBSCAN
    python二维数组初始化
    leetcode 661. Image Smoother
    leetcode 599. Minimum Index Sum of Two Lists
    Python中的sort() key含义
    leetcode 447. Number of Boomerangs
    leetcode 697. Degree of an Array
    滴滴快车奖励政策,高峰奖励,翻倍奖励,按成交率,指派单数分级(1月3日)
    北京Uber优步司机奖励政策(1月2日)
  • 原文地址:https://www.cnblogs.com/chxer/p/4658141.html
Copyright © 2011-2022 走看看