题目大意:n个点的树,i点到j点的路径最大价值是i到j点的路的权值最小值,以一个点为源点,使得从这个点到其他点的总价值最大
具体思路:贪心
先给边排序,按边权从大到小考虑是从哪个联通块做到哪个联通块
AC代码
#include<bits/stdc++.h> #define int long long using namespace std; int n,a[500000],b[500000],fa[500000],sum[500000],ans[500000]; pair<int,int> e[500000]; int ask(int x) { if(fa[x]==x)return x;else return fa[x]=ask(fa[x]); } main() { while(~scanf("%lld",&n)) { for (int i=1;i<n;i++)scanf("%d%d%d",&a[i],&b[i],&e[i].first),e[i].second=i; for (int i=1;i<=n;i++)fa[i]=i,sum[i]=1,ans[i]=0; sort(e+1,e+n); for (int i=n-1;i>=1;i--) { int fx=ask(a[e[i].second]),fy=ask(b[e[i].second]); if(ans[fx]+sum[fy]*e[i].first>ans[fy]+sum[fx]*e[i].first)fa[fy]=fx,sum[fx]+=sum[fy],ans[fx]=ans[fx]+sum[fy]*e[i].first; else fa[fx]=fy,sum[fy]+=sum[fx],ans[fy]=ans[fy]+sum[fx]*e[i].first; } printf("%lld\n",ans[ask(n)]); } return 0; }