日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當(dāng)前位置: 首頁 > 编程资源 > 编程问答 >内容正文

编程问答

【并查集】 HDU 4424 Conquer a New Region 贪心

發(fā)布時間:2025/3/20 编程问答 30 豆豆
生活随笔 收集整理的這篇文章主要介紹了 【并查集】 HDU 4424 Conquer a New Region 贪心 小編覺得挺不錯的,現(xiàn)在分享給大家,幫大家做個參考.

題意:給出一棵樹,樹的邊上都有邊權(quán)值,求從一點(diǎn)出發(fā)的權(quán)值和最大,權(quán)值為從一點(diǎn)出去路徑上邊權(quán)的最小值 ?

邊權(quán)從大到小排序后 ?每次合并

取總權(quán)值最大的為父親節(jié)點(diǎn)

#include <cstdio> #include <cstring> #include <cstdlib> #include <string> #include <iostream> #include <algorithm> #include <sstream> #include <cmath> using namespace std; #include <queue> #include <stack> #include <vector> #include <deque> #include <map> #define cler(arr, val) memset(arr, val, sizeof(arr)) typedef long long LL; const int MAXN = 555; const int MAXM = 260110; const int INF = 0x3f3f3f3f; const int mod = 1000000007; struct node {LL a1,b1;LL v; }; struct node a[200010]; LL father[200010],rank[200010]; LL dis[200010];///邊權(quán)和 LL n; bool cmp(node c1,node c2) {return c1.v>c2.v; } void init() {for(LL i=1; i<=n; i++){father[i]=i;dis[i]=0;rank[i]=1;///以i為根的節(jié)點(diǎn)數(shù)} } LL find(LL x) {if(x==father[x])return x;return father[x]=find(father[x]); } void Union(LL x,LL y,LL w) {father[x]=y;rank[y]+=rank[x];dis[y]=w; } int main() { #ifndef ONLINE_JUDGEfreopen("in.txt", "r", stdin);// freopen("out.txt", "w", stdout); #endifLL x1,x2;while(~scanf("%lld",&n)){for(int i=1; i<n; i++)scanf("%lld%lld%lld",&a[i].a1,&a[i].b1,&a[i].v);init();sort(a+1,a+n,cmp);for(int i=1; i<n; i++){LL fa=find(a[i].a1);LL fb=find(a[i].b1);x1=dis[fa]+a[i].v*rank[fb];x2=dis[fb]+a[i].v*rank[fa];if(x1>x2)Union(fb,fa,x1);elseUnion(fa,fb,x2);}printf("%lld\n",dis[find(1)]);}return 0; }

轉(zhuǎn)載于:https://www.cnblogs.com/kewowlo/p/4088296.html

總結(jié)

以上是生活随笔為你收集整理的【并查集】 HDU 4424 Conquer a New Region 贪心的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網(wǎng)站內(nèi)容還不錯,歡迎將生活随笔推薦給好友。