POJ 1679 The Unique MST 推断最小生成树是否唯一
Time Limit: 1000MS | Memory Limit: 10000K | |
Total Submissions: 22715 | Accepted: 8055 |
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
triple (xi, yi, wi), indicating that xi and yi are connected by an edge with weight = wi. For any two nodes, there is at most one edge connecting them.
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!
#include <iostream>
#include <stdio.h>
#include <string>
#include <cstring>
#include <cmath>
#include <algorithm>
#define N 10090
using namespace std; struct Node
{
int a,b,c;
bool same,used,del;
}f[N];
int n,m;
int fa[N]; int findfa(int x)
{
if(x!=fa[x])
fa[x]=findfa(fa[x]); return fa[x];
} void init()
{
for(int i=0;i<200;i++)
fa[i]=i;
} int cmp(Node a,Node b)
{
return a.c<b.c;
} bool first; void make_same(int m)
{
for(int i=1;i<m;i++)
if(f[i].c==f[i-1].c)
f[i-1].same=true;
} int kruscal(int m)
{
int ans=0;
for(int i=0;i<m;i++)
{
if(f[i].del)continue; int x=findfa(f[i].a);
int y=findfa(f[i].b); if(x==y)
continue;
else
{
fa[x]=y; ans+=f[i].c;
if(first)
f[i].used=true; }
}
return ans;
} int main()
{
int ca=1;
scanf("%d",&ca); while(ca--)
{
scanf("%d %d",&n,&m);
for(int i=0;i<m;i++)
{
scanf("%d %d %d",&f[i].a,&f[i].b,&f[i].c);
f[i].del=false;f[i].same=false;f[i].used=false;
} sort(f,f+m,cmp); first=true;
init(); int ans1=kruscal(m);
first=false; make_same(m); int flag=0; for(int i=0;i<m;i++)
{
if(f[i].used && f[i].same)//used表示在第一次求出的最小生成树中加入过的边
{//same表示在存在和已加入边权值同样的边,此时标记删除该边在推断是否ans相等 f[i].del=true;
init(); int ans2=kruscal(m); //cout<<"ans2="<<ans2<<endl;
if(ans1==ans2)
{
puts("Not Unique!");
flag=1;
break;
}
f[i].del=false;
}
} if(flag==0)
printf("%d\n",ans1); } return 0;
}
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 判断最小生成树是否唯一(图论)
借用的是Kruskal的并查集,算法中的一点添加和改动. 通过判定其中有多少条可选的边,然后跟最小生成树所需边做比较,可选的边多于所选边,那么肯定方案不唯一. 如果不知道这个最小生成树的算法,还是先去 ...
- 【POJ 1679 The Unique MST】最小生成树
无向连通图(无重边),判断最小生成树是否唯一,若唯一求边权和. 分析生成树的生成过程,只有一个圈内出现权值相同的边才会出现权值和相等但“异构”的生成树.(并不一定是最小生成树) 分析贪心策略求最小生成 ...
- 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(最小生成树)
Description Given a connected undirected graph, tell if its minimum spanning tree is unique. Definit ...
- 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 The Unique MST Time Limit: 1000MS Memory Limit: 10000K Total Submis ...
随机推荐
- PHP12 文件操作
学习要点 文件系统概述 目录基本操作 文件基本操作 文件上传下载 文件上传类的设计 文件系统概述 概述 PHP文件系统的操作是基于UNIX系统模型,所以有一些文件处理函数无法在windows服 ...
- Perl: hash散列转换为Json报错集, perl.c,v $$Revision: 4.0.1.8 $$Date: 1993/02/05 19:39:30 $
bash-2.03$ ./u_json.pl Can't locate object method "encode" via package "JSON" at ...
- 第4节 hive调优:2、数据倾斜
数据的倾斜: 主要就是合理的控制我们的map个数以及reduce个数 第一个问题:maptask的个数怎么定的???与我们文件的block块相关,默认一个block块就是对应一个maptask 第二个 ...
- kubeadm1.14.1 安装Metrics Server
Metrics API 介绍Metrics-Server之前,必须要提一下Metrics API的概念 Metrics API相比于之前的监控采集方式(hepaster)是一种新的思路,官方希望核心指 ...
- Java开发中常见的异常问题
要调试程序,自然需要对程序中的常见的异常有一定的了解,因此在这里我将一些常见的Java程序中的异常列举出来给大家参考 AD: 作为一名开发者,Java程序员,很自然必须熟悉对程序的调试方法.而要调试程 ...
- [LOJ] 分块九题 2
https://loj.ac/problem/6278 区间修改,查询区间第k大. 块内有序(另存),块内二分. 还是用vector吧,数组拷贝排序,下标搞不来.. //Stay foolish,st ...
- 8. EVENTS
8. EVENTS EVENTS表提供有关事件管理器事件的信息,这将在"使用事件调度程序"中讨论. EVENTS表有以下列: - EVENT_CATALOG:事件所属目录的名称.这 ...
- CSS--基础块级元素与内联元素
在CSS中,html中的标签元素大体被分为三种不同的类型:块状元素.内联元素(又叫行内元素)和内联块状元素.在HTML和XHTML中,块级元素不能继承自行内元素(即不能嵌套在行内元素),<p&g ...
- 条款8:别让异常逃离析构函数(prevent exception from leaving destructors)
NOTE: 1.析构函数绝对不要吐出异常.如果一个被析构函数调用的函数可能抛出异常,析构函数应该扑捉任何异常,然后吞下他们(不传播)或结束程序. 2.如果客户需要对某个操作函数运行期间抛出的异常做出反 ...
- Could not find conduit initiator for address:xxxxxxxxx and transport: http://schemas.xmlsoap.org/soap/http
<properties> <cxf.version>3.1.12</cxf.version> </properties> <dependencie ...