GYM101667E

题目链接

题目

一张无向图$G$,定义$H(e)$为如果$e$这条边属于$G$的最小生成树,那么最少要在原图上删除边的数量。 求$G$中所有边的$H(e)$和

$1 \leq n \leq 100, 1 \leq m \leq 500$

分析

如何计算$H(e)$呢?

首先的想法是枚举边,假设当前边已经在最小生成树上了,然后再重新跑一边最小生成树的算法,计算要删的边的个数。
当然这么做是有问题的,因为

考虑将所有权值小于$e$的重新构图。 对于$e$的两个端点跑一个最小割,割边的数量就是$H(e)$

代码

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
// ybmj
#include <bits/stdc++.h>
using namespace std;
#define lson (rt << 1)
#define rson (rt << 1 | 1)
#define lson_len (len - (len >> 1))
#define rson_len (len >> 1)
#define pb(x) push_back(x)
#define clr(a, x) memset(a, x, sizeof(a))
#define mp(x, y) make_pair(x, y)
typedef long long ll;
typedef pair<int, int> pii;
typedef pair<ll, ll> pll;
#define my_unique(a) a.resize(distance(a.begin(), unique(a.begin(), a.end())))
#define my_sort_unique(c) (sort(c.begin(), c.end())), my_unique(c)
const int INF = 0x3f3f3f3f;
const int NINF = 0xc0c0c0c0;
const double PI = acos(-1.0);
const int maxn = 105;
struct Edge {
int from, to, cap, flow;
};
struct EdmonsKarp {
int n, m;
vector<Edge> edges;
vector<int> G[maxn];
int a[maxn], p[maxn];
void init(int n) {
for (int i = 0; i <= n; i++) G[i].clear();
edges.clear();
}
void AddEdge(int from, int to, int cap) {
edges.push_back({from, to, cap, 0});
edges.push_back({to, from, cap, 0});
m = edges.size();
G[from].push_back(m - 2);
G[to].push_back(m - 1);
}
int MaxFlow(int s, int t) {
int flow = 0;
for (;;) {
memset(a, 0, sizeof(a));
queue<int> Q;
Q.push(s);
a[s] = INF;
while (!Q.empty()) {
int x = Q.front();
Q.pop();
for (auto &id : G[x]) {
Edge &e = edges[id];
if (!a[e.to] && e.cap > e.flow) {
p[e.to] = id;
a[e.to] = min(a[x], e.cap - e.flow);
Q.push(e.to);
}
}
if (a[t]) break;
}
if (!a[t]) break;
for (int u = t; u != s; u = edges[p[u]].from) {
edges[p[u]].flow += a[t];
edges[p[u] ^ 1].flow -= a[t];
}
flow += a[t];
}
return flow;
}
};
EdmonsKarp wss;
struct _Edge {
int u, v, w;
bool operator<(const _Edge &A) const { return w < A.w; }
};
vector<_Edge> edges;
int main() {
// /*
#ifndef ONLINE_JUDGE
freopen("1.in", "r", stdin);
freopen("1.out", "w", stdout);
#endif
// */
std::ios::sync_with_stdio(false);
int n, m;
scanf("%d%d", &n, &m);
for (int i = 0, u, v, w; i < m; i++) {
scanf("%d%d%d", &u, &v, &w);
edges.push_back({u, v, w});
}
sort(edges.begin(), edges.end());
int ans = 0;
for (auto &e : edges) {
wss.init(n);
for (auto &tmp : edges) {
if (tmp.w >= e.w) break;
wss.AddEdge(tmp.u, tmp.v, 1);
}
ans += wss.MaxFlow(e.u, e.v);
}
printf("%d\n", ans);
}