1 Star 0 Fork 0

Jelly-Aries / pastebin

加入 Gitee
与超过 1400万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
文件
克隆/下载
model_tarjan.cpp 1.48 KB
一键复制 编辑 原始数据 按行查看 历史
Jelly-Aries 提交于 2019-08-14 09:23 +08:00 . Upload 真tarjan缩点板子
#include <iostream>
#include <cstdio>
using namespace std;
const int maxn=10086;
//forward star node
struct edge
{
int to,next;
} Edge[50086];
int cnt=1,head[maxn];
//forward star add edges
inline void add_edge(int from,int to)
{
Edge[cnt]=(edge){to,head[from]};
head[from]=cnt++;
}
//tarjan uses
int dfn[maxn],low[maxn],stack[maxn],now,top;
bool vst[maxn];
//After tarjan these point will put into baskets(group)
int group[maxn],gcnt;
void tarjan(int n)
{
dfn[n]=low[n]=++now;
stack[++top]=n;
vst[n]=true;
for (int i=head[n];i;i=Edge[i].next)
{
int to=Edge[i].to;
if (dfn[to]==0)
{
tarjan(to);
low[n]=min(low[n],low[to]);
}
else
{
if (vst[to])
{
low[n]=min(low[n],low[to]);
}
}
}
if (low[n]==dfn[n])
{
group[n]=++gcnt;
while (stack[top]!=n)
{
vst[stack[top]]=false;
group[stack[top--]]=gcnt;
}
//make n get out of stack
vst[n]=false;
top--;
}
}
int main()
{
int n,m;
cin>>n>>m;
for (register int i=1;i<=m;i++)
{
int f,t;
scanf("%d%d",&f,&t);
add_edge(f,t);
}
for (register int i=1;i<=n;i++)
{
if (dfn[i]==0)tarjan(i);
}
for (register int i=1;i<=n;i++)
{
cout<<group[i]<<endl;
}
return 0;
}
Loading...
马建仓 AI 助手
尝试更多
代码解读
代码找茬
代码优化
1
https://gitee.com/Jelly-Goat/pastebin.git
git@gitee.com:Jelly-Goat/pastebin.git
Jelly-Goat
pastebin
pastebin
master

搜索帮助