POJ1679 The Unique MST
| Time Limit: 1000MS | Memory Limit: 10000K | |
| Total Submissions: 26782 | Accepted: 9598 |
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
first line contains a single integer t (1 <= t <= 20), the number
of test cases. Each case represents a graph. It begins with a line
containing two integers n and m (1 <= n <= 100), the number of
nodes and edges. Each of the following m lines contains a 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!
Source
先跑一遍最小生成树,把树上边都记录下来。
然后枚举不使用其中一条边而再跑最小生成树,若答案没变,说明最小生成树不止一条。
注意数组大小←至少有十道题死在这个问题上了
用n估算的话5000最保险,实际上3000可AC
#include<iostream>
#include<cstdio>
#include<cstring>
#include<algorithm>
#include<cmath>
using namespace std;
const int mxn=;
int n,m;
struct edge{
int x,y;
int v;
}e[mxn];
int tot;
int mst[mxn],cnt;
int cmp(const edge a,const edge b){
return a.v<b.v;
}
int fa[mxn];
void init(int x){
for(int i=;i<=x;i++)fa[i]=i;return;
}
int find(int x){
if(fa[x]==x)return x;
return fa[x]=find(fa[x]);
}
void Kruskal(){ init(n);
int i,j;
cnt=;
int ans1=;
tot=;
for(i=;i<=m;i++){
int x=find(e[i].x);int y=find(e[i].y);
if(x!=y){
fa[x]=y;
ans1+=e[i].v;
mst[++cnt]=i;
tot++;
}
if(tot==n-)break;
}
//
int ans2;
for(int k=;k<=cnt;k++){
tot=; ans2=;
init(n);
//init
for(i=;i<=m;i++){
if(i==mst[k])continue;
int x=find(e[i].x);int y=find(e[i].y);
if(x!=y){
fa[x]=y;
ans2+=e[i].v;
// mst[++cnt]=i;//!!这步不能加! 偷懒从上面复制的结果就是WA记录喜+1
tot++;
}
if((tot==n-) && ans1==ans2){
printf("Not Unique!\n");
return;
}
}
}
printf("%d\n",ans1);
return;
}
int main(){
int T;
scanf("%d",&T);
while(T--){
scanf("%d%d",&n,&m);
for(int i=;i<=m;i++)
scanf("%d%d%d",&e[i].x,&e[i].y,&e[i].v);
sort(e+,e+m+,cmp);
Kruscal();
}
return ;
}
POJ1679 The Unique MST的更多相关文章
- POJ1679 The Unique MST[次小生成树]
The Unique MST Time Limit: 1000MS Memory Limit: 10000K Total Submissions: 28673 Accepted: 10239 ...
- [poj1679]The Unique MST(最小生成树)
The Unique MST Time Limit: 1000MS Memory Limit: 10000K Total Submissions: 28207 Accepted: 10073 ...
- POJ1679 The Unique MST 【次小生成树】
The Unique MST Time Limit: 1000MS Memory Limit: 10000K Total Submissions: 20421 Accepted: 7183 D ...
- POJ1679 The Unique MST 2017-04-15 23:34 29人阅读 评论(0) 收藏
The Unique MST Time Limit: 1000MS Memory Limit: 10000K Total Submissions: 29902 Accepted: 10697 ...
- POJ1679 The Unique MST(Kruskal)(最小生成树的唯一性)
The Unique MST Time Limit: 1000MS Memory Limit: 10000K Total Submissions: 27141 Accepted: 9712 D ...
- POJ1679 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 Description Given a connected undirec ...
- poj1679 The Unique MST(判定次小生成树)
The Unique MST Time Limit: 1000MS Memory Limit: 10000K Total Submissions: 23180 Accepted: 8235 D ...
- POJ-1679.The Unique MST.(Prim求次小生成树)
The Unique MST Time Limit: 1000MS Memory Limit: 10000K Total Submissions: 39561 Accepted: 14444 ...
- POJ1679 The Unique MST(次小生成树)
可以依次枚举MST上的各条边并删去再求最小生成树,如果结果和第一次求的一样,那就是最小生成树不唯一. 用prim算法,时间复杂度O(n^3). #include<cstdio> #incl ...
随机推荐
- C语言实例解析精粹学习笔记——26
实例26:阿拉伯数字转换为罗马数字,将一个整数n(1~9999)转换为罗马数字,其中数字和罗马数字的对应关系如下: 原书中的开发环境很老,我也没有花心思去研究.自己在codeblocks中进行开发的, ...
- dategrip破解
https://blog.csdn.net/weixin_39428938/article/details/81078806
- python Re库的介绍
re库的贪婪匹配和最小匹配 后面跟着?变为最小匹配
- MySQL CONCAT()与GROUP_CONCAT()的使用
1 . MySQL CONCAT(str1,str2, ...) --返回连接的字符串 mysql> SELECT CONCAT('My', 'S', 'QL'); -> 'MySQ ...
- Android Studio的Log日志调试
本人菜鸟一枚,极大发挥了搜索的功能.现记录一番,以备后患. 用断点真的很烦,因为之前写linux的时候,就是用最蠢但是也是挺有帮助的printf()来进行调试. 其实用Log输出日志的原理也是差不多的 ...
- cocos2d-x 3.0 导演,场景,层,精灵
导演(Director) 一款游戏好比一部电影,只是游戏具有更强的交互性,不过它们的基本原理是一致的.所以在Cocos2dx中把统筹游戏大局的类抽象为导演(Director),Director是整个c ...
- 4525: [Cerc2012]Kingdoms
4525: [Cerc2012]Kingdoms 题意 n个国家,两两之间可能存在欠债或者被欠债的关系,一个国家破产:其支出大于收入.问一个国家能否坚持到最后. 思路 很有意思的一道题. dp[s]表 ...
- echarts 地图的背景色和各省颜色配置以及地图饼图联动
myChart.on(ecConfig.EVENT.MAP_SELECTED, function (param) { var selected = param.selected; var str = ...
- Java线程的两种实现形式
一.创建线程的第一种方式:继承Thread类 class Demo extends Thread{ @Override public void run() { super.run(); for(int ...
- Nuget 异常引用记录
事件描述 Nuget未能将packages.config中的dll成功引入项目中 解决办法 从Nuget中删除对NewtonSoft.Json的引用并重新对NewtonSoft.Json 4.5.0. ...