tarjan判有几个缩点的代码,求助find错
  • 板块学术版
  • 楼主shitbro
  • 当前回复2
  • 已保存回复2
  • 发布时间2020/6/6 11:00
  • 上次更新2023/11/7 01:08:42
查看原帖
tarjan判有几个缩点的代码,求助find错
90972
shitbro楼主2020/6/6 11:00
#include<bits/stdc++.h>
#define MAXN 1000005
using namespace std;
stack <int> s;
struct node {
	int nxt,to;
}t[MAXN];
int head[MAXN],dfn[MAXN],low[MAXN],vis[MAXN],used[MAXN],a[1005][1005],co_id[MAXN],cnt = 0,tot = 0,co = 0;
void add(int x,int y) {
	t[++cnt].nxt = head[x];
	t[cnt].to = y;
	head[x] = cnt;
} 
void tarjan(int u) {
	dfn[u] = low[u] = ++tot;
	used[u] = vis[u] = 1;
	s.push(u);
	for(int i = head[u];i;i = t[i].nxt) {
		int v = t[i].to;
		if(!dfn[v]) {
			tarjan(v);
			low[u] = min(low[u],low[v]);
		}
		else if(vis[v]) {
			low[u] = min(low[u],dfn[v]);
		}
	}
	if(low[u] == dfn[u]) {
		co ++;
		while(s.top() != u) {
			vis[s.top()] = 0;
			s.pop();
		}
		vis[s.top()] = 0;
		s.pop();
	}
}
int main()
{
	int n;
	scanf("%d",&n);
	for(int i = 1;i <= n;i ++) {
		for(int  j = 1;j <= n;j ++) {
			scanf("%d",&a[i][j]);		
			add(i,j);
		} 
	} 
	for(int i = 1;i <= n;i ++) {
		if(!used[i]) {
			tarjan(i);
		}
	}
	printf("%d",co);
	return 0;
}
2020/6/6 11:00
加载中...