借用的是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. Leetcode166. Fraction to Recurring Decimal分数到小数

    给定两个整数,分别表示分数的分子 numerator 和分母 denominator,以字符串形式返回小数. 如果小数部分为循环小数,则将循环的部分括在括号内. 示例 1: 输入: numerator ...

  2. It's a Mod, Mod, Mod, Mod World (类欧几里得模板题

    https://vjudge.net/contest/317000#problem/F #include <iostream> #include <cstdio> #inclu ...

  3. 容斥原理——hdu3208

    和hdu2204有点像 这题要特别注意精度问题,如pow的精度需要自己搞一下,然后最大的longlong可以设为1<<31 /* 只要求[1,n]范围内的sum即可 那么先枚举幂次k[1, ...

  4. LUOGU P1342 请柬(最短路)

    传送门 解题思路 又是一道语文题,弄清楚题意之后其实就能想出来了,从1跑一遍最短路,把$dis[n]$加入答案.在建个反图跑一遍最短路,把$dis[n]_$加入最短路就行了.第一遍是去的时候,第二遍是 ...

  5. 19-11-13-Night-∠

    连夜补博客 ZJ: 看见T1就自闭了.(高考数学)(但是好像不是) 三个暴力就结束了. 35 Miemeng 20 00:00:41 10 00:00:41 10 00:00:41 40 00:00: ...

  6. 19-11-10-Night

    关于$Miemeng$,它死了. 大家有没有记得我在暑假里曾经写过一个著名模数? const int Mod=998224353; 现在有续集了(捂脸)(改不过题.jpg) const int Mod ...

  7. js实现获取两个日期之间所有日期的方法

    function getDate(datestr){ var temp = datestr.split("-"); var date = new Date(temp[0],temp ...

  8. VS2010-MFC(对话框:创建对话框类和添加控件变量)

    转自:http://www.jizhuomi.com/software/153.html 前两讲中讲解了如何创建对话框资源.创建好对话框资源后要做的就是生成对话框类了.生成对话框类主要包括新建对话框类 ...

  9. iOS之CGPath的应用(二)

    1.矩形路径 CG_EXTERN CGPathRef CGPathCreateWithRect(CGRect rect, const CGAffineTransform * __nullable tr ...

  10. 关于socket的setsockopt的使用

    关于setsockopt的使用 学习python的时候学习到了socket,其中有个setsockopt方法的使用,于是乎整理一下关于这个方法的一些内容. 本节目录 一 功能描述 二 用法(getso ...