深谈并查集
我们理解并查集这个数据结构的时候不要过于 死板 , 我们要知道 并查集是用来维护关系的,而不是 单纯一味 去归,并,归,并,归,并 以前我就是一味的只知道 归,并,归,并 要深刻理解只有通过做题来打磨 https://www.luogu.org/problem/P2502 吐槽: 这道题把我坑惨了 花了半晚上去做,最后发现我的思路是错的 应该开始看数据范围的时候就该察觉了 说到底还是对并查集理解不够深刻 分析: 先对边进行排序 再n^2^枚举,跑生成树跟新答案 开始枚举的i一定是minn 最后枚举到find(s)==find(t)时j就maxx 不断跟新答案 直到再也无法S与T连通为止 code: #include <cstdio> #include <algorithm> #define maxn 600 #define maxm 5010 using namespace std; int n,m,s,t; int father[maxn]; int ans1,ans2; struct rec { int a,b,len; } c[maxm]; bool cmp(rec a,rec b) {return (a.len<b.len);} int getfather(int x) { if (father[x]==x) return x; return father[x]