0%

割点、桥模板

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
void dfs(int x,int ef) {
int son=0;
f[x]=true;
low[x]=d[x]=++Time;
for (int e=head[x];e!=-1;e=next[e]) {
if (e==(ef^1)) continue;//针对无向图,而且重边算作多条边
if (!f[vet[e]]) {
dfs(vet[e],e);
low[x]=min(low[x],low[vet[e]]);
if (x!=root && d[x]<=low[vet[e]]) cut[x]++;//x为割点
if (low[vet[e]]>d[x]) bridge[e/2]=true;//此边为桥
son++;
}
else low[x]=min(low[x],d[vet[e]]);
}
if (x==root && son>1) cut[x]=son-1;//x为割点,注意-1
}


num=-1;//边从0开始计数
memset(head,-1,sizeof(head));


for (int i=1;i<=n;i++)
if (!f[i]) {
root=i;
dfs(i,-1);
}


for (int i=1;i<=n;i++)
if (cut[i]!=0)
cout<< i << " " << cut[i]+1 << endl;//输出删去该点后分离出的子图个数,注意+1
for (int i=0;m>i;i++)
if (bridge[i])
cout<< vet[i*2] << " " << vet[i*2+1] << endl;