TheUniqueMSTTimeLimit:1000MSMemoryLimit:10000KTotalSubmissions:28409Accep The Unique MSTTime Limit: 1000MS Memory Limit: 10000KTotal Submissions: 28409 Accepted: 10153DescriptionGiven a connected undirected graph, tell if its minimum spannin
TheUniqueMSTTimeLimit:1000MSMemoryLimit:10000KTotalSubmissions:28409Accep The Unique MSTTime Limit: 1000MS Memory Limit: 10000KTotal Submissions: 28409 Accepted: 10153DescriptionGiven a connected undirected graph, tell if its minimum spanning tree is unique.Definition 1 (Spanning Tree): Consider a connected, undirected graph G = (V, E). A spanning tree of G is a subgraph of G, say T = (V', E'), with the following properties:1. V' = V.2. T is connected and acyclic.Definition 2 (Minimum Spanning Tree): Consider an edge-weighted, connected, undirected graph G = (V, E). The minimum spanning tree T = (V, E') of G is the spanning tree that has the smallest total cost. The total cost of T means the sum of the weights on all the edges in E'.InputThe first line contains a single integer t (1 <= t <= 20), the number of test cases. Each case represents a graph. It begins with a line containing two integers n and m (1 <= n <= 100), the number of nodes and edges. Each of the following m lines contains a triple (xi, yi, wi), indicating that xi and yi are connected by an edge with weight = wi. For any two nodes, there is at most one edge connecting them.OutputFor each input, if the MST is unique, print the total cost of it, or otherwise print the string 'Not Unique!'.Sample Input23 31 2 12 3 23 1 34 41 2 22 3 23 4 24 1 2Sample Output3Not Unique!SourcePOJ Monthly--2004.06.27 srbga@POJ
在图G的最小生成树T 中 如果往T加入一条不属于T 但属于G的边e 就得到一个包含边e的环 在环中删除掉除了e以外的任意一条边 就得到一棵新的生成树 显然 对加入边e的情况 删除环中除了e外 权值的最大的边 得到的生成树最小 枚举每条不属于T的边 并删去环中权值最大的边 就得到图G的次小生成树 不难理解 kruskal算法在加入一条边e={a,b,w}时 a所在集合必定与b所在集合不联通 且任意一点u属于a所在集合 任意一点v属于b所在集合 在加入该边后 uv之间的路径中 边的权值最大就是w了(e之前加入的边 权值都比e小) 那么在kruskal算法运行时 就可以得到任意2点间权值最大的边
#include#include#include#include#include#include#include#include#include#include#include#include#include#include#include#include//#includeusing namespace std;#define ll long long#define ull unsigned long long#define pii pair#define INF 1000000007#define pll pair#define pid pairconst int N=100+5;struct edge{ int a,b,w; bool selected;//是否包含在最小生成树中};vectorvedge;vectorG[N];//G[i]记录所有与i联通的点 (i所在集合的点)int par[N];//并查集int len[N][N];//len[i][j]:ij路径中权值最大的边权int mst;//最小生成树值int secmst;//次小生成树值inline int find(int x){ return x==par[x]?x:par[x]=find(par[x]);}inline void merge(int x,int y){ par[find(y)]=find(x);}bool comp(const edge if(a.a!=b.a) return a.a