题意:

      给你一个图,问你最小树是否唯一,唯一则输出最小数的权值,不唯一输出Not Unique!

思路:

     题目问的是最小树是否唯一,其实也就是在问次小树是否等于最小树,如果等于则不唯一,求次小树快的方法应该是先求最小树,然后枚举删除最小树上的边,最快的应该是树形dp优化的那个吧,刚刚忘记了,直接求出最小树,然后暴力深搜分成两个集合枚举,0ms AC,因为点才100,所以暴力也无压力。


#include<stdio.h>
#include<string.h>
#include<algorithm> #define N_node 100 + 10
#define N_edge 20000 + 100
#define INF 1000000000 using namespace std; typedef struct
{
int to ,next ,cost;
}STAR; typedef struct
{
int a ,b ,c;
}EDGE; STAR E[N_edge];
EDGE edge[N_edge] ,Tree[N_node];
int list[N_node] ,tot;
int mer[N_node];
int mark[N_node];
int map[N_node][N_node];
int node_l[N_node] ,node_r[N_node];
int ll ,rr; void add(int a ,int b)
{
E[++tot].to = b;
E[tot].next = list[a];
list[a] = tot;
E[++tot].to = a;
E[tot].next = list[b];
list[b] = tot;
} bool camp(EDGE a ,EDGE b)
{
return a.c < b.c;
} int finds(int x)
{
return x == mer[x] ? x : mer[x] = finds(mer[x]);
} void DFS1(int s)
{
node_l[++ll] = s;
for(int k = list[s] ;k ;k = E[k].next)
{
int to = E[k].to;
if(mark[to]) continue;
mark[to] = 1;
DFS1(to);
}
} void DFS2(int s)
{
node_r[++rr] = s;
for(int k = list[s] ;k ;k = E[k].next)
{
int to = E[k].to;
if(mark[to]) continue;
mark[to] = 1;
DFS2(to);
}
} int minn(int x ,int y)
{
return x < y ? x : y;
} int main ()
{
int t ,n ,m ,i ,j;
scanf("%d" ,&t);
while(t--)
{
scanf("%d %d" ,&n ,&m);
for(i = 1 ;i <= n ;i ++)
for(j = 1 ;j <= n ;j ++)
map[i][j] = INF;
for(i = 1 ;i <= m ;i ++)
{
scanf("%d %d %d" ,&edge[i].a ,&edge[i].b ,&edge[i].c);
map[edge[i].a][edge[i].b] = map[edge[i].b][edge[i].a] = edge[i].c;
}
sort(edge + 1 ,edge + m + 1 ,camp);
int sum = 0;
for(i = 1 ;i <= n ;i ++) mer[i] = i;
memset(list ,0 ,sizeof(list)) ,tot = 1;
int tt = 0;
for(i = 1 ;i <= m ;i ++)
{
int x = finds(edge[i].a);
int y = finds(edge[i].b);
if(x == y) continue;
mer[x] = y;
sum += edge[i].c;
add(edge[i].a ,edge[i].b);
Tree[++tt].a = edge[i].a;
Tree[tt].b = edge[i].b;
Tree[tt].c= edge[i].c;
}
int now = 1000000000;
for(i = 1 ;i <= tt ;i ++)
{
int a = Tree[i].a;
int b = Tree[i].b;
int c = Tree[i].c;
memset(mark ,0 ,sizeof(mark));
mark[a] = mark[b] = 1;
ll = 0;
DFS1(a);
rr = 0;
DFS2(b);
for(int ii = 1 ;ii <= ll ;ii ++)
for(int jj = 1 ;jj <= rr ;jj ++)
{
int aa = node_l[ii];
int bb = node_r[jj];
if(map[aa][bb] == INF || aa == a && bb == b || aa == b && bb == a) continue;
now = minn(now ,sum - c + map[aa][bb]);
}
}
if(now != sum) printf("%d\n" ,sum);
else printf("Not Unique!\n");
}
return 0;
}



