Blog of RuSun

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

P4123 [CQOI2016]不同的最小割

P4123 [CQOI2016]不同的最小割

最小割树。

查看代码
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
#include <cstdio>
#include <queue>
#include <vector>
#include <bitset>
#include <algorithm>
using namespace std;
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 inf = 85e6;
int n, m, q;
bitset<inf> ans;
namespace NetFlow
{
const int N = 860, M = 17e3 + 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 N = 860, M = 1710, K = 12;
int 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();
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()
{
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();
for (int i = 1; i <= n; i++)
for (int j = i + 1; j <= n; j++)
ans.set(Tree::query(i, j));
write(ans.count());
return 0;
}