niusitu @ 2024-11-17 16:40:29
代码如下
#include<bits/stdc++.h>
#define I return
#define love 0
#define DLY ;
using namespace std;
const int maxn=5e5+10;
const int maxm=2e6+10;
int head[maxn],ver[maxm],nxt[maxm],tot=-1;
int dfn[maxn],low[maxn],bridge[maxn];
int times;
int id[maxn],dcc=1;
int v[maxn];
void tarjan(int x,int edge)
{
dfn[x]=low[x]=times++;
for (int i=head[x];i;nxt[i])
{
int y=ver[i];
if (!dfn[y])
{
tarjan(x,i);
low[x]=min(low[x],low[y]);
if (dfn[x]<low[x])
{
bridge[i]=bridge[i^1]=1;
}
}
else if(i!=(edge^1))
{
low[x]=min(low[x],dfn[y]);
}
}
}
void dfs(int x)
{
id[x]=dcc;
for (int i=head[x];i;nxt[i])
{
int y=ver[i];
if (id[y]||bridge[i]){continue;}
dfs(y);
}
}
void add(int x,int y)
{
ver[++tot]=y,nxt[tot]=head[x],head[x]=tot;
}
int main()
{
int n,m;
cin>>n>>m;
for(int i=0;i<m;i++)
{
int u,v;
cin>>u>>v;
add(u,v);
add(v,u);
}
for (int i=0;i<n;++i)
{
if (!dfn[i])
{
tarjan(i,0);
}
}
for (int i=0;i<n;++i)
{
if (!id[i])
{
dcc++;
dfs(i);
}
}
cout<<dcc<<endl;
for (int i=1;i<=dcc;++i)
{
for (int j=0;j<=tot;j+=2)
{
int x=ver[j],y=ver[j^1];
if (id[y]==j&&id[x]==j&&!v[x]&&!v[y]){cout<<x<<' '<<y;v[x]=v[y]=1;}
}
cout<<endl;
}
I love DLY
}
by xiedequan130412 @ 2024-11-17 16:42:58
等一下
by xiedequan130412 @ 2024-11-17 16:47:10
私信
by niusitu @ 2024-11-19 14:20:12
@xiedequan130412 链接打不开