poj 1679 The Unique MST 判断最小生成树是否唯一(图论)
借用的是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 判断最小生成树是否唯一(图论)的更多相关文章
- poj 1679 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 Total Submissions: 22715 Accepted: 8055 D ...
- 【POJ 1679 The Unique MST】最小生成树
无向连通图(无重边),判断最小生成树是否唯一,若唯一求边权和. 分析生成树的生成过程,只有一个圈内出现权值相同的边才会出现权值和相等但“异构”的生成树.(并不一定是最小生成树) 分析贪心策略求最小生成 ...
- POJ 1679 The Unique MST(最小生成树)
Description Given a connected undirected graph, tell if its minimum spanning tree is unique. Definit ...
- POJ 1679 The Unique MST (最小生成树)
The Unique MST 题目链接: http://acm.hust.edu.cn/vjudge/contest/124434#problem/J Description Given a conn ...
- POJ 1679 The Unique MST 【最小生成树/次小生成树模板】
The Unique MST Time Limit: 1000MS Memory Limit: 10000K Total Submissions: 22668 Accepted: 8038 D ...
- (poj)1679 The Unique MST 求最小生成树是否唯一 (求次小生成树与最小生成树是否一样)
Description Given a connected undirected graph, tell if its minimum spanning tree is unique. Definit ...
- poj 1679 The Unique MST 【次小生成树】【模板】
题目:poj 1679 The Unique MST 题意:给你一颗树,让你求最小生成树和次小生成树值是否相等. 分析:这个题目关键在于求解次小生成树. 方法是,依次枚举不在最小生成树上的边,然后加入 ...
- POJ 1679 The Unique MST(判断最小生成树是否唯一)
题目链接: http://poj.org/problem?id=1679 Description Given a connected undirected graph, tell if its min ...
随机推荐
- 记mysql 启动不了了的解决方法
系统: centos7 本地的环境,mysql启动不了,查看 /var/log/mysqld.log 有以下内容 2018-12-24T08:05:38.090527Z 0 [Warning] TIM ...
- Tomcat小技巧
目录 1.项目路径忽略项目名 2.配置tomcat虚拟目录 3.显示目录文件列表 4.设置URL不区分大小写 1.项目路径忽略项目名 server.xml中修改Context标签中的path属性为/ ...
- 廖雪峰Java15JDBC编程-3JDBC接口-1JDBC简介
JDBC:Java DataBase Connectivity Java程序访问数据库的标准接口 使用Java程序访问数据库的时候,Java代码并不是直接通过TCP连接去访问数据库,而是通过JDBC接 ...
- C++内存字节对齐规则
为什么要进行内存对齐以及对齐规则 C/C++—— 内存字节对齐规则 C++内存字节对齐规则
- poj 2774 字符串哈希求最长公共子串
Long Long Message #include <iostream> #include <algorithm> #include <cstdio> #incl ...
- System.Text.Encoding.cs
ylbtech-System.Text.Encoding.cs 1.程序集 mscorlib, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77 ...
- System.Web.Mvc.ViewResultBase.cs
ylbtech-System.Web.Mvc.ViewResultBase.cs 1.程序集 System.Web.Mvc, Version=5.2.3.0, Culture=neutral, Pub ...
- charles-过滤网络请求方法
方法一:在主界面的中部的 Filter 栏中填入需要过滤出来的关键字.例如我们的服务器的地址是:https://www.baidu.com , 那么只需要在 Filter 栏中填入 https://w ...
- 验证occ和vtk整合工作的demo
在编译occ通过过后,我需要验证occ是否能够正常结合vtk进行开发工作 使用CMake进行环境变量设置: CMakeList.txt PROJECT (IGESReader) #VTK Part: ...
- 二分判定 覆盖问题 BZOJ 1052
//二分判定 覆盖问题 BZOJ 1052 // 首先确定一个最小矩阵包围所有点,则最优正方形的一个角一定与矩形一个角重合. // 然后枚举每个角,再解决子问题 #include <bits/s ...