[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 ...
随机推荐
- leetcode 406
该算法题就是leetcode 406题, 题目描述: Suppose you have a random list of people standing in a queue. Each person ...
- 如何创建一个简单的C++同步锁框架(译)
翻译自codeproject上面的一篇文章,题目是:如何创建一个简单的c++同步锁框架 目录 介绍 背景 临界区 & 互斥 & 信号 临界区 互斥 信号 更多信息 建立锁框架的目的 B ...
- 对点餐APP现阶段开发的问题
团队的成立,基本是一气呵成.但是,github团队的建立却成为 第一个难题,大家对github都不熟,又刚刚好没课时间的任务,大家 已经各有安排,造成时间上的紧急.没有按时.按要求完成github的 ...
- 1.<%@Page%>中的Codebehind、AutoEventWireup、Inherits有何作用?
AutoEventWireup --- 指示是否自动启用页事件. Codebehind --- 指示后台代码文件. Inherits --- 继承类. AutoEventWireup:指示该页的事件是 ...
- mvc中测试网络
.//控制器层面 public string IsAnyNetworkAvailable() { try { foreach (NetworkInterface ni in NetworkInterf ...
- ubuntu执行sudo apt-get update提示缺少公钥
提示信息如下: 获取:1 http://archive.ubuntukylin.com:10006/ubuntukylin xenial InRelease [3,192 B] 命中:2 http:/ ...
- AC6102 DDR2测试工程
AC6102 DDR2测试工程 本文档介绍AC6102上DDR2存储器基于Verilog代码的测试过程.AC6102上使用了2片16bit的DDR2存储器组成了32bit的硬件总线.虽然是32bit硬 ...
- ip相关
查看ip ifconfig -a 查看端口占用 ps -aux | grep tomcat
- ASP.net gridview之性别
using System; using System.Collections.Generic; using System.Linq; using System.Web; using System.We ...
- js函数的传参是按值传对递
根据js高级程序设计第三版编写,对于函数的参数只能按值传递是这样解释的: ECMAScript 中所有函数的参数都是按值传递的.也就是说,把函数外部的值复制给函数内部的参数,就和把值从一个变量复制到另 ...