K - The Unique MST
#include<iostream>
#include<cstdio>
#include<cstring>
#include<algorithm>
using namespace std;
struct nond{
int x,y,z;
}edge[*];
int T,N,M,x,y,z,fa[],num,ans[];
int tot,bns,k,answer=;
int cmp(nond aa,nond bb){
return aa.z<bb.z;
}
int find(int x){
return fa[x]==x?x:fa[x]=find(fa[x]);
}
int main(){
cin>>T;
while(T--){
cin>>N>>M;
tot=;bns=;
answer=;
memset(fa,,sizeof(fa));
memset(ans,,sizeof(ans));
for(int i=;i<=M;i++){
cin>>x>>y>>z;
edge[i].x=x;
edge[i].y=y;
edge[i].z=z;
}
sort(edge+,edge++M,cmp);
for(int i=;i<=N;i++) fa[i]=i;
for(int i=;i<=M;i++){
int dx=find(edge[i].x),dy=find(edge[i].y);
if(dx!=dy){
fa[dx]=dy;
tot++;
ans[tot]=i;
bns+=edge[i].z;
}
if(tot==N-) break;
}
for(int i=;i<=tot;i++){
k=;num=;
for(int j=;j<=N;j++) fa[j]=j;
sort(edge+,edge++M,cmp);
for(int j=;j<=M;j++){
if(j==ans[i]) continue;
int dx=find(edge[j].x),dy=find(edge[j].y);
if(dx!=dy){
fa[dx]=dy;
num++;
k+=edge[j].z;
}
if(num==N-) break;
}
if(num==N-) answer=min(k,answer);
}
if(answer==bns) cout<<"Not Unique!"<<endl;
else cout<<bns<<endl;
}
}
K - The Unique MST的更多相关文章
- K - The Unique MST - poj 1679
题目的意思已经说明了一切,次小生成树... ****************************************************************************** ...
- K - The Unique MST (最小生成树的唯一性)
Given a connected undirected graph, tell if its minimum spanning tree is unique. Definition 1 (Spann ...
- The Unique MST (判断是否存在多个最小生成树)
The Unique MST Time Limit: 10 ...
- POJ 1679 The Unique MST (次小生成树kruskal算法)
The Unique MST 时间限制: 10 Sec 内存限制: 128 MB提交: 25 解决: 10[提交][状态][讨论版] 题目描述 Given a connected undirect ...
- POJ1679:The Unique MST(最小生成树)
The Unique MST Time Limit: 1000MS Memory Limit: 10000K Total Submissions: 38430 Accepted: 14045 ...
- POJ 1679 The Unique MST 【最小生成树/次小生成树模板】
The Unique MST Time Limit: 1000MS Memory Limit: 10000K Total Submissions: 22668 Accepted: 8038 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.(Prim求次小生成树)
The Unique MST Time Limit: 1000MS Memory Limit: 10000K Total Submissions: 39561 Accepted: 14444 ...
- POJ - 1679_The Unique MST
The Unique MST Time Limit: 1000MS Memory Limit: 10000K Description Given a connected undirected grap ...
随机推荐
- notifyDataSetChanged()刷新ListView(使用JSONArray绑定的Adapter)
1.fragment代码: package com.ts.fragment; import java.util.ArrayList; import java.util.HashMap; import ...
- [SPOJ VLATTICE]Visible Lattice Points 数论 莫比乌斯反演
7001. Visible Lattice Points Problem code: VLATTICE Consider a N*N*N lattice. One corner is at (0,0, ...
- 三星抛出万亿投资计划 布局四大科技创新领域(5G、人工智能、汽车半导体、生物技术四大新兴产业)
三星近日抛出震惊世人的投资计划,未来三年将在全球范围新增投资180万亿韩元(约1万亿元人民币).新增员工4万名.这是韩国单一企业集团大规模的投资计划. 笔者获悉,三星万亿投资计划主要分两大部分,一是在 ...
- luogu3953 逛公园
正解:SPFA+DP 将POJ3463中maxDist(Target)由minDist(Target)+1改为minDist(Target+K)即可.判断0环,需要对每个节点建立下标为maxDist- ...
- vue+element-ui实现前端分页
按照他的文档来写分页 最主要的是 el-table里面展示的数据怎么处理 <el-table :data="AllCommodityList.slice((currentPage-1 ...
- JQuery常用的api[最好是系统地学习一下《锋利的JQuery》]
text http://api.jquery.com/text/ Get the combined text contents of each element in the set of matche ...
- css中!important的用法
{*rule !important}这个css规则当今在网页制作的时候的普及已经非常流行了,以前我对它的理解就停留在‘浏览器是否识别阶段’ 而没有真正去研究过,可是现在发生了变化.众所周知,!impo ...
- [计蒜客] tsy's number 解题报告 (莫比乌斯反演+数论分块)
interlinkage: https://nanti.jisuanke.com/t/38226 description: solution: 显然$\frac{\phi(j^2)}{\phi(j)} ...
- JS中的数据类型及判断数据类型的方法
简单类型(基本类型): number,string,boolean,null,undefined 复杂类型(引用类型):object typeof 只能判断基本数据类型 instanceof 能够判断 ...
- 爬虫之 Requests库的基本使用
引入 Requests 唯一的一个非转基因的 Python HTTP 库,人类可以安全享用. 警告:非专业使用其他 HTTP 库会导致危险的副作用,包括:安全缺陷症.冗余代码症.重新发明轮子症.啃文档 ...