If the length of each edge on the MST is equal to that of other edges, delete the edge and calculate the MST again. If it is the same as the original cost, is not unique
# Include <cstdio> # include <cstring> # include <cmath> # include <algorithm> # include <climits> # include <string> # include <iostream> # include <map> # include <cstdlib> # include <list> # include <set> # include <queue> # include <stack> using namespace STD; typedef long ll; const int maxn = 105; const int maxk = 105*105; struct edge {int U, V, W; bool operator <(const edge & X) const {return W <X. W ;}}; edge e [Max K]; bool del [maxk], EQU [maxk], used [maxk]; int n, m, Fa [maxn]; int findp (int x) {return x = Fa [x]? X: Fa [x] = findp (Fa [x]);} int Kruskal (bool flag) {int ret = 0; For (INT I = 1; I <= N; I ++) Fa [I] = I; for (INT I = 0; I <m; I ++) if (! Del [I]) {int Pa = findp (E [I]. u), Pb = findp (E [I]. v); If (Pa = Pb) continue; If (FLAG) used [I] = true; Fa [PA] = Pb; RET + = E [I]. w;} return ret;} int main () {int t; scanf ("% d", & T); While (t --) {scanf ("% d", & N, & M); memset (Del, 0, sizeof (DEL); memset (equ, 0, sizeof (equ); memset (used, 0, sizeof (used); For (INT I = 0; I <m; I ++) {scanf ("% d", & E [I]. u, & E [I]. v, & E [I]. w);} Sort (E, E + M); For (INT I = 1; I <m; I ++) {If (E [I]. W = E [I-1]. w) {equ [I] = equ [I-1] = true ;}} int first = Kruskal (1); bool unique = true; For (INT I = 0; I <m; I ++) if (used [I] & equ [I]) {del [I] = true; int now = Kruskal (0 ); if (now = first) {unique = false; break ;}} if (unique) printf ("% d \ n", first); else puts ("not unique! ");} Return 0 ;}