#include<cstdio>
using namespace std;
const int maxn=1e5+5;
struct edge{
int to,val;
};
vector<edge> G[maxn];
int dfn[maxn];
int low[maxn];
int cnt;
bool ins[maxn];
stack<int> s;
int timing;
int color[maxn];
int colorcnt[maxn];
void tarjian(int u){
timing++;
dfn[u]=low[u]=timing;
s.push(u);
ins[u]=true;
for (int v:G[u]){
if (dfn[v]==0)
tarjian(v);
low[u]=min(low[u],low[v]);
}
else if (ins[v]==true){
low[u]=min(low[u],dfn[v]);
}
if (dfn[u]==low[u]){
colorcnt++;
while (s.top!=u){
int temp=s.top();
s.pop();
ins[temp]=false;
color[temp]=colorcnt;
colornum[colorcnt]++;
}
s.pop();
color[u]=colorcnt;
colornum[colorcnt]++;
ins[u]=false;
}
}
int main(){
int n,m;
int x,y;
for (int i=1;;i<=m;++i)
{
scanf("%d%d",&x,&y);
G[x].push_back(y);
G[y].push_back(x);
}
for (int i=1;i<=n;i++)
{
if (dfn[i]==0)
tarjian(i);
}
return 0;
}
```
强联通分量整理板子
最新推荐文章于 2022-02-12 16:36:11 发布