【题目大意】

共T组数据,对于每组数据,给你一个n个点,m条边的图,设图的最小生成树为MST,次小生成树为ans,若MST=ans,输出Not Unique!,否则输出MST

【题解】

很明确,先求MST再求ans。

关于求次小生成树,我打算写一个总结,先留个坑。

 #include<iostream>
#include<cstdio>
#include<cstdlib>
#include<cstring>
#include<cmath>
#include<ctime>
#include<algorithm>
using namespace std;
const int INF=~0u>>;//正无穷
struct node{int x,y,v;}E[];
struct Node{int y,next,v;}e[];
int T,n,m,len,mst,ans(INF),Link[],f[],vis[],map[][],flag[][],dp[][];
inline int read()
{
int x=,f=; char ch=getchar();
while(!isdigit(ch)) {if(ch=='-') f=-; ch=getchar();}
while(isdigit(ch)) {x=x*+ch-''; ch=getchar();}
return x*f;
}
void insert(int x,int y,int v) {e[++len].next=Link[x];Link[x]=len;e[len].y=y;e[len].v=v;}
int find(int x) {return f[x]==x?x:f[x]=find(f[x]);}
bool cmp(node a,node b) {return a.v<b.v;}
void Kruskal()
{
sort(E+,E+m+,cmp);
for(int i=;i<=m;i++)
{
int x=find(E[i].x),y=find(E[i].y);
if(x!=y)
{
f[x]=y;
mst+=E[i].v;
flag[E[i].x][E[i].y]=flag[E[i].y][E[i].x]=;
}
}
for(int i=;i<=n;i++)
for(int j=i+;j<=n;j++)
if(flag[i][j]) {insert(i,j,map[i][j]); insert(j,i,map[j][i]);}
}
void dfs(int st,int x,int v)
{
vis[x]=;
dp[st][x]=v;
for(int i=Link[x];i;i=e[i].next)
if(!vis[e[i].y]) dfs(st,e[i].y,max(dp[st][x],e[i].v));
}
void pre()
{
memset(Link,,sizeof(Link));
memset(flag,,sizeof(flag));
memset(dp,,sizeof(dp));
len=mst=; ans=INF;
for(int i=;i<=n;i++) f[i]=i;
for(int i=;i<=n;i++)
for(int j=;j<=n;j++)
map[i][j]=(i==j?:INF);
}
int main()
{
freopen("cin.in","r",stdin);
freopen("cout.out","w",stdout);
T=read();
while(T--)
{
n=read(); m=read();
pre();
for(int i=;i<=m;i++)
{
int x=read(),y=read(),v=read();
map[x][y]=map[y][x]=v;
E[i].x=x; E[i].y=y; E[i].v=v;
}
Kruskal();
for(int i=;i<=n;i++) {memset(vis,,sizeof(vis)); dfs(i,i,);}
for(int i=;i<=n;i++)
for(int j=i+;j<=n;j++)
if(!flag[i][j]&&map[i][j]!=INF) ans=min(ans,mst+map[i][j]-dp[i][j]);
if(ans==mst) printf("Not Unique!\n");
else printf("%d\n",mst);
}
return ;
}

