Give n coordinate points, some of which are connected
Find a Minimum Spanning Tree and output the two endpoints of the edge to be connected, so you have to record the path
In fact, the kruskal algorithm should be more concise for this output edge question.
# Include
# Include
# Include
# Include
# Include
# Include
# Include
# Include
# Define inf 0x3f3f3f # define ll _ int64using namespace std; int n, vis [755], pre [755], x [755], y [755], d [755], e [755] [755]; void prim () {int I, j, p, tmp; memset (vis, 0, sizeof vis ); for (I = 2; I <= n; I ++) {pre [I] = 1; // record the previous vertex d [I] = e [1] [I];} d [1] = 0; vis [1] = 1; for (I = 2; I <= n; I ++) {tmp = inf; p = 0; for (j = 1; j <= n; j ++) {if (! Vis [j] & tmp> d [j]) {tmp = d [j]; p = j ;}} if (tmp! = 0) printf ("% d \ n", pre [p], p); if (tmp = inf) break; vis [p] = 1; for (j = 1; j <= n; j ++) {if (! Vis [j] & d [j]> e [p] [j]) {d [j] = e [p] [j]; pre [j] = p ;}}} int main () {int I, j, a, B, q; scanf ("% d", & n ); for (I = 1; I <= n; I ++) scanf ("% d", & x [I], & y [I]); for (I = 1; I <= n; I ++) {for (j = 1; j <= I; j ++) {if (I = j) e [I] [j] = 0; // The specific distance value does not affect the judgment size. Therefore, you can also choose not the root number else e [I] [j] = e [j] [I] = (x [I]- x [j]) * (x [I]-x [j]) + (y [I]-y [j]) * (y [I]-y [j]);} scanf ("% d", & q); while (q --) {scanf ("% d", & a, & B ); // The connected edge directly sets the edge weight to 0 e [a] [B] = e [B] [a] = 0;} prim (); return 0 ;}