借用的是Kruskal的并查集,算法中的一点添加和改动。

通过判定其中有多少条可选的边,然后跟最小生成树所需边做比较,可选的边多于所选边,那么肯定方案不唯一。

如果不知道这个最小生成树的算法,还是先去理解下这个算法的原理,再继续看。

多加的几行与cnt2很类似的cnt1统计,是统计当前未选边中相同长度的边还有哪些可以选,但不标记,只是为了把当前可选的边全部统计出来。

其他细节,自己要细心点~

 #include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <math.h>
#include <iostream>
#include <algorithm>
#include <climits>
#include <queue> using namespace std; //Kruskal算法 求最小生成树是否唯一 const int MAX = ;
int F[MAX];
struct Edge
{
int u,v,w;
} edge[MAX*MAX];
int tol;
void addedge(int u,int v,int w)
{
edge[tol].u = u;
edge[tol].v = v;
edge[tol++].w = w;
}
bool cmp(Edge a,Edge b)
{
return a.w < b.w;
}
int find(int x)
{
if(F[x] == -) return x;
else return F[x] = find(F[x]);
}
void Kruskal(int n)
{
memset(F,-,sizeof(F));
sort(edge,edge+tol,cmp);
int cnt1 = ,cnt2 = ;
int ans = ;
for(int i = ; i < tol; )
{
int j = i;
while(j < tol && edge[j].w == edge[i].w)
{
int u = edge[j].u;
int v = edge[j].v;
int t1 = find(u);
int t2 = find(v);
if(t1 != t2)
{
cnt1++;
}
j++;
}
j = i;
while(j < tol && edge[j].w == edge[i].w)
{
int u = edge[j].u;
int v = edge[j].v;
int w = edge[j].w;
int t1 = find(u);
int t2 = find(v);
if(t1 != t2)
{
ans += w;
F[t1] = t2;
cnt2++;
}
j++;
}
i = j;
if(cnt2 == n - )
break;
}
//printf("%d %d\n",cnt1,cnt2);
if(cnt1 > cnt2 || cnt2 != n -)
printf("Not Unique!\n");
else
printf("%d\n",ans);
}
int main(void)
{
int t,m,n,i,x,y,w;
scanf("%d",&t);
while(t--)
{
scanf("%d %d",&n,&m);
tol = ;
for(i = ; i < m; i++)
{
scanf("%d %d %d",&x,&y,&w);
addedge(x,y,w);
}
Kruskal(n);
}
return ;
}

poj 1679 The Unique MST 判断最小生成树是否唯一(图论)的更多相关文章

  1. poj 1679 The Unique MST (判定最小生成树是否唯一)

    题目链接:http://poj.org/problem?id=1679 The Unique MST Time Limit: 1000MS   Memory Limit: 10000K Total S ...

  2. POJ 1679 The Unique MST 推断最小生成树是否唯一

    The Unique MST Time Limit: 1000MS   Memory Limit: 10000K Total Submissions: 22715   Accepted: 8055 D ...

  3. 【POJ 1679 The Unique MST】最小生成树

    无向连通图(无重边),判断最小生成树是否唯一,若唯一求边权和. 分析生成树的生成过程,只有一个圈内出现权值相同的边才会出现权值和相等但“异构”的生成树.(并不一定是最小生成树) 分析贪心策略求最小生成 ...

  4. POJ 1679 The Unique MST(最小生成树)

    Description Given a connected undirected graph, tell if its minimum spanning tree is unique. Definit ...

  5. POJ 1679 The Unique MST (最小生成树)

    The Unique MST 题目链接: http://acm.hust.edu.cn/vjudge/contest/124434#problem/J Description Given a conn ...

  6. POJ 1679 The Unique MST 【最小生成树/次小生成树模板】

    The Unique MST Time Limit: 1000MS   Memory Limit: 10000K Total Submissions: 22668   Accepted: 8038 D ...

  7. (poj)1679 The Unique MST 求最小生成树是否唯一 (求次小生成树与最小生成树是否一样)

    Description Given a connected undirected graph, tell if its minimum spanning tree is unique. Definit ...

  8. poj 1679 The Unique MST 【次小生成树】【模板】

    题目:poj 1679 The Unique MST 题意:给你一颗树,让你求最小生成树和次小生成树值是否相等. 分析:这个题目关键在于求解次小生成树. 方法是,依次枚举不在最小生成树上的边,然后加入 ...

  9. POJ 1679 The Unique MST(判断最小生成树是否唯一)

    题目链接: http://poj.org/problem?id=1679 Description Given a connected undirected graph, tell if its min ...

随机推荐

  1. 【JZOJ3238】【BZOJ3482】超空间旅行

    description 在遥远的未来,行星之间的食品运输将依靠单向的贸易路线.每条路径直接连接两个行星,且其运输时间是已知的. 贸易商协会打算利用一项最近发现的新技术--超空间旅行,以增加一些新的航线 ...

  2. MSSQLSERVER跨服务器连接(远程登录)的示例代码

    MSSQLSERVER跨服务器链接服务器创建方法如下 复制代码 代码如下: --声明变量 Declare @svrname varchar(255), @dbname varchar(255), @s ...

  3. String相加解析

    Java代码 package com.utils.test; public class TestObject { public static void main(String[] args) { St ...

  4. strict

    strict为3.2.3新增连贯操作,用于设置数据写入和查询是否严格检查是否存在字段.默认情况下不合法数据字段自动删除,如果设置了严格检查则会抛出异常. 例如: $model->strict(t ...

  5. 解决无法wifi上网的问题

    1.查看网卡型号 lspci | grep Network 可以看到我的是Wireless-AC 9560 2.登录Inter官网下载网卡驱动 https://www.intel.com/conten ...

  6. [POI2014]KAR-Cards

    题目链接: 传送门 题目分析: 线段树妙题,感觉思路奇奇怪怪的,虽然对我来说不是"线段树菜题"(\(ldx\)神仙\(blog\)原话)\(QAQ\) 考虑怎么样维护可合并的信息解 ...

  7. opencv-图像类型、深度、通道

    转自:图像类型   与  opencv中图像基础(大小,深度,通道) 一.图像基本类型 在计算机中,按照颜色和灰度的多少可以将图像分为四种基本类型. 1. 二值图像 2. 灰度图像 3. 索引图像 4 ...

  8. PAT甲级——A1068 Find More Coins

    Eva loves to collect coins from all over the universe, including some other planets like Mars. One d ...

  9. 查询sitemap中重复的记录

    ; ;

  10. 关于Collection接口和Map

    Iterable才是Collection的父接口.不是Iterator. Map,SortedMap属于接口类型,不可以new的方式创建对象. HashMap基于哈希表实现Map接口的类,并允许nul ...