zoukankan      html  css  js  c++  java
  • Tarjan算法之简单缩点

    有向图

    inline void tarjan(int x) {
    	dfn[x] = low[x] = ++cnt;
    	st[++top] = x;vis[x] = 1;
    	for(rint i = head[x];i;i = e[i].last) {
    		if(!dfn[e[i].to]) {
    			tarjan(e[i].to);
    			low[x] = Min(low[x],low[e[i].to]);
    		}
    		else if(vis[e[i].to]) low[x] = Min(low[x],dfn[e[i].to]);
    	}
    	if(dfn[x] == low[x]) {
    		col[x] = ++col[0];
    		vis[x] = 0;
    		while(st[top] ^ x) {
    			col[st[top]] = col[0];
    			vis[st[top]] = 0;
    			--top;
    		}
    		--top;
    	}
    	return ;
    }
    

    无向图

    inline void tarjan(int x,int da) {
    	dfn[x] = low[x] = ++cnt;
    	st[++top] = x;
    	vis[x] = 1;
    	for(rint i = head[x];i;i = e[i].last) {
    		if(e[i].to == da) continue;
    		if(!dfn[e[i].to]) {
    			tarjan(e[i].to,x);
    			low[x] = Min(low[x],low[e[i].to]);
    		}
    		else low[x] = Min(low[x],dfn[e[i].to]);
    	}
    	if(dfn[x] == low[x]) {
    		col[x] = ++col[0];
    		vis[x] = 0;
    		while(st[top] ^ x) {
    			col[st[top]] = col[0];
    			vis[st[top]] = 0;
    			--top;
    		}
    		--top;
    	}
    	return ;
    }
    
  • 相关阅读:
    10000000000
    vue生命周期
    react基础
    第一个react
    vuex状态管理2
    vue配合UI组件
    vuex
    vue-router配合vue-cli的实例
    vue-router2.0
    父子组件2.0
  • 原文地址:https://www.cnblogs.com/Thomastine/p/11855060.html
Copyright © 2011-2022 走看看