Blog of RuSun

\begin {array}{c} \mathfrak {One Problem Is Difficult} \\\\ \mathfrak {Because You Don't Know} \\\\ \mathfrak {Why It Is Diffucult} \end {array}

CF343E Pumping Stations

LuoGu: CF343E Pumping Stations

CF: E. Pumping Stations

做最小割树,答案是是最小割树上的边权和,因为对于任意两点的最小割是树上路径上的边权最小值,所以取相邻的两点是最优的,但是如何保证是一个排列,先选择边权大的点,再选择边权小的点,那么比这条边边权小的边不会贡献答案。同样地,构造答案时也这样做。

堆维护最大边权。首先指定一个点作为第一个点,同时作为最小割树的根节点,将其放入堆中。每次从堆中取出当前最大的,输出,同时继续将与其相邻的点放入堆中。这样保证了当前不选的边一定都是比选过的边边权更小。如果是上一个点的儿子,那么显然是正确的;否则,如果路径上存在一个比当前边还小的边,那么要么当前点不应该被放入,要么上一个点不应该被放入。

查看代码
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
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
#include <cstdio>
#include <queue>
#include <vector>
#include <algorithm>
using namespace std;
typedef pair<int, int> PII;
template <class Type>
void read(Type &x)
{
char c;
bool flag = false;
while ((c = getchar()) < '0' || c > '9')
c == '-' && (flag = true);
x = c - '0';
while ((c = getchar()) >= '0' && c <= '9')
x = (x << 3) + (x << 1) + c - '0';
flag && (x = ~x + 1);
}
template <class Type>
void write(Type x)
{
x < 0 && (putchar('-'), x = ~x + 1);
x > 9 && (write(x / 10), 0);
putchar(x % 10 + '0');
}
const int N = 210, inf = 1e5;
int n, m;
namespace NetFlow
{
const int M = 2e3 + 10;
int st, ed, d[N], cur[N];
int idx, hd[N], nxt[M], edg[M], wt[M];
bool bfs()
{
for (int i = 1; i <= n; i++)
d[i] = -1;
d[st] = 0;
cur[st] = hd[st];
queue<int> q;
q.push(st);
while (!q.empty())
{
int t = q.front();
q.pop();
for (int i = hd[t]; ~i; i = nxt[i])
if (d[edg[i]] == -1 && wt[i])
{
cur[edg[i]] = hd[edg[i]];
d[edg[i]] = d[t] + 1;
if (edg[i] == ed)
return true;
q.push(edg[i]);
}
}
return false;
}
int exploit(int x, int limit)
{
if (x == ed)
return limit;
int res = 0;
for (int i = cur[x]; ~i && res < limit; i = nxt[i])
{
cur[x] = i;
if (d[edg[i]] == d[x] + 1 && wt[i])
{
int t = exploit(edg[i], min(wt[i], limit - res));
if (!t)
d[edg[i]] = -1;
wt[i] -= t;
wt[i ^ 1] += t;
res += t;
}
}
return res;
}
int dinic()
{
int res = 0, flow;
while (bfs())
while (flow = exploit(st, inf))
res += flow;
for (int i = 0; i <= idx; i += 2)
wt[i] = wt[i + 1] = wt[i] + wt[i + 1] >> 1;
return res;
}
void add(int a, int b, int c)
{
nxt[++idx] = hd[a];
hd[a] = idx;
edg[idx] = b;
wt[idx] = c;
}
void init()
{
idx = -1;
for (int i = 1; i <= n; i++)
hd[i] = -1;
}
}
namespace Tree
{
const int M = 410, K = 10;
bool vis[N];
int ans, lg[N], d[N], fa[N][K], mn[N][K];
int idx, hd[N], nxt[M], edg[M], wt[M];
void add(int a, int b, int c)
{
nxt[++idx] = hd[a];
hd[a] = idx;
edg[idx] = b;
wt[idx] = c;
}
void build(vector<int> &p)
{
if (p.size() == 1)
return;
int s = p.front(), t = p.back();
NetFlow::st = s, NetFlow::ed = t;
int cut = NetFlow::dinic();
ans += cut;
add(s, t, cut), add(t, s, cut);
vector<int> L, R;
for (int i : p)
~NetFlow::d[i] ? L.push_back(i) : R.push_back(i);
build(L), build(R);
}
void dfs(int x)
{
for (int i = hd[x]; ~i; i = nxt[i])
{
if (edg[i] == fa[x][0])
continue;
d[edg[i]] = d[x] + 1;
fa[edg[i]][0] = x;
mn[edg[i]][0] = wt[i];
for (int k = 1; (1 << k) <= d[edg[i]]; k++)
{
fa[edg[i]][k] = fa[fa[edg[i]][k - 1]][k - 1];
mn[edg[i]][k] = min(mn[edg[i]][k - 1], mn[fa[edg[i]][k - 1]][k - 1]);
}
dfs(edg[i]);
}
}
int query(int x, int y)
{
int res = inf;
d[x] < d[y] && (swap(x, y), 0);
while (d[x] > d[y])
{
int k = lg[d[x] - d[y]];
res = min(res, mn[x][k]);
x = fa[x][k];
}
if (x == y)
return res;
for (int k = lg[d[x]]; ~k; k--)
if (fa[x][k] != fa[y][k])
{
res = min(res, min(mn[x][k], mn[y][k]));
x = fa[x][k], y = fa[y][k];
}
return min(res, min(mn[x][0], mn[y][0]));
}
void init()
{
lg[1] = 0;
for (int i = 2; i <= n; i++)
lg[i] = lg[i >> 1] + 1;
vector<int> p;
for (int i = 1; i <= n; i++)
p.push_back(i);
idx = -1;
for (int i = 1; i <= n; i++)
hd[i] = -1;
build(p);
dfs(1);
}
int main()
{
write(ans), puts("");
priority_queue<PII> q;
q.push(make_pair(0, 1));
while (!q.empty())
{
int t = q.top().second;
printf("%d ", t);
q.pop();
vis[t] = true;
for (int i = hd[t]; ~i; i = nxt[i])
!vis[edg[i]] && (q.push(make_pair(wt[i], edg[i])), 0);
}
return 0;
}
}
int main()
{
read(n), read(m);
NetFlow::init();
for (int a, b, c; m; m--)
{
read(a), read(b), read(c);
NetFlow::add(a, b, c);
NetFlow::add(b, a, c);
}
Tree::init();
return Tree::main();
}