Minimal Cut
Time limit3sMemory limit1024 MB
Given a weighted undirected graph plus a cycle of 10^9-weight edges through all vertices, sum the minimum cut values over all vertex pairs modulo 998244353.
- Level
Hard9 of 10
- Topics
- Graph, Divide and conquer
- Solved
- No attempts yet
Problem
Today Rikka got an undirected graph with vertices and edges. The vertices are numbered by integers from to . The -th edge connects vertices and , and its weight is .
Rikka likes Hamiltonian graphs, that is, graphs that have a Hamiltonian cycle. So Rikka builds a graph from that is always Hamiltonian. She does this by adding extra edges. The -th extra edge connects vertices and , and its weight is .
Let be the value of the minimal cut between vertices and . A set of edges is a cut between and if, in the graph , vertices and are not connected, directly or indirectly. The minimal cut is the cut with the smallest total edge weight, and is that total.
Rikka asks you to compute the following sum.
Input
The first line contains two integers and (, ).
Each of the next lines contains three integers , , and (, , ).
The graph has no self-loops, but it may contain multiple edges.
Output
Print a single line containing the answer modulo .