【题目大意】

共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. "==" 与 "is"的区别

    Is there a difference between `==` and `is` in Python? "=="是比较内容相当;"is"是比较对象的id是 ...

  2. InnoSetup使用笔记

    今天用InnoSetup做安装包时,因为要装的驱动区分32位.64位,64位系统中要安装32位+64位驱动. 想在脚本中进行判断.折腾一阵,终于搞定: 参考了:http://379910987.blo ...

  3. input光标位置不居中问题

    文本输入框默认在谷歌,火狐浏览器中,光标是居中显示的.但在IE7中一开始会在顶部闪烁(输入文字后光标居中),加上行高就可以,值为文本框的高度. 注意要加*号,否则在谷歌浏览其中光标会在顶部闪烁. *l ...

  4. HDU - 3374:String Problem (最小表示法模板题)

    Give you a string with length N, you can generate N strings by left shifts. For example let consider ...

  5. JAMstack 技术要点

    1.  简要说明 Modern web development architecture based on client-side JavaScript, reusable APIs,and preb ...

  6. 解决Iframe session过期,登录界面无法全页刷新

    在登录界面增加如下js代码: <script language=”JavaScript”> if (window != top) top.location.href = location. ...

  7. linux下修改ip地址

    1.more  /etc/sysconfig/network-scripts/ifcfg-eth0 2.ifconfig eth0 192.168.1.211 netmask 255.255.255. ...

  8. mysql数据库备份与还原(转)

    MySQL备份和还原,都是利用mysqldump.mysql和source命令来完成的. 1.Linux下MySQL的备份与还原 1.1 备份 [root@localhost ~]# cd /var/ ...

  9. 双击 cui

    //改变属性块的双击事件 //将菜单文件中的双击改一下,退出时还原文件 acad.bak.cui 改这个名字 每次用这个更新为新的 acad.cui进行修改 //退出时再用 acad.bak.cui还 ...

  10. install OS from usb

      https://unetbootin.github.io/ https://rufus.akeo.ie/