Find the critical edges and pseudo-critical edges in the minimum spanning tree

Give you a nnWeighted undirected connected graph with n points, node number is0 00 ton − 1 n-1n1 , and there is also an arrayedges edgesedges ,其中 e d g e s [ i ] = [ f r o m i , t o i , w e i g h t i ] edges[i] = [from_i, to_i, weight_i] edges[i]=[fromi,toi,weighti] 表示在 f r o m i from_i fromitoi to_itoiThere is a weighted undirected edge between the nodes. Minimum Spanning Tree (MST) (MST)( M S T ) is a subset of edges in a given graph, which connects all nodes and has no loops, and the sum of the weights of these edges is the smallest.

Please find all key edges and pseudo key edges of the minimum spanning tree in a given graph. If an edge is deleted from the graph, it will lead to an increase in the weight of the minimum spanning tree, then we say that it is a critical edge. Pseudo critical edges are edges that may appear in some minimum spanning trees but not in all minimum spanning trees.

Please note that you can return the subscripts of key edges and pseudo-key edges in any order, respectively.
Example 1:
Insert picture description here
Input: n = 5, edges = [[0,1,1],[1,2,1],[2,3,2],[0,3,2],[0,4,3],[3,4,3],[1,4,6]]
Output: [[0,1],[2,3,4,5]]
Explanation: The figure above describes the given figure.
The figure below shows all the minimum spanning trees.
Insert picture description here
Note that the 0th edge and the 1st edge appear in all the minimum spanning trees, so they are the key edges. We use these two subscripts as the first list of output.
Edges 2, 3, 4 and 5 are the remaining edges of all MSTs, so they are pseudo-critical edges. We will use them as the second list of output.
Example 2:
Insert picture description here
Input: n = 4, edges = [[0,1,1],[1,2,1],[2,3,1],[0,3,1]]
Output: [[],[0,1,2,3]]
Explanation: It can be observed that 4 edges have the same weight, and you can choose 3 of them to form an MST. So the 4 edges are all pseudo-critical edges.
Tips:
2 <= n <= 100 1 <= edges. Length <= min (200, n ∗ (n − 1) 2) edges [i]. Length = = 3 0 <= fromi <toi <n 1 <= weighti <= 1000 2 <= n <= 100\\ 1 <= edges.length <= min(200, \frac{n * (n-1)}{2})\\ edges[i].length == 3\\ 0 <= from_i <to_i <n\\ 1 <= weight_i <= 10002<=n<=1001<=edges.length<=m i n ( 2 0 0 ,2n(n1))edges[i].length==30<=fromi<toi<n1<=weighti<=1000
所有 ( f r o m i , t o i ) (from_i, to_i) (fromi,toi) Number pairs are different from each other.

Ideas

We first need to understand the definition of "critical edge" and "pseudo critical edge" in the title description:

  • Critical edge: If an edge is deleted from the minimum spanning tree, it will lead to an increase in the weight of the minimum spanning tree, then we say that it is a critical edge. In other words, if the weight of the minimum spanning tree of the original image is set to value \textit{value}value , then after removing this edge:

    • Either the entire graph is not connected and there is no minimum spanning tree;
    • Either the entire graph is connected, and the weight of the corresponding minimum spanning tree is vvv , which is strictly greater thanvalue \textit{value}value
  • Pseudo critical edges: edges that may appear in some minimum spanning trees but not in all minimum spanning trees. That is to say, we can consider this edge first in the process of calculating the minimum spanning tree, that is, first merge the two endpoints of this edge in the merge query set. Let the final minimum spanning tree weight be vvv , ifv = value v = \textit{value}v=value , then this edge is a pseudo-critical edge.

It should be noted that key edges also satisfy the corresponding properties of pseudo-key edges. Therefore, we first execute Kruskal \texttt{Kruskal} on the original imageKruskal algorithm, get the weight value of the minimum spanning tree\textit{value}value , then we enumerate each edge, first judge whether it is a critical edge according to the above method, if it is not a critical edge, then judge whether it is a pseudo-critical edge.

#include <bits/stdc++.h>

using namespace std;
const int maxn = 110;
int a[maxn][maxn];

int main() {
    
    
    int n;
    while (~scanf("%d", &n)) {
    
    
        if (!n) break;
        for (int i = 1; i <= n; i++) {
    
    
            int x = i, y = i;
            for (int j = 1; j <= n - i + 1; j++) {
    
    
                a[x][y] = j;
                x++;
            }
            x = i;
            y = i;
            for (int j = 1; j <= n - i + 1; j++) {
    
    
                a[x][y] = j;
                y++;
            }
        }
        for (int i = 1; i <= n; i++) {
    
    
            for (int j = 1; j <= n; j++) {
    
    
                printf("%d", a[i][j]);
                if (j == n) puts(""); else printf(" ");
            }
        }
        puts("");
    }
    return 0;
}

Guess you like

Origin blog.csdn.net/weixin_43601103/article/details/112919767