poj 1679 The Unique MST
来源:互联网 发布:淘宝刷单权重 编辑:程序博客网 时间:2024/06/14 21:22
The Unique MST
Time Limit: 1000MS Memory Limit: 10000KTotal Submissions: 20787 Accepted: 7315
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!
这题就是求次小生成树。
#include<cstdio>#include<iostream>#include<cstring>#include<algorithm>#define Maxn 110using namespace std;struct line{ int u,v,w; bool operator<(const line &a)const{ return w<a.w; }}p[Maxn*Maxn];int fa[Maxn],head[Maxn],ed[Maxn],nxt[Maxn],use[Maxn*Maxn],maxcost[Maxn][Maxn];void init(int n){ for(int i=1;i<=n;i++) fa[i]=head[i]=ed[i]=i,nxt[i]=-1;}int findset(int x){ return fa[x]==x?x:(fa[x]=findset(fa[x]));}void unionset(int a,int b){ fa[b]=a; nxt[ed[a]]=head[b]; ed[a]=ed[b];}int kruskal(int n,int m){ init(n); int ans=0,cnt=0; for(int i=0;i<m;i++){ int a=findset(p[i].u),b=findset(p[i].v); if(a!=b){ ans+=p[i].w; for(int j=head[a];j!=-1;j=nxt[j]) for(int k=head[b];k!=-1;k=nxt[k]) maxcost[j][k]=maxcost[k][j]=p[i].w; unionset(a,b); use[i]=1; if(++cnt==n-1) break; } } return ans;}int main(){ int t,n,m; scanf("%d",&t); while(t--){ scanf("%d%d",&n,&m); for(int i=0;i<m;i++) scanf("%d%d%d",&p[i].u,&p[i].v,&p[i].w); sort(p,p+n); memset(use,0,sizeof use); int ans=kruskal(n,m); bool flag=false; for(int i=0;i<m;i++) if(!use[i]&&p[i].w==maxcost[p[i].u][p[i].v]) {flag=true;break;} if(flag) puts("Not Unique!"); else printf("%d\n",ans); } return 0;}
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
- 学习网络基础ccna
- 周鸿弈自述-我的互联网方法论 读后感
- 计算四则表达式(中缀式转后缀式,然后计算结果)
- 读懂Spring核心系列3(自动装配属性)
- 数学建模高级方法(三):灰色关联分析,复相关系数
- poj 1679 The Unique MST
- yum 常用参数
- qt creator 在linux下不能调试的解决方案
- C++11 this_thread::get_id(9)
- poj 2656 Unhappy Jinjin
- elasticsearch-curator的安装
- QT Creator 快捷键
- 刚问过
- Subshell 执行