Find the minimum spanning tree while using max[i][j] to record the maximum edge on the unique path of I to J. Then replace it with an edge i-j not in the smallest spanning tree to see if the difference is 0.
#include <algorithm> #include <cstdio> #include <cstring>using namespace std;const int N=101;const int Inf=0x3f3f3f3f;int n,m,ans,s;int lowc[n],vis[n],g[n][n];int max[n][n],fa[n],used[n][n];int Prim () {int Ans=0;memset ( Max,0,sizeof Max); memset (used,0,sizeof used); memset (vis,0,sizeof Vis); vis[0]=1;for (int i=1;i<n;i++) {lowc[i]=g[0 ][i];fa[i]=0;} for (int i=1;i<n;i++) {int p=0;while (vis[p]) p++;for (int j=p+1;j<n;j++) if (!vis[j] && lowc[j]<lowc[p]) P=j;if (Lowc[p]==inf) return-1;//original not connected ans+=lowc[p];vis[p]=1;used[p][fa[p]]=used[fa[p]][p]=1;for (int j=0;j<n;j+ +) if (Vis[j]) Max[j][p]=max[p][j]=max (max[j][fa[p]],lowc[p]), else if (G[p][j]<lowc[j]) {lowc[j]=g[p][j];fa[j]=p;}} return ans;} int main () {int t;scanf ("%d", &t), while (t--) {memset (g,inf,sizeof g); scanf ("%d%d", &n,&m) and while (m--) {int U , v,w;scanf ("%d%d%d", &u,&v,&w); u--;v--;g[u][v]=g[v][u]=w;} Ans=prim (); s=inf;for (int i=0;i<n;i++) for (int j=i+1;j<n;j++) if (G[i][j]!=inf&&!used[i][j]) s=min (S,g[i][j]-max[i][j]); if (ans==-1| | s==0) puts ("Not unique!"); elseprintf ("%d\n", ans); return 0;}
WA has several rounds, because S is initialized to ans, and if ans itself is 0, it should be the only minimal spanning tree.
"POJ 1679" the Unique MST (sub-niche into a tree)