POJ 1679 The Unique MST
来源:互联网 发布:高晓松评价张学良 知乎 编辑:程序博客网 时间:2024/06/04 23:24
The Unique MST
Time Limit: 1000MS Memory Limit: 10000KTotal Submissions: 29079 Accepted: 10398
Description
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'.
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
23 31 2 12 3 23 1 34 41 2 22 3 23 4 24 1 2
Sample Output
3Not Unique!
Source
POJ Monthly--2004.06.27 srbga@POJ
1 #include<iostream> 2 #include<cstdio> 3 #include<cstring> 4 #include<algorithm> 5 using namespace std; 6 const int VM=120; 7 const int EM=10010; 8 struct Edge{ 9 int u,v,cap;10 bool operator < (const Edge& a)const {11 return cap<a.cap;12 }13 }edge[EM<<1];14 int n,m,flag,ans,father[VM];15 void makeSet(){16 for(int i=1;i<=n;i++)17 father[i]=i;18 }19 int findSet(int x){20 if(x==father[x]) return x;21 else return father[x]=findSet(father[x]);22 }23 void Kruskal(){24 makeSet();25 sort(edge,edge+m);26 int path[EM],cnt=0;27 ans=0;28 for(int i=0;i<m;i++){29 int u=findSet(edge[i].u);30 int v=findSet(edge[i].v);31 if(u!=v){32 father[v]=u; ans+=edge[i].cap;33 path[cnt++]=i; //记录路径34 }35 }36 for(int k=0;k<cnt;k++){ //枚举去掉每一条边37 makeSet();38 int sum=0,j=0;39 for(int i=0;i<m;i++){40 if(i==path[k]) continue;41 int u=findSet(edge[i].u),v=findSet(edge[i].v);42 if(u!=v){43 father[v]=u;44 sum+=edge[i].cap;45 j++;46 }47 }48 if(j==n-1 && sum==ans){ //判断是否能构成树 且 是否与最小生成树相等49 flag=0;return ;50 }51 }52 }53 int main(){54 int T;55 scanf("%d",&T);56 while(T--){57 scanf("%d%d",&n,&m);58 for(int i=0;i<m;i++)59 scanf("%d%d%d",&edge[i].u,&edge[i].v,&edge[i].cap);60 flag=1;61 Kruskal();62 if(flag) printf("%d\n",ans);63 else printf("Not Unique!\n");64 }65 return 0;66 }
思路:先来一遍最小生成树,此时顺便记录下树边,然后枚举删除掉树边,每次删除后看是否还能生成一棵最小生成树和原最小生成树的MST相等..
0 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
- 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
- POJ 1679 The Unique MST
- POJ-1679-The Unique MST
- poj 1679 The Unique MST
- poj 1679-The Unique MST
- OpenJudge 东方14ACM小组 / 20170123 06:Challenge 3
- Scala语言初步
- POJ 2349 Arctic Network
- windows 直接用命令提取.msi安装文件到特定路径
- 最小二乘法总结
- POJ 1679 The Unique MST
- python代码`if not x:` 和`if x is not None:`和`if not x is None:`使用
- POJ 1064 Cable master
- 左滑返回手势失效问题
- nvm 安装node.js问题Computing checksum with shasum -a 256 Checksums do not match:
- Codevs 3578 无线网络发射器选址== NOIP 2014 Day2 T1
- <琐碎的黑盒测试日常>[日记1]
- NOIP2013/day1/1/转圈游戏
- POJ 1979 Red and Black