【poj1679】The Unique MST的更多相关文章

  1. 【题解】 AT2134 Zigzag MST

    [题解]AT2134 Zigzag MST 一道MST好题 \(Anson\)有云: 要么是减少边的数量. 要么是改变连接边的方式. 那么如何减少边的数量呢?很简单,把所有不可能对答案产生贡献的边去掉 ...

  2. 【LeetCode】95. Unique Binary Search Trees II 解题报告(Python)

    [LeetCode]95. Unique Binary Search Trees II 解题报告(Python) 标签(空格分隔): LeetCode 作者: 负雪明烛 id: fuxuemingzh ...

  3. 【POJ 1679】 The Unique MST

    [题目链接] 点击打开链接 [算法] 先求出图的最小生成树 枚举不在最小生成树上的边,若加入这条边,则形成了一个环,如果在环上且在最小生成树上的权值最大的边等于 这条边的权值,那么,显然最小生成树不唯 ...

  4. 【KM】BZOJ1937 [Shoi2004]Mst 最小生成树

    这道题拖了好久因为懒,结果1A了,惊讶∑( 口 || [题目大意] 给定一张n个顶点m条边的有权无向图.现要修改各边边权,使得给出n-1条边是这张图的最小生成树,代价为变化量的绝对值.求最小代价之和. ...

  5. POJ1679:The Unique MST(最小生成树)

    The Unique MST Time Limit: 1000MS   Memory Limit: 10000K Total Submissions: 38430   Accepted: 14045 ...

  6. 【POJ 1679 The Unique MST】最小生成树

    无向连通图(无重边),判断最小生成树是否唯一,若唯一求边权和. 分析生成树的生成过程,只有一个圈内出现权值相同的边才会出现权值和相等但“异构”的生成树.(并不一定是最小生成树) 分析贪心策略求最小生成 ...

  7. 【POJ 1679】The Unique MST(次小生成树)

    找出最小生成树,同时用Max[i][j]记录i到j的唯一路径上最大边权.然后用不在最小生成树里的边i-j来替换,看看是否差值为0. #include <algorithm> #includ ...

  8. 【LeetCode】95. Unique Binary Search Trees II

    Unique Binary Search Trees II Given n, generate all structurally unique BST's (binary search trees) ...

  9. 【LeetCode】96 - Unique Binary Search Trees

    Given n, how many structurally unique BST's (binary search trees) that store values 1...n? For examp ...

随机推荐

  1. Linux下shell命令 1

    1   [root@hadoop-namenode-1 iebd] cd /filename/filename  跳转至filename文件夹 2   [root@hadoop-namenode-1 ...

  2. SPOJ1812 Longest Common Substring II

    题意 A string is finite sequence of characters over a non-empty finite set Σ. In this problem, Σ is th ...

  3. Servlet、Filter、Listener

    1.Servlet 1.1servlet接口 All Known Implementing Classes:GenericServlet, HttpServlet GenericServlet:与协议 ...

  4. Linux 环境下安装Maven

    1.安装wget命令 如果需要通过使用wget命令,直接通过网络下载maven安装包时,需要在linux系统中安装wget命令. yum -y install wget 2.下载maven安装包 wg ...

  5. idea的有些路径问题

    resource文件在xml的路径 http://www.cnblogs.com/zqr99/p/7642712.html resource文件在io流的读取 this.getClass().getR ...

  6. PUTTY学习

    参考地址:http://blog.csdn.net/eastmount/article/details/52753135 putty介绍: PuTTY是一个Telnet.SSH.rlogin.纯TCP ...

  7. Ubuntu 14.04报“leaking memory”错误

    在做一些实验的时候,临时配置了笔记本网卡eth0的IP地址,结果出现了以下错误, $ sudo ifconfig eth0 192.168.2.100/24 no talloc stackframe  ...

  8. shell中的输入输出和编程中的变量(shell 03)

    shell中的输入输出标准输入:键盘标准输出:终端显示器>> 追加是换行追加的echo -n 不尾随换行符 -e 启用解释反斜杠的转义功能 -E 禁用解释反斜杠的转义功能(默认) --he ...

  9. MongoDB部署实战(一)MongoDB在windows平台分片集群部署

    前言-为什么我要使用mongodb 最近我公司要开发一个日志系统,这个日志系统包括很多类型,错误的,操作的,...用MongoDB存储日志,大量的日志产生,大量读写吞吐量很大的时候,单个Server很 ...

  10. 关于.NET中的Session

    Asp.net 默认配置下,Session莫名丢失的原因及解决办法正常操作情况下Session会无故丢失.因为程序是在不停的被操作,排除Session超时的可能.另外,Session超时时间被设定成6 ...