[poj1679]The Unique MST(最小生成树)
Time Limit: 1000MS | Memory Limit: 10000K | |
Total Submissions: 28207 | Accepted: 10073 |
Description
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
Output
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
#include<cstdio>
#include<cstdlib>
#include<cstring>
#include<cmath>
#include<climits>
#include<algorithm>
#include<queue>
#define LL long long
using namespace std;
typedef struct{
int to,frm,dis;
}edge;
edge gra[];
int num=,fa[];
int n,m;
int cmp(const edge &a,const edge &b){
return a.dis<b.dis;
}
int fnd(int x){
return x==fa[x]?x:fnd(fa[x]);
}
int uni(int x,int y){
int fx=fnd(x);
int fy=fnd(y);
fa[fy]=fx;
return ;
}
inline int read(){
int sum=;char ch=getchar();
while(ch>''||ch<'')ch=getchar();
while(ch<=''&&ch>=''){
sum=sum*+ch-'';
ch=getchar();
}
return sum;
}
int kru(){
int ans=;
sort(gra+,gra+m+,cmp);
for(int i=;i<=n;i++)fa[i]=i;
for(int i=;i<=m;i++){
int x=gra[i].frm;
int y=gra[i].to;
int fx=fnd(x);
int fy=fnd(y);
if(fx!=fy){
int j=i+;
while(j<=m&&gra[j].dis==gra[i].dis){
int y1=gra[j].frm;
int x1=gra[j].to;
int fy1=fnd(y1);
int fx1=fnd(x1);
if((fx1==fx&&fy1==fy)||(fx1==fy&&fy1==fx))return -;
j++;
}
ans+=gra[i].dis;
uni(fx,fy);
}
}
return ans;
}
int main(){
int t;
t=read();
while(t--){
memset(gra,,sizeof(gra));
n=read(),m=read();
num=;
for(int i=;i<=m;i++){
gra[i].frm=read();
gra[i].to=read();
gra[i].dis=read();
} int ans=kru();
if(ans==-)printf("Not Unique!\n");
else printf("%d\n",ans);
}
return ;
}
[poj1679]The Unique MST(最小生成树)的更多相关文章
- POJ1679 The Unique MST(Kruskal)(最小生成树的唯一性)
The Unique MST Time Limit: 1000MS Memory Limit: 10000K Total Submissions: 27141 Accepted: 9712 D ...
- POJ-1679 The Unique MST(次小生成树、判断最小生成树是否唯一)
http://poj.org/problem?id=1679 Description Given a connected undirected graph, tell if its minimum s ...
- POJ1679 The Unique MST[次小生成树]
The Unique MST Time Limit: 1000MS Memory Limit: 10000K Total Submissions: 28673 Accepted: 10239 ...
- POJ1679 The Unique MST 【次小生成树】
The Unique MST Time Limit: 1000MS Memory Limit: 10000K Total Submissions: 20421 Accepted: 7183 D ...
- POJ1679 The Unique MST 2017-04-15 23:34 29人阅读 评论(0) 收藏
The Unique MST Time Limit: 1000MS Memory Limit: 10000K Total Submissions: 29902 Accepted: 10697 ...
- POJ1679 The Unique MST —— 次小生成树
题目链接:http://poj.org/problem?id=1679 The Unique MST Time Limit: 1000MS Memory Limit: 10000K Total S ...
- POJ-1679 The Unique MST,次小生成树模板题
The Unique MST Time Limit: 1000MS Memory Limit: 10000K Description Given a connected undirec ...
- poj1679 The Unique MST(判定次小生成树)
The Unique MST Time Limit: 1000MS Memory Limit: 10000K Total Submissions: 23180 Accepted: 8235 D ...
- POJ-1679.The Unique MST.(Prim求次小生成树)
The Unique MST Time Limit: 1000MS Memory Limit: 10000K Total Submissions: 39561 Accepted: 14444 ...
随机推荐
- Design Tic-Tac Toe
Design a Tic-tac-toe game that is played between two players on a n x n grid. You may assume the fol ...
- uva 147 Dollars
http://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&page=show_problem&p ...
- Linux 发行版本及其基于
Independent ---> Debian Debian/Ubuntu(LTS) ---> Linux Mint ---> Linux Lite Debian/Ubuntu -- ...
- pod install 无限卡顿
pod install 被墙了,请大家换成pod install --verbose --no-repo-update
- (一)GPIO 编程实验 LED 流水灯控制
7个寄存器 是R1-R16.(当然,里面有很多是分几个模式的,所以总共有37个)类似于单片机的R0-R7. GPXCON,GPXDAT等等是另外的寄存器,应该叫,特殊功能寄存器,类似于单片机的P0,P ...
- JavaSE基础第四篇
1.参数传递 2,方法的重载 方法的参数的个数.类型.顺序 跟修饰符.返回值无关 3.构造方法: return 表示当前方法执行结束,后面不能写任何语句 4工程导入 单个.java文件粘贴 ...
- Lunix 命令
awk '{a[$1]+=1;if(a[$1]==1){print $0}}' awk -F ',' '{print $1, $6}' IS.csv | sort -k1n -k2n | awk ...
- iOS 推送小记
ios做推送功能时,最烦得就是各种证书的问题,以前自己做的时候经常要反复搞那些证书搞好几遍才能成功,现在发现归根到底都是appid这个东西搞错了,做个笔记记下来,以免忘了. 首先是程序里面注册推送的变 ...
- Android 控件属性介绍
1.LinearLayout(线性布局): 可以分为水平线性:android:orientation= " horizontal " 和垂直线性:android:orientati ...
- JS数组操作示意图(shift,unshift,pop,push)
shift:删除原数组第一项,并返回删除元素的值:如果数组为空则返回undefined var a = [1,2,3,4,5]; var b = a.shift(); //a:[2,3,4,5] b: ...