并查集优化下-3
来源:互联网 发布:网络医疗 编辑:程序博客网 时间:2024/06/05 18:52
#include<bits/stdc++.h>using namespace std;const int N=10000,M=100000;int f[N+100];int siz[N+100];struct nodes{ int u,v,w;}node[M+100];bool cmp(nodes p,nodes q){ return p.w>q.w;}void init(int n){ for(int i=1;i<=n;i++) f[i]=i; for(int i=1;i<=n;i++) siz[i]=1;}int Find(int x){ if(x==f[x]) return x; return Find(f[x]);}int Union(int x,int y){ int tx,ty; tx=Find(x); ty=Find(y); if(tx==ty) return 0; if(siz[tx]<siz[ty]) {f[tx]=ty;siz[ty]+=tx;} else {f[ty]=tx;siz[tx]+=ty;} return 1;}int main(){ int n,m; scanf("%d%d",&n,&m); for(int i=0;i<m;i++) scanf("%d%d%d",&node[i].u,&node[i].v,&node[i].w); sort(node,node+m,cmp); init(n); int ans=0,pre=-1; for(int i=0;i<m;i++) { int way=Union(node[i].u,node[i].v); if(way&&node[i].w!=pre) { ans++; pre=node[i].w; } } printf("%d\n",ans); return 0;}
国王的烦恼
0 0
- 并查集优化下-3
- 并查集优化
- frindship(优化并查集)
- 并查集及优化
- 并查集的优化
- kruskal-边的贪心(并查集优化)-3
- poj 1456 贪心+并查集优化
- hdu1213+并查集模板(优化)
- 并查集优化之路径压缩
- 并查集的链表优化
- 并查集-一笔画优化
- 并查集及优化(C++)
- bzoj 1098 dfs+并查集优化
- 并查集的优化措施
- 并查集--算法,优化,变种
- Building Block(并查集 优化)
- 并查集的“并优化”(leader合并)和“查优化”(路径压缩)
- 下步计划 - 并查集
- 数据库三大范式
- cpp——复合类型——函数——函数重载
- #undef 的用法及其意义
- POJ 2502 Subway
- 广州 Android 安卓培训一期视频+原课件代码
- 并查集优化下-3
- c# 调用 C++ dll 传入传出 字符串
- 斐波那契数列的编程
- 阿里巴巴2015算法工程师实习生笔试卷
- NOIP 2005 普及组 复赛 apple 淘淘摘苹果
- android 圆形虚线背景
- 建造器模式(builder)
- cpp——复合类型——函数——运算符重载
- TP框架实现发送邮件