通信网络中,用来衡量系统可靠性,连通度越高,可靠性越高。
割点模板:
// luogu P3388
#include
using namespace std;
const int MAXN=100010,MAXM=100010;
int Head[MAXN],Next[MAXM*2],To[MAXM*2];
bool vis[MAXN],cutv[MAXN];
int dfn[MAXN],low[MAXN];
int n,m,tot,tim,root,rootson;
void add_eage(int x,int y)
{
tot++;
Next[tot]=Head[x];
Head[x]=tot;
To[tot]=y;
}
void ReadInfo()
{
scanf("%d%d",&n,&m);
tim=tot=0;
for (int i=1;i<=m;i++)
{
int x,y;
scanf("%d%d",&x,&y);
add_eage(x,y); add_eage(y,x);
}
}
void Tarjan(int u,int pre)
{
dfn[u]=low[u]=++tim;
vis[u]=true;
for (int i=Head[u];i;i=Next[i])
{
int v=To[i];
if (!vis[v])
{
Tarjan(v,u);
low[u]=min(low[u],low[v]);
if (u!=root && dfn[u]<=low[v]) cutv[u]=true;
else if (u==root && ++rootson==2) cutv[u]=true;
}
else if (v!=pre) low[u]=min(low[u],dfn[v]);
}
}
void solve()
{
memset(dfn,-1,sizeof(dfn));
memset(low,-1,sizeof(low));
memset(vis,false,sizeof(vis));
memset(cutv,false,sizeof(cutv));
for (int i=1;i<=n;i++)
if (!vis[i])
{
root=i;
rootson=0;
Tarjan(i,0);
}
}
void Outit()
{
int num=0;
for (int i=1;i<=n;i++)
if (cutv[i]) num++;
printf("%d\n",num);
for (int i=1;i<=n;i++)
if (cutv[i]) printf("%d ",i);
printf("\n");
}
int main()
{
ReadInfo();
solve();
Outit();
return 0;
}
桥模板(可处理重边):
#include
using namespace std;
const int MAXN=100010,MAXM=100010;
int Head[MAXN],Next[MAXM*2],To[MAXM*2];
bool vis[MAXN];
int dfn[MAXN],low[MAXN];
int n,m,tot,tim,num_cutedge;
struct Edge{
int u,v;
}cutedge[MAXM];
void add_eage(int x,int y)
{
tot++;
Next[tot]=Head[x];
Head[x]=tot;
To[tot]=y;
}
void ReadInfo()
{
memset(Head,0,sizeof(Head));
scanf("%d%d",&n,&m);
tim=tot=0;
for (int i=1;i<=m;i++)
{
int x,y;
scanf("%d%d",&x,&y);
add_eage(x,y); add_eage(y,x);
}
}
void Tarjan(int u,int id)
{
dfn[u]=low[u]=++tim;
vis[u]=true;
for (int i=Head[u];i;i=Next[i])
{
int v=To[i];
if (!vis[v])
{
Tarjan(v,i);
low[u]=min(low[u],low[v]);
if (dfn[u]else if ((i+1)/2!=(id+1)/2) low[u]=min(low[u],dfn[v]);
}
}
void solve()
{
memset(dfn,-1,sizeof(dfn));
memset(low,-1,sizeof(low));
memset(vis,false,sizeof(vis));
num_cutedge=0;
for (int i=1;i<=n;i++)
if (!vis[i]) Tarjan(i,0);
}
void Outit()
{
printf("the number of the bridges is %d\n",num_cutedge);
for (int i=1;i<=num_cutedge;i++)
printf("%d %d\n",cutedge[i].u,cutedge[i].v);
}
int main()
{
ReadInfo();
solve();
Outit();
return 0;
}