To solve this problem, we must understand the impact of performing operations on the MST (Minimum Spanning Tree) of a graph. Here's a step-by-step explanation of how to approach and solve this problem effectively:
Step 1: Understanding the Problem
- Graph Structure: We start with a connected weighted graph with
n vertices and m edges.
- Operations: In each operation, we remove a degree-2 vertex and replace its two incident edges with a new edge connecting its two neighbors. The weight of this new edge is the maximum of the two removed edges.
- Objective: After performing
k such operations, we need to find the expected sum of the weights of the MST of the resulting graph.
Step 2: Key Observations
- MST Sum Reduction: Each operation affects the MST by potentially removing edges and adding new ones. The expectation is that the MST sum decreases as edges are removed.
- Leaf Pruning: The operation essentially prunes leaves from the MST. Initially, leaves are vertices with degree 1 in the MST. Each operation removes one leaf and possibly creates a new leaf.
Step 3: Mathematical Modeling
- Initial MST Sum: Start by finding the MST of the original graph and calculate its sum.
- Simulation of Operations: Simulate the operations step-by-step, keeping track of:
- Leaves: Nodes with degree 1 in the MST.
- Sum of Leaf Weights (
S): The sum of the weights of edges connected to current leaves.
- Number of Leaves (
C): The count of current leaves.
- Potential New Leaves (
A): The sum of the weights of parents of leaves that might become new leaves after removal.
- Count of Such Potential New Leaves (
B): Number of leaves whose parents have degree 2.
Step 4: Simulation Mechanics
-
Initialize MST:
- Use Krusky's algorithm to find the MST of the graph.
- Root the MST and determine the parent and children for each node.
- Identify initial leaves and calculate
S, C, A, and B.
-
Perform Operations:
- For each operation up to
k:
- Calculate the expected reduction in the MST sum as
S / C.
- Update
S, C, A, and B based on the removal of a leaf and potential creation of a new leaf.
- Accumulate the total expected reduction.
-
Final Calculation:
- Subtract the total expected reduction from the initial MST sum to get the expected MST sum after
k operations.
- Represent the result as a fraction
P/Q and compute (P * Q^(-1)) mod (10^9 + 7).
Step 5: Implementation Considerations
- Modular Arithmetic: All operations are performed modulo
10^9 + 7. Care must be taken to handle divisions by using modular inverses.
- Efficiency: Given the constraints (
n, m up to 5 * 10^5), the solution must be efficient, typically O(n log n) for Krusky's algorithm and O(k) for the simulation.
Final Solution:
Below is the C++ implementation based on the above approach:
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
const int MOD = 1e9 + 7;
// Function to compute x^y mod MOD
ll power_pow(ll x, ll y) {
ll res = 1;
x %= MOD;
while(y > 0){
if(y & 1) res = res * x % MOD;
x = x * x % MOD;
y >>=1;
}
return res;
}
// Function to compute inverse of x mod MOD
ll inv(ll x){
return power_pow(x, MOD-2);
}
struct DSU {
vector<int> parent;
DSU(int n){
parent.resize(n+1);
for(int i=0;i<=n;i++) parent[i]=i;
}
int find_set(int x){
if(parent[x]!=x){
parent[x]=find_set(parent[x]);
}
return parent[x];
}
void union_set(int x, int y){
int fx = find_set(x);
int fy = find_set(y);
if(fx != fy){
parent[fy] = fx;
}
}
};
int main(){
ios::sync_with_stdio(false);
cin.tie(0);
int n, m, k;
cin >> n >> m >> k;
struct Edge {
int u, v;
ll w;
};
vector<Edge> edges(m);
for(int i=0;i<m;i++) cin >> edges[i].u >> edges[i].v >> edges[i].w;
// Krusky's algorithm
sort(edges.begin(), edges.end(), [&](const Edge &a, const Edge &b) -> bool{
if(a.w != b.w) return a.w < b.w;
if(a.u != b.u) return a.u < b.u;
return a.v < b.v;
});
DSU dsu(n);
// Build MST adjacency list
vector<vector<pair<int, ll>>> adj(n+1, vector<pair<int, ll>>());
ll initial_mst_sum =0;
for(auto &e : edges){
if(dsu.find_set(e.u) != dsu.find_set(e.v)){
dsu.union_set(e.u, e.v);
adj[e.u].emplace_back(e.v, e.w);
adj[e.v].emplace_back(e.u, e.w);
initial_mst_sum += e.w;
}
}
// Now, build the tree, assign parent and degrees
// Let's root the tree at node1
int root = 1;
vector<int> parent(n+1, -1);
vector<int> degree(n+1, 0);
vector<ll> edge_to_parent(n+1, 0);
// BFS to assign parent
queue<int> q;
q.push(root);
parent[root] = 0;
while(!q.empty()){
int u = q.front(); q.pop();
for(auto &[v, w] : adj[u]){
if(parent[v]==-1){
parent[v]=u;
edge_to_parent[v]=w;
q.push(v);
}
}
}
// Now, assign degrees
// degree[u] = number of children + (u != root ? 1 : 0)
vector<int> children(n+1, 0);
for(int v=1; v<=n; v++) {
if(parent[v]!=0){
children[parent[v]]++;
}
}
for(int u=1; u<=n; u++) {
degree[u] = children[u];
if(u != root) degree[u]++;
}
// Initialize leaves
ll S_num =0;
ll S_den =1;
int C =0;
ll A_num =0;
ll A_den =1;
int B =0;
for(int u=1; u<=n; u++) {
if(degree[u]==1 && u != root){
S_num = (S_num + edge_to_parent[u]) % MOD;
C++;
int p = parent[u];
if(degree[p]==2){
A_num = (A_num + edge_to_parent[p]) % MOD;
B++;
}
}
}
// Simulation
ll sum_reduction =0;
for(int step=0; step<k; step++){
if(C ==0) break;
// Compute step_reduction = S_num / S_den / C
ll term = (S_num * inv(S_den)) % MOD;
term = (term * inv(C)) % MOD;
sum_reduction = (sum_reduction + term) % MOD;
// Update S and C
// S_new = (S_num * (C-1) + A_num) / (C * S_den)
ll S_num_new = (S_num * (C -1)) % MOD;
S_num_new = (S_num_new + A_num) % MOD;
ll S_den_new = (C * S_den) % MOD;
// A_new = A / C => A_num / (A_den * C)
ll A_num_new = A_num;
ll A_den_new = (A_den * C) % MOD;
// B_new = B / C
ll B_new = (B * inv(C)) % MOD;
// Update S
S_num = S_num_new;
S_den = S_den_new;
// Update A
A_num = A_num_new;
A_den = A_den_new;
// Update B
B = B_new;
// Update C
ll C_new = ((ll)(C) -1 + B) % MOD;
C = C_new;
}
// Now, compute initial_mst_sum - sum_reduction
// initial_mst_sum can be up to 5e5 *1e9=5e14, but take modulo
ll initial_sum_mod = initial_mst_sum % MOD;
ll expected_sum = (initial_sum_mod - sum_reduction) % MOD;
if(expected_sum <0) expected_sum += MOD;
// Now, output expected_sum as P/Q, but it's already in form after modulo subtraction
// Wait, sum_reduction was P/Q where Q=1 implicitly under modulo, so:
// The answer is expected_sum
cout << expected_sum;
}