SSL2817 2017年11月1日提高组 树论(并查集)
来源:互联网 发布:新网域名查询 编辑:程序博客网 时间:2024/04/20 01:19
2017年11月1日提高组 树论
Description
Input
Sample Input
1
4 2
3 1 3375
3 2 8707
5 5
2 1 455
3 1 3616
4 1 9494
5 4 1448
5 1 8366
Sample Output
-1
7911
Hint
分析:按边权从小到大排序,枚举最小边,往后把边加入并查集,如果n个点都属于同一集合时就统计答案
代码
#include <cstdio>#include <algorithm>#define N 5000using namespace std;struct arr{ int x,y,w;}a[N];int f[N],n,m;int find(int x){ if (f[x]==x) return x; return f[x]=find(f[x]);}int min(int x,int y){ return x<y?x:y;}int so(arr p,arr q){ return p.w<q.w;}int main(){// freopen("graph.in","r",stdin);// freopen("graph.out","w",stdout); int T; scanf("%d",&T); while (T--) { scanf("%d%d",&n,&m); for (int i=1;i<=m;i++) scanf("%d%d%d",&a[i].x,&a[i].y,&a[i].w); sort(a+1,a+m+1,so); int ans=123456789; for (int i=1;i<=m;i++) { int tot=0; for (int j=1;j<=n;j++) f[j]=j; int j=i; while (j<=m) { int u=find(a[j].x); int v=find(a[j].y); if (u!=v) { tot++; f[u]=v; if (tot==n-1) break; } j++; } if (tot==n-1) ans=min(ans,a[j].w-a[i].w); } if (ans==123456789) ans=-1; printf("%d\n",ans); } fclose(stdin); fclose(stdout);}
阅读全文
0 0
- SSL2817 2017年11月1日提高组 树论(并查集)
- SSL2687 2017年8月14日提高组T1 跳跃(kruskal,并查集)
- SSL2785 2017年10月23日提高组T1 询问 (二分+并查集)
- SSL2859 2017年12月9日提高组 矩阵变换(并查集)
- 2017年11月1日提高组T2 树论
- 10月12日 并查集(Cube Stacking)
- 10月13日 并查集(生物链)
- 2017年11月1日提高组 第k小数(二分+分块)
- 2017年8月11日提高组T3 难题
- 2017年11月2日提高组T1 Sequence
- 2017年11月4日提高组T1 背包
- 2017年11月4日提高组T3 序列
- 2017年11月1日提高组T1 第k小数
- 2017年11月1日提高组T3 极大极小值差
- SSL2681 2017年8月11日提高组T3 难题(math)
- SSL2828 2017年11月2日提高组T1 Sequence(kmp)
- SSL2831 2017年11月3日提高组T1 跃动(dp)
- SSL2832 2017年11月3日提高组T1 生日宴会(二分)
- Springcloud学习笔记(二)
- Oracle 查看表空间的大小及使用情况sql语句
- Golang基本工具的安装以及cobra的安装
- 逻辑覆盖
- CCF CSP 2016年9月第3题 炉石传说(模拟)
- SSL2817 2017年11月1日提高组 树论(并查集)
- 搜索条(在表格中)实现搜索功能
- python3.X 和python2.X的几点小区别
- 父元素高度自适应,子元素浮动,引起的高度塌陷问题
- 清除浮动
- C Primer Plus 第六版 所有章节课后编程练习答案
- Ubuntu 16.04 ssh 登陆出现错误Algorithm Negotiation failed when trying to connect to server 解决
- gulp JS文件合并和压缩
- 常用命令:su / sudo