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 ...
随机推荐
- chrome ubuntu启动不了
安装好后,点击图标没反应 用命令行运行chrome并显示log:google-chrome --enable-logging=stderr --log-level=4 报错如下: 解决:安装更高版本的 ...
- 在实现栈中原来功能的基础上,获得一个栈中最小值,要求时间复杂度 bigO(1)
思路: 准备两个栈 stackData stackMin package my_basic; import java.util.Stack; public class GetMinStack { St ...
- 关于DTCC数据库技术大会
本次DTCC数据库技术大会是第9届了,这次大会虽然有不少公司的产品推介,总体来说还是有不少干货的. 专场较多,有选择地主要听了大数据实践跟流式计算这块.网易跟滴滴的分享比较不错. 了解到了现在大家是用 ...
- JS 数组间的操作
JS 数组间的操作(交集,并集.差集) 以下是js数组之间常用的操作,如交集,并集.差集等. 迭代 each是一个集合迭代函数,可以将一个函数作为参数和一组可以选的参数.依次将集合的每一个元素和可选参 ...
- NodeJs运行服务器-day01
//读取内置模块http,这个模块开发服务器用的var http =require('http'); var server=http.createServer(function(req,res){ r ...
- PTA|团体程序设计天梯赛-练习题目题解锦集(C/C++)(持续更新中……)
PTA|团体程序设计天梯赛-练习题目题解锦集(持续更新中) 实现语言:C/C++: 欢迎各位看官交流讨论.指导题解错误:或者分享更快的方法!! 题目链接:https://pintia.cn/ ...
- luogu 1968 美元汇率
https://www.luogu.org/problemnew/show/P1968 定义二维数组f[ ][ ],f[i][1]表示在第i天将马克变为美元,f[i][2]表示在第i天将美元变为马克. ...
- luogu 1608 路径统计--最短路计数
https://www.luogu.org/problemnew/show/P1608 题意https://www.cnblogs.com/rmy020718/p/9440588.html相似,建议还 ...
- LInux设备驱动分析—— kmalloc和kzalloc函数
今晚在研究EVM5728开发板上面Linux系统的IIC设备驱动程序,偶然之间看到驱动程序中有一处使用了kzalloc函数,本人之前都是使用Linux内核提供的kmalloc / kfree函数来给设 ...
- 计算机网络之传输层 下(TCP)
1. TCP的特点 特点:它是一个点到点的通信机制,只能有一个发送方和一个接收方:它提供是一个可靠的,按序的字节流机制:使用流水线机制,通过拥塞控制和流量控制的机制设置窗口尺寸:发送方和接收方都有缓存 ...