POJ 1679 判断最小树是否唯一的更多相关文章

  1. poj 1679 判断MST是不是唯一的 (次小生成树)

    判断MST是不是唯一的 如果是唯一的 就输出最小的权值和 如果不是唯一的 就输出Not Unique! 次小生成树就是第二小生成树  如果次小生成树的权值和MST相等  那么MST就不是唯一的 法一: ...

  2. poj 1679 判断最小生成树是否唯一

    /* 只需判断等效边和必选边的个数和n-1的关系即可 */ #include<stdio.h> #include<stdlib.h> #define N 110 struct ...

  3. POJ 1679 The Unique MST(判断最小生成树是否唯一)

    题目链接: http://poj.org/problem?id=1679 Description Given a connected undirected graph, tell if its min ...

  4. POJ 1679 The Unique MST 【判断最小生成树是否唯一】

    Description Given a connected undirected graph, tell if its minimum spanning tree is unique.  Defini ...

  5. poj 1679 Prim判断次短路

    题意:判断最短路是否唯一. 思路:先prrim一次求出最短路同时记录最短路加入的边: 然后枚举所求边,将其删除再求n-1次prim,判断再次所求得的最短路与第一次求得的次短路的关系. 代码: #inc ...

  6. poj 1679 The Unique MST 【次小生成树】【模板】

    题目:poj 1679 The Unique MST 题意:给你一颗树,让你求最小生成树和次小生成树值是否相等. 分析:这个题目关键在于求解次小生成树. 方法是,依次枚举不在最小生成树上的边,然后加入 ...

  7. ZOJ - 2587 Unique Attack (判断最小割是否唯一)

    题意:判断最小割是否唯一. 分析:跑出最大流后,在残余网上从源点和汇点分别dfs一次,对访问的点都打上标记. 若还有点没有被访问到,说明最小割不唯一. https://www.cnblogs.com/ ...

  8. POJ 1679 The Unique MST (次小生成树 判断最小生成树是否唯一)

    题目链接 Description Given a connected undirected graph, tell if its minimum spanning tree is unique. De ...

  9. poj 1679 The Unique MST 判断最小生成树是否唯一(图论)

    借用的是Kruskal的并查集,算法中的一点添加和改动. 通过判定其中有多少条可选的边,然后跟最小生成树所需边做比较,可选的边多于所选边,那么肯定方案不唯一. 如果不知道这个最小生成树的算法,还是先去 ...

随机推荐

  1. 微信小程序3D轮播图

    <!-- 轮播图 --> <swiper previous-margin='50px' next-margin='50px' bindchange="swiperChang ...

  2. WPF 基础 - 图片与 base64

    1. base64 转图片 将 base64 转成 byte[] 将 byte[] 作为内存流保存到一个 BitmapImage 实例的流的源 把 BitmapImage 作为目标图片的 Source ...

  3. WPF 应用 - 使用 Properties.Settings 保存客户端密码

    1. 先在项目的 Settings.settings 新建需要的字段和类型 有需要还可设置初始默认值 2. 启动客户端时,获取 Properties.Settings 的属性值 public void ...

  4. 源码解析之 Mybatis 对 Integer 参数做了什么手脚?

    title: 源码解析之 Mybatis 对 Integer 参数做了什么手脚? date: 2021-03-11 updated: 2021-03-11 categories: Mybatis 源码 ...

  5. Redis之数据类型和持久化及高可用

    数据类型 Redis支持五种数据类型:string(字符串),hash(哈希),list(列表),set(集合)及zset(sorted set:有序集合). String(字符串) String是r ...

  6. 一个名叫Sentinel-Rules-SDK的组件,使得Sentinel的流控&熔断规则的配置更加方便

    原文链接:一个名叫Sentinel-Rules-SDK的组件,使得Sentinel的流控&熔断规则的配置更加方便 1 Sentinel 是什么? 随着微服务的流行,服务和服务之间的稳定性变得越 ...

  7. Redis入门到放弃系列-redis安装

    Redis是什么? Redis is an open source (BSD licensed), in-memory data structure store, used as a database ...

  8. python中gzip模块的使用

    gzip模块能够直接压缩和解压缩bytes-like类型的数据,同时也能实现对应格式文件的压缩与解压缩 一.数据压缩与解压缩 压缩 函数-gzip.compress(data, compresslev ...

  9. Android Studio 如何运行单个activity

    •写在前面 调试界面运行单个 Activity 可节省编译整个项目的时间提高效率: 本着提高效率的角度,特地上网百度相关知识: •解决方法 首先,在 AndroidManifest.xml 文件中,找 ...

  10. ECharts绘制折线图

    首先看实现好的页面 实现 首先引入echarts工具 // vue文件中引入echarts工具 let echarts = require('echarts/lib/echarts') require ...