题解:P17418 [ICPC 2018 Xuzhou R] Rikka with Minimum Spanning Trees

· · 题解

可恶的出题人,这竟然是诈骗题。

思路

求出“不同最小生成树”的边权和的和。
似乎要在生成树算法上套一个题目里的什么矩阵……好复杂……
于是,我们关注数据范围。嗯,没什么有用的信息。但是,题目竟然给出了数据生成的代码!这其中一定有问题。

突破

注意到,这题的边权在 2^{64} 大小,甚至要开 unsigned long long!什么意思呢?就是说,两条边长度相同的概率是 \dfrac{1}{2^{64}},这几乎可以忽略不计了。
换句话说,我们可以认为这题边权不重复。同时,要想“选出 a 条边的和”与“选出 b 条边的和”一样,且两次选边不同(就是说,构造出两棵不一样的生成树),这种概率还要再小一点。因此,这题其实总共只有 \bm{1} 棵最小生成树嘛!敲个 Kruskal 板子就行。不过别忘了取模。

代码

:::success[AC Code]

//Someone tell Vedal there is a problem with my AI.
#include<iostream>
#include<cstdio>
#include<vector>
#include<algorithm>
#define int long long
#define rep(v, s, e) for(int v=(s);v<=(e);v++)
#define dec(v, e, s) for(int v=(e);v>=(s);v--)
const int MAXN = 1e5, MOD = 1e9+7;
struct Edge{
    Edge()=default;
    Edge(int x, int y, unsigned int w);
    int u, v;
    unsigned int w;
    bool operator<(const Edge rhs) const;
};
struct DSU{
    int fa[MAXN+5];
    inline void Init(int _n);
    inline int Findfa(int x);
    inline bool isConnected(int x, int y);
    inline void Merge(int x, int y);
};
unsigned int k1, k2;
int n, m, u[MAXN+5], v[MAXN+5];
unsigned int w[MAXN+5];
DSU dsu;
unsigned int ans;
std::vector<Edge>edge;
unsigned int xorShift128Plus();
void gen();
inline unsigned int Kruskal(int blk=1);
void Init(), PreSolve(), Input(), Solve(), Answer(), AC();
signed main(){
    AC();
    return 0;
}
void Init(){
    std::ios::sync_with_stdio(false),
    std::cin.tie(0),std::cout.tie(0);
    #ifndef ONLINE_JUDGE
    std::freopen("FILE.in" , "r", stdin );
    std::freopen("FILE.out", "w", stdout);
    #endif
}
void AC(){
    Init();
    int T = 1;
    std::cin >> T;
    while(T--){
        PreSolve();
        Input();
        Solve();
        Answer();
    }
}
void PreSolve(){
    dsu.Init(MAXN);
    ans = 0;
    edge.clear();
}
void Input(){
    gen();
}
void Solve(){
    rep(i, 1, m)
        edge.push_back({u[i], v[i], w[i]});
    std::sort(edge.begin(), edge.end());
    ans = Kruskal(1);
}
void Answer(){
    std::cout << ans << "\n";
}
unsigned int xorShift128Plus(){
    unsigned int k3 = k1, k4 = k2;
    k1 = k4;
    k3 ^= k3 << 23;
    k2 = k3 ^ k4 ^ (k3 >> 17) ^ (k4 >> 26);
    return k2 + k4;
}
void gen(){
    std::cin >> n >> m >> k1 >> k2;
    rep(i, 1, m){
        u[i] = xorShift128Plus() % n + 1;
        v[i] = xorShift128Plus() % n + 1;
        w[i] = xorShift128Plus();
    }
}
Edge::Edge(int x, int y, unsigned int z):
    u{x}, v{y}, w{z} {}
bool Edge::operator<(const Edge rhs) const{
    return w < rhs.w;
}
inline void DSU::Init(int _n){
    rep(i, 1, _n) fa[i] = i;
}
inline int DSU::Findfa(int x){
    return fa[x] == x ? x : fa[x] = Findfa(fa[x]);
}
inline bool DSU::isConnected(int x, int y){
    return Findfa(x) == Findfa(y);
}
inline void DSU::Merge(int x, int y){
    x = Findfa(x), y = Findfa(y);
    if(x == y) return;
    fa[x] = y;
}
inline unsigned int Kruskal(int blk){
    unsigned int res=0;
    int cnt=0;
    for(Edge &e : edge){
        if(cnt == n-blk) return res % MOD;
        int u = e.u, v = e.v, w = e.w;
        u = dsu.Findfa(u), v = dsu.Findfa(v);
        if(u == v) continue;
        dsu.Merge(u, v), cnt++;
        (res += w) %= MOD;
    }
    return 0;
}

:::

注意事项