POJ 1679 The Unique MST 次小生成树
2014-08-05 11:46
525 查看
The Unique MST
Given 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'. Input The 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. Output For each input, if the MST is unique, print the total cost of it, or otherwise print the string 'Not Unique!'. Sample Input 2 3 3 1 2 1 2 3 2 3 1 3 4 4 1 2 2 2 3 2 3 4 2 4 1 2 Sample Output 3 Not Unique! Source POJ Monthly--2004.06.27 srbga@POJ |
[Discuss]
Home Page
Go
Back
To top
#include <cstdlib> #include <cctype> #include <cstring> #include <cstdio> #include <cmath> #include <algorithm> #include <vector> #include <string> #include <iostream> #include <sstream> #include <map> #include <set> #include <queue> #include <stack> #include <fstream> #include <numeric> #include <iomanip> #include <bitset> #include <list> #include <stdexcept> #include <functional> #include <utility> #include <ctime> using namespace std; #define PB push_back #define MP make_pair #define CLR(vis) memset(vis,0,sizeof(vis)) #define MST(vis,pos) memset(vis,pos,sizeof(vis)) #define MAX3(a,b,c) max(a,max(b,c)) #define MAX4(a,b,c,d) max(max(a,b),max(c,d)) #define MIN3(a,b,c) min(a,min(b,c)) #define MIN4(a,b,c,d) min(min(a,b),min(c,d)) #define PI acos(-1.0) #define INF 0x7FFFFFFF #define LINF 1000000000000000000LL #define eps 1e-8 typedef long long ll; typedef unsigned long long ull; const int maxn=111; int father[maxn]; void build(int n) { for(int i=1;i<=n;i++) father[i]=i; } int find(int x) { if(father[x]!= x) father[x]=find(father[x]); return father[x]; } void merge(int x,int y) { int xx=find(x); int yy=find(y); father[xx]=yy; } const int maxm=maxn*maxn; struct edge{ int u,v; int w; bool select; }e[maxm]; bool cmp(edge A,edge B) { if(A.w!=B.w) return A.w<B.w; if(A.u!=B.u) return A.u<B.u; return A.v<B.v; } struct node{ int to; int next; }link[maxn]; int il; int head[maxn]; int end[maxn]; int len[maxn][maxn]; void kruskal(edge * e,int n,int m) { int k=0; int i,x,y; int w,v; for(il=0;il<n;il++) { link[il].to=il+1; link[il].next=head[il+1]; end[il+1]=il; head[il+1]=il; } sort(e+1,e+1+m,cmp); for(i=1;i<=m;i++) { if(k==n-1) break; if(e[i].w<0) continue; x=find(e[i].u); y=find(e[i].v); if(x!=y) { for(w=head[x];~w;w=link[w].next) { for(v=head[y];~v;v=link[v].next) { len[link[w].to][link[v].to]=len[link[v].to][link[w].to]=e[i].w; } } link[end[y]].next=head[x]; end[y]=end[x]; merge(x,y); k++; e[i].select=true; } } } int main() { int t; int n,m; int u,v,w; cin>>t; while(t--) { scanf("%d%d",&n,&m); build(n); MST(head,-1); for(int i=1;i<=m;i++) { scanf("%d%d%d",&u,&v,&w); e[i].u=u; e[i].v=v; e[i].w=w; e[i].select=false; } int mst,secmst; kruskal(e,n,m); mst=0; for(int i=1;i<=m;i++) { if(e[i].select) mst+=e[i].w; } secmst=INF; for(int i=1;i<=m;i++) { if(!e[i].select) secmst=min(secmst,mst+e[i].w-len[e[i].u][e[i].v]); } if(mst==secmst) printf("Not Unique!\n"); else printf("%d\n",mst); } return 0; }
相关文章推荐
- POJ 1679 The Unique MST(次小生成树)
- poj 1679 The Unique MST (次小生成树)
- POJ 1679 The Unique MST(次小生成树)
- POJ 1679 The Unique MST (次小生成树)
- POJ 1679 The Unique MST(次小生成树)
- (kruscal12.3.4)POJ 1679 The Unique MST(判断最小生成树的唯一性||次小生成树)
- poj 1679 The Unique MST(次小生成树)
- poj 1679 The Unique MST (次小生成树)
- POJ 1679 -- The Unique MST(次小生成树)
- poj 1679 The Unique MST(次小生成树)
- POJ - 1679 The Unique MST (次小生成树)
- POJ 1679 The Unique MST(次小生成树)
- POJ 1679 The Unique MST (次小生成树)
- POJ 1679 The Unique MST 次小生成树Prim
- POJ1679_The Unique MST(次小生成树)
- POJ-1679 The Unique MST 次小生成树
- POJ 1679 The Unique MST (次小生成树)
- poj1679 The Unique MST(判定次小生成树)
- POJ 1679 The Unique MST(最小生成树--prime||次小生成树)
- POJ - 1679 The Unique MST(次小生成树)