# 缩点

P3387 【模板】缩点 - 洛谷

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
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
#include<bits/stdc++.h>
#define int long long
using namespace std;
const int N = 1e4 + 10, M = 1e5 + 10;
int head1[M], ne1[M], to1[M], from1[M], idx1;
int head2[M], ne2[M], to2[M], from2[M], idx2;
int a[N];
int dfn[N], low[N], sta[N], vis[N], cnt, top;
int f[N];//记录每个节点所在的强连通分量
int in[N], dist[N];
int n, m;
void add(int x, int y)
{
ne1[idx1] = head1[x], to1[idx1] = y, from1[idx1] = x, head1[x] = idx1++;
}
void add2(int x, int y)
{
ne2[idx2] = head2[x], to2[idx2] = y, from2[idx2] = x, head2[x] = idx2++;
}
void Tarjan(int u)
{
dfn[u] = low[u] = ++cnt;
sta[++top] = u, vis[u] = 1;
for(int i = head1[u]; ~i; i = ne1[i])
{
int v = to1[i];
if(!dfn[v])
{
Tarjan(v);
low[u] = min(low[u], low[v]);
}
if(vis[v])//如果在队里
{
low[u] = min(low[u], dfn[v]);
}
}
//强连通分量的代表元素
if(dfn[u] == low[u])
{
int y;
while(y = sta[top--])
{
f[y] = u;
vis[y] = 0;
if(u == y) break;
a[u] += a[y];
}
}
}
//拓扑排序求最长路径
int topo()
{
queue<int> q;
for(int i = 1; i <= n; i++)
{
if(f[i] == i && in[i] == 0)
{
q.push(i);
dist[i] += a[i];
}
}
while(q.size())
{
int t = q.front();
q.pop();
for(int i = head2[t]; ~i; i = ne2[i])
{
int v = to2[i];
dist[v] = max(dist[v], dist[t] + a[v]);
in[v]--;
if(in[v] == 0) q.push(v);
}
}
int ans = 0;
for(int i = 1; i <= n; i++) ans = max(ans, dist[i]);
return ans;
}
signed main()
{
ios::sync_with_stdio(0);
cin.tie(0); cout.tie(0);
memset(head1, -1, sizeof head1);
memset(head2, -1, sizeof head2);
cin >> n >> m;
for(int i = 1; i <= n; i++) cin >> a[i];
for(int i = 1; i <= m; i++)
{
int u, v;
cin >> u >> v;
add(u, v);
}
//Tarjan
for(int i = 1; i <= n; i++)
if(!dfn[i]) Tarjan(i);
//建立缩点之后的图
for(int i = 1; i <= m; i++)
{
int x = f[from1[i]], y = f[to1[i]];
if(x == y) continue;
add2(x, y);
in[y]++;
}
cout << topo();
return 0;
}

# 割点

P3388 【模板】割点(割顶) - 洛谷

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>
#define int long long
using namespace std;
const int N = 2e4 + 10, M = 1e5 + 10;
vector<int> e[N];
int dfn[N], low[N], idx;
int buc[N];
int root, cnt;
int n, m;

void tarjan(int u)
{
dfn[u] = low[u] = ++idx;
int son = 0;
for(auto v: e[u])
{
if(!dfn[v])
{
son++;
tarjan(v);
low[u] = min(low[v], low[u]);
//判断是否是割点
//1. 不是根节点
//2. 子节点的low值不小于当前节点的dfn值
if(root != u && low[v] >= dfn[u])
{
cnt += !buc[u], buc[u] = 1;
}
}
else
{
low[u] = min(low[u], dfn[v]);
}
}
if(son >= 2 && u == root)
cnt += !buc[u], buc[u] = 1;
}
signed main()
{
ios::sync_with_stdio(0);
cin.tie(0); cout.tie(0);
cin >> n >> m;
for(int i = 1; i <= m; i++)
{
int x, y;
cin >> x >> y;
e[x].push_back(y);
e[y].push_back(x);
}
for(int i = 1; i <= n; i++)
if(!dfn[i]) root = i, tarjan(i);
cout << cnt << '\n';
for(int i = 1; i <= n; i++)
if(buc[i]) cout << i << " ";
return 0;
}