Tarjan 缩点模板 (将有向有环图缩点成为有向无环图DAG)

缩点的作用

        1)缩点的实际作用:把一个有向带环图,变成一个有向无环图(DAG) ,这样基于DAG的算法就能跑了。

        2)可以算缩点后个点的出度

基本思想

        一我们既然能在有向图中找到环,那么我们就可以吧环给缩成点了(利用Tarjan缩点),缩点基于一种染色实现,在DFS搜索的过程中,尝试吧属于同一个强连通分量的点都染成一个颜色,同一颜色的点就相当于一个点。
    步骤:1)Tarjan缩点,2)染色处理,3)建有向无环图 (DAG) 

代码:

const int MAXN = 5e3 + 20;
const int MAXM = 1e6 + 10; 
int head[MAXN], cnt, tot, dfn[MAXN], low[MAXN], color[MAXN], col;
bool vis[MAXN];
int degree[MAXN];
stack<int> stc;
int n, m;
struct Edge {
	int to, next, dis;	
}edge[MAXM << 1];
void add_edge(int u, int v, int dis) {
	edge[++cnt].to = v;
	edge[cnt].next = head[u];
	head[u] = cnt; 
}
void Tarjan(int x) {
	vis[x] = 1;
	dfn[x] = low[x] = ++tot;
	stc.push(x);
	for(int i = head[x]; i; i = edge[i].next) {
		int to = edge[i].to;
		if (!dfn[to]) {
			Tarjan(to);
			low[x] = min(low[x], low[to]);
		} else if( vis[to] ) {
			low[x] = min(low[x], dfn[to]);
		}
	}
	if(dfn[x] == low[x]) {
		col ++;
		while(true) {
			int top = stc.top();
			stc.pop();
			color[top] = col;	//颜色相同的点缩点 
			vis[top] = 0;
		//	cout << top << " ";
			if(top == x) break; 
		}
		//cout << endl;
	}
}
void solve(){
	for(int i = 1; i <= n; ++i) {
		if(!dfn[i])
			Tarjan(i);
	}
	
	for(int x = 1; x <= n; ++x) {	//遍历 n个节点 
		for(int i = head[x]; i; i = edge[i].next) {	//缩点后  每个点的出度 
			int to = edge[i].to;
			if(color[x] != color[to]) {
				degree[color[x]] ++;
			}
		}
	}
}
void init () {
	cnt = 1;
	tot = 0;
	col = 0;
	memset(vis, 0, sizeof(vis));
	memset(head, 0, sizeof(head));
	memset(dfn, 0, sizeof(dfn));
	memset(low, 0, sizeof(low));
	memset(degree, 0, sizeof(degree));
	memset(color, 0, sizeof(color));
	while(!stc.empty()) stc.pop();
}
int main () {
	std::ios::sync_with_stdio(false);
	cin.tie(0);
	while(cin >> n && n) {
		cin >> m;
		init();
		int x, y;
		for(int i = 1; i <= m; ++i) {
			cin >> x >> y;
			add_edge(x, y, 0);
		}
		solve();
	} 
	return 0;
}

    练习题:

    POJ-2553 (出度为0的点) 
    P2002 消息扩散 (与入度连用)
    P2341 [HAOI2006]受欢迎的牛 (与出度连用)
    P2746 [USACO5.3]校园网Network of Schools (入度和出度)
    P2863 [USACO06JAN]牛的舞会The Cow Prom (图论知识及对强联通的理解)
    P2835 刻录光盘 (同上)
    P3387 【模板】缩点

原创文章 207 获赞 174 访问量 7万+

猜你喜欢

转载自blog.csdn.net/Harington/article/details/105646918