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

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程资源 > 编程问答 >内容正文

编程问答

P2619 [国家集训队2]Tree I

發布時間:2023/12/2 编程问答 27 豆豆
生活随笔 收集整理的這篇文章主要介紹了 P2619 [国家集训队2]Tree I 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

P2619 [國家集訓隊2]Tree I

鏈接

?

分析:

  為了確定白邊選入的數量,所以給白邊加一個權值,二分這個值,然后最小生成樹。可以發現白邊的數量雖這個值的增大而減小,滿足單調性。

  有一個問題:如果在二分過程中給白邊加上mid,白邊數比need多,加mid+1,白邊數need少。即存在很多相等的白邊 和 很多相等的黑邊。

  如果白邊大于need一定是不合法的解,之后當加到一個數,使這些白邊和黑邊相等時,就可以隨意的替換了,也使解變得合法了。所以二分的過程ans是可以增大的,所以56行要是ans-k*x不能是ans-cntwhite*x

?

上述情況的樣例:

4 5 2 0 1 3 0 0 1 4 1 1 2 3 0 1 2 3 1 2 3 3 0

?

代碼:

1 #include<cstdio> 2 #include<algorithm> 3 #include<cstring> 4 #include<cmath> 5 #include<iostream> 6 #include<cctype> 7 8 using namespace std; 9 10 const int N = 50100; 11 12 struct Edge{ 13 int u,v,w,c; 14 bool operator < (const Edge &a) const { 15 if (w == a.w) return c < a.c; 16 return w < a.w; 17 } 18 }e[200100]; 19 int fa[N]; 20 int n,m,k,Ans = 0; 21 22 inline char nc() { 23 static char buf[100000],*p1 = buf,*p2 = buf; 24 return p1==p2&&(p2=(p1=buf)+fread(buf,1,100000,stdin),p1==p2)?EOF:*p1++; 25 } 26 inline int read() { 27 int x = 0,f = 1;char ch = nc(); 28 for (; !isdigit(ch); ch=nc()) if(ch=='-') f=-1; 29 for (; isdigit(ch); ch=nc()) x=x*10+ch-'0'; 30 return x * f; 31 } 32 int find(int x) { 33 if (x == fa[x]) return x; 34 return fa[x] = find(fa[x]); 35 } 36 bool check(int x) { 37 for (int i=1; i<=m; ++i) { 38 if (e[i].c == 0) e[i].w += x; 39 } 40 sort(e+1,e+m+1); 41 for (int i=1; i<=n; ++i) fa[i] = i; 42 int cnt = 0,cntwhite = 0,ans = 0; 43 for (int i=1; i<=m; ++i) { 44 int fu = find(e[i].u),fv = find(e[i].v); 45 if (fu == fv) continue; 46 fa[fv] = fu; 47 ans += e[i].w; 48 cnt ++; 49 if (e[i].c == 0) cntwhite ++; 50 if (cnt == n-1) break; 51 } 52 for (int i=1; i<=m; ++i) { 53 if (e[i].c == 0) e[i].w -= x; 54 } 55 if (cntwhite < k) return false; 56 ans -= x * k; 57 Ans = ans; 58 return true; 59 } 60 int main () { 61 n = read(),m = read(),k = read(); 62 for (int i=1; i<=m; ++i) { 63 e[i].u = read() + 1,e[i].v = read() + 1,e[i].w = read(),e[i].c = read(); 64 } 65 int L = -101,R = 101; 66 while (L <= R) { 67 int mid = (L + R) / 2; 68 if (check(mid)) L = mid + 1; 69 else R = mid - 1; 70 } 71 cout << Ans; 72 return 0; 73 }

?

轉載于:https://www.cnblogs.com/mjtcn/p/9096349.html

總結

以上是生活随笔為你收集整理的P2619 [国家集训队2]Tree I的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。