Description
给定一张 nnn 点 mmm 边的无向图,求它的最小生成树,以及最小生成树的数量(模 109+710^9+7109+7)。具有相同权值的边最多 333 条。
Analysis
第一问就是 kruskal 板子。
接下来是最关键的第二问,首先我们考虑一下,假如有相同的边权,并且可以作为最小生成树的一条边,那么我们就让数量加一,每次只需要依次遍历相同边权的边即可。
由于具有相同权值的边最多 333 条,我们可以分类讨论。
对于每种权值的边,我们需要知道:总共的条数,MST 中用到的条数,以及哪些边等价。
在处理等价边的问题时,为了方便,我们可以将所有该权值的边加入 set 中,利用 set 的去重功能,就可以得到不等价边的数量。
这里需要注意:
- 边的两端顶点 u,vu,vu,v 在
pair中的先后顺序不重要,这里我们按 u≤vu \le vu≤v 的原则去存放; - 由于
kruskal的实现机制,需要放的应该是并查集中 u,vu,vu,v 各自的集合编号。
接下来就是分类讨论:
如果当前权值的边中,MST 只用了一条,那么每一条都满足要求,方案数乘上相同边权的个数。
如果 MST 用了两条,且相同边权的边有三条,那么有以下情况:
- 如果没有等价的边,那么就有 (32)=3{3 \choose 2}=3(23)=3 种挑法。
- 如果有两条等价的边,那么只有 222 种挑法。
根据乘法原理,每一种权值的挑法数之积就是答案。
Code
// Problem: P3037 [USACO11DEC] Simplifying the Farm G
// Contest: Luogu
// URL: https://www.luogu.com.cn/problem/P3037
// Memory Limit: 125 MB
// Time Limit: 1000 ms
//
// Powered by CP Editor (https://cpeditor.org)
#include <bits/stdc++.h>
using namespace std;
using i64 = long long;
using PII = pair<int, int>;
template <int MOD>
struct modint {
int val;
static int norm(const int& x) { return x < 0 ? x + MOD : x; }
modint inv() const {
int a = val, b = MOD, u = 1, v = 0, t;
while (b > 0) t = a / b, swap(a -= t * b, b), swap(u -= t * v, v);
return modint(u);
}
modint() : val(0) {}
// modint(const int& m) : val(norm(m % MOD)) {}
modint(const long long& m) : val(norm(m % MOD)) {}
modint operator-() const { return modint(norm(-val)); }
bool operator==(const modint& o) { return val == o.val; }
bool operator!=(const modint &o) { return val != o.val; }
bool operator<(const modint& o) { return val < o.val; }
modint& operator+=(const modint& o) { return val = (1ll * val + o.val) % MOD, *this; }
modint& operator-=(const modint& o) { return val = norm(1ll * val - o.val), *this; }
modint& operator*=(const modint& o) { return val = static_cast<int>(1ll * val * o.val % MOD), *this; }
modint& operator/=(const modint& o) { return *this *= o.inv(); }
modint& operator^=(const modint& o) { return val ^= o.val, *this; }
modint& operator>>=(const modint& o) { return val >>= o.val, *this; }
modint& operator<<=(const modint& o) { return val <<= o.val, *this; }
modint operator-(const modint& o) const { return modint(*this) -= o; }
modint operator+(const modint& o) const { return modint(*this) += o; }
modint operator*(const modint& o) const { return modint(*this) *= o; }
modint operator/(const modint& o) const { return modint(*this) /= o; }
modint operator^(const modint& o) const { return modint(*this) ^= o; }
modint operator>>(const modint& o) const { return modint(*this) >>= o; }
modint operator<<(const modint& o) const { return modint(*this) <<= o; }
friend std::istream& operator>>(std::istream& is, modint& a) {
long long v;
return is >> v, a.val = norm(v % MOD), is;
}
friend std::ostream& operator<<(std::ostream& os, const modint& a) { return os << a.val; }
friend std::string tostring(const modint& a) { return std::to_string(a.val); }
template <class T>
friend modint qpow(const modint& a, const T& b) {
modint x = a, res = 1;
for (T p = b; p; x *= x, p >>= 1)
if (p & 1) res *= x;
return res;
}
};
using Z = modint<1000000007>;
struct Edge{
int u, v, w;
bool operator<(const Edge &rhs) const{
return w < rhs.w;
}
};
struct dsu{
vector<int> fa, siz;
dsu() {}
dsu(int n){
fa.resize(n);
siz.assign(n, 1);
iota(fa.begin(), fa.end(), 0);
}
int find(int x){
while(x != fa[x]) x = fa[x] = fa[fa[x]];
return x;
}
bool same(int x, int y){
return find(x) == find(y);
}
bool unite(int x, int y){
x = find(x); y = find(y);
if(x == y) return false;
siz[x] += siz[y];
fa[y] = x;
return true;
}
int size(int x){
return siz[find(x)];
}
};
signed main() {
ios::sync_with_stdio(0);
cin.tie(0), cout.tie(0);
int n, m;
cin >> n >> m;
vector<Edge> edges(m);
for(auto &edge: edges){
cin >> edge.u >> edge.v >> edge.w;
edge.u--, edge.v--;
}
dsu d(n);
sort(edges.begin(), edges.end());
int ans = 0;
Z sum = 1;
for(int i = 0; i < m;){
int cnt = 0;
set<PII> s;
int j;
for(j = i; j < m && edges[i].w == edges[j].w; j++){
int pu = d.find(edges[j].u), pv = d.find(edges[j].v);
if(pu > pv) swap(pu, pv);
if(pu != pv){
cnt++;
s.emplace(pu, pv);
}
}
int num = 0;
for(; i < j; i++){
if(d.unite(edges[i].u, edges[i].v)) num++;
}
if(i) ans += edges[i - 1].w * num;
if(num == 1) sum *= cnt;
if(num == 2){
if(cnt == 3 && s.size() == 2) sum *= 2;
if(cnt == 3 && s.size() == 3) sum *= 3;
}
}
cout << ans << ' ' << sum << endl;
return 0;
}
600

被折叠的 条评论
为什么被折叠?



