代码关联题目

边双连通 点双连通

附言

由于(点)双连通的特殊性,所以不能像强连通那样写整块统一的 low 数组(即全部写成low[x] = min(low[x], low[j]))。

Hack我自己:

Input

1
2
3
4
5
6
7
5 6
2 4
3 4
2 3
1 5
2 5
1 2

Output

1
2
3
2
3 1 2 5
3 2 3 4

边 の 代码

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
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
#include <bits/stdc++.h>
using namespace std;

const int N = 500100, M = 2000100;

int n, m;

vector<int> ebl[N];
int ebl_cnt;
int dfn[N], low[N], cnt;

vector<int> ecut; // 或者写成bool变成表 都行
// 这里ecut存的是桥边的idx(边号) 正反均存

int h[N], e[M << 1], ne[M << 1], idx;
void add(int a, int b) {e[idx] = b, ne[idx] = h[a], h[a] = idx++;}

stack<int> S;
void tarjan(int x, int l) {
S.push(x);
dfn[x] = low[x] = ++cnt;
for(int i = h[x]; ~i; i = ne[i]) {
int j = e[i];
if((i ^ 1) == l) continue;
if(!dfn[j]) tarjan(j, i), low[x] = min(low[x], low[j]);
else low[x] = min(low[x], dfn[j]); // 注意
}
if(dfn[x] == low[x]) {
ebl_cnt++;
if(l >= 0) ecut.push_back(l), ecut.push_back(l ^ 1);
ebl[ebl_cnt].push_back(x);
while(S.top() != x) {
ebl[ebl_cnt].push_back(S.top());
S.pop();
}
S.pop();
}
}

int main() {
memset(h, -1, sizeof h);
scanf("%d%d", &n, &m);
for(int i = 1; i <= m; i++) {
int a, b; scanf("%d%d", &a, &b);
add(a, b); add(b, a);
}
for(int i = 1; i <= n; i++) if(!dfn[i]) tarjan(i, -1);
printf("%d\n", ebl_cnt);
for(int i = 1; i <= ebl_cnt; i++) {
printf("%d ", ebl[i].size());
for(int j : ebl[i]) printf("%d ", j);
putchar('\n');
}
// for(int i : ecut) printf("%d ", i); // 题目不要求输出
return 0;
}

点 の 代码

Tips - 如何让点双代码好看?

首先我们要找到它难看的点在哪里,无非就是特判割点的代码十分冗杂、突兀。
观察代码可以得到:特判割点的代码是十分独立的,也就是说可以直接删去,而保留代码求点双的功能。而绝大多数题目中不会让你具体地求割点,所以在写这些题目时,son、cnt 以及一堆 if 就可以完全省略,然后就得到了以下代码中非注释部分。然后 … 代码就至少变得能看。

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
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
#include <bits/stdc++.h>
using namespace std;

const int N = 500100, M = 2000100;

int n, m;

vector<int> pbl[N];
int pbl_cnt;
int dfn[N], low[N], cnt;

vector<int> pcut;

int h[N], e[M << 1], ne[M << 1], idx;
void add(int a, int b) {e[idx] = b, ne[idx] = h[a], h[a] = idx++;}

stack<int> S;
void tarjan(int x, int l) {
S.push(x);
dfn[x] = low[x] = ++cnt;
// int son = 0; bool cut = false;
for(int i = h[x]; ~i; i = ne[i]) {
int j = e[i];
if((i ^ 1) == l) continue;
if(!dfn[j]) {
tarjan(j, i);
if(dfn[x] <= low[j]) {
// cut = true, son++;
pbl_cnt++;
pbl[pbl_cnt].push_back(x);
pbl[pbl_cnt].push_back(j);
while(S.top() != j) {
pbl[pbl_cnt].push_back(S.top());
S.pop();
}
S.pop();
}
low[x] = min(low[x], low[j]);
} else low[x] = min(low[x], dfn[j]); // 注意
}
if(l == -1 && !son) pbl[++pbl_cnt].push_back(x); // 特判孤立点 非必要别写
// if(cut && (l >= 0 || son > 1)) pcut.push_back(x);
}

int main() {
memset(h, -1, sizeof h);
scanf("%d%d", &n, &m);
for(int i = 1; i <= m; i++) {
int a, b; scanf("%d%d", &a, &b);
add(a, b); add(b, a);
}
for(int i = 1; i <= n; i++) if(!dfn[i]) tarjan(i, -1);
printf("%d\n", pbl_cnt);
for(int i = 1; i <= pbl_cnt; i++) {
printf("%d ", pbl[i].size());
for(int j : pbl[i]) printf("%d ", j);
putchar('\n');
}
// for(int i : pcut) printf("%d ", i); // 题目不要求输出
return 0;
}