Kichstart solutions
Kickstart solutions
Kickstart Round A 2021
Problem D: Checksum
Given a matrix with noise (represented by ) and their checksum in row/column direction. You can use some cost to recover a number from noise. Calculate the minimum cost that recovers the whole matrix.
According to characteristic of xor, we can recover a number without any cost if it is the only unknown number in this row/column. And after recovering it, maybe another number can be recovered. If we use brute-force to check this, the complexity will be very huge.
Considering a bipartite graph based on rows and columns like the graph above, a node can be erased if and only if it is a leaf. After removing all of leaves, if the graph is a tree, there will be no nodes. Otherwise, there will be some cycles. In this case, we must use some cost to break the cycles. So the problem becomes: how to remove some edges to change the graph to a tree, which is a spanning tree problem. The tree edge is the cost that we do not need to pay. So if we can calculate the maximum spanning tree (in fact forest here) for the graph, the answer is the total cost minus tree edges cost.
Here using kruscal algorithm, we can solve this problem in . About how to calculate maximum spanning forest, I choose to add a super node which connects to all of nodes with cost . Then graph becomes connected and the edges with cost will not influence the maximum spanning forest.
1 |
|
本博客所有文章除特别声明外,均采用 CC BY-SA 4.0 协议 ,转载请注明出处!