割点:若删除该点,图不连通,则该点为割点
桥:若删除该边,图不连通,则该边为桥
如何求割点:
Tarjan算法,一次dfs遍历:
对每个点,记录dfs序为dfn[],low值为low[](low初始值与dfn相同)
回溯时,如果回溯点的low值小于当前点low值,更新当前点low值
对于根节点,需要特判,其子节点>=2为割点
我们可以发现,low值记录的是:该点所在的强连通 能达到的最上高度
那么,对于一个点u,存在它的子节点和孙子节点v的low值,满足:
low[v]>=dfn[u]。
即代表,v所在的强连通最上高度,都不超过u ,也就是说,如果v点想要达到u以上高度,必须经过u。
此时,我们判定U为割点。
如何求桥:
和上述一致,判定条件变为:
对点u,存在子节点满足,
low(v)>dfn(u),该边(u,v)为桥。
#include <vector>
#include <cstdio>
#include <cstring>
#include <iostream>
#include <algorithm>
#define maxn 1005
using namespace std;
vector<int> e[maxn];
int root,n,m;
int num[maxn]={0},low[maxn]={0},flag[maxn]={0},t=0;
void dfs(int cur,int father)
{
int child=0;
t++;
low[cur] = num[cur] = t;
for(int i = 0;i < e[cur].size();i++)
{
int k = e[cur][i];
if(num[k] == 0)//时间戳为0,表示节点未被访问
{
child++;//i是生成树中cur的儿子
dfs(k,cur);
low[cur] = min(low[cur],low[k]);
if(cur != root && low[k] >= num[cur])//儿子节点不能绕过当前点回到根,说明此点为割点
flag[cur] = 1;
if(cur == root && child == 2)//如果当前节点只有两个儿子,此点一定为割点
flag[cur] = 1;
}
else if(k != father)//如果这个点已经访问过,而且还不是父亲节点,则
low[cur] = min(low[cur],num[k]);
}
}
int main()
{
//freopen("point.in","r",stdin);
//freopen("point.out","w",stdout);
scanf("%d%d",&n,&m);
for(int i = 0;i < m;i++)
{
int x,y;
scanf("%d%d",&x,&y);
e[x].push_back(y);
e[y].push_back(x);
}
root = 1;
dfs(1,root);
for(int i = 1;i <= n;i++)
if(flag[i] == 1)
cout<<i<<endl;
return 0;
}
不要抄袭,并不是该题的答案。