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

歡迎訪問 生活随笔!

生活随笔

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

编程问答

POJ1679 Luogu4180 次小生成树

發(fā)布時間:2024/10/6 编程问答 49 豆豆
生活随笔 收集整理的這篇文章主要介紹了 POJ1679 Luogu4180 次小生成树 小編覺得挺不錯的,現(xiàn)在分享給大家,幫大家做個參考.

The Unique MST
Given a connected undirected graph, tell if its minimum spanning tree is unique.

Definition 1 (Spanning Tree): Consider a connected, undirected graph G = (V, E). A spanning tree of G is a subgraph of G, say T = (V’, E’), with the following properties:

  • V’ = V.
  • T is connected and acyclic.
  • Definition 2 (Minimum Spanning Tree): Consider an edge-weighted, connected, undirected graph G = (V, E). The minimum spanning tree T = (V, E’) of G is the spanning tree that has the smallest total cost. The total cost of T means the sum of the weights on all the edges in E’.
    Input
    The first line contains a single integer t (1 <= t <= 20), the number of test cases. Each case represents a graph. It begins with a line containing two integers n and m (1 <= n <= 100), the number of nodes and edges. Each of the following m lines contains a triple (xi, yi, wi), indicating that xi and yi are connected by an edge with weight = wi. For any two nodes, there is at most one edge connecting them.
    Output
    For each input, if the MST is unique, print the total cost of it, or otherwise print the string ‘Not Unique!’.
    Sample Input
    2
    3 3
    1 2 1
    2 3 2
    3 1 3
    4 4
    1 2 2
    2 3 2
    3 4 2
    4 1 2
    Sample Output
    3
    Not Unique!
    算法過程:

    先求出最小生成樹,然后枚舉每一條不再最小生成樹上的邊,并把這條邊放到最小生成樹上面,那么我們在這條環(huán)路上取出一條最長的路,處理新加入的那一條邊,最終得到的權值就是最小生成樹的權值

    #include <vector> #include <iostream> #include <cstdio> #include <cstring> #include <algorithm> #define INF 0x3f3f3f3fusing namespace std; int n,m; struct data {int u,v,w;bool vis; } p[20010]; vector<int>G[110]; int per[110],maxd[110][110]; bool cmp(data a,data b) {return a.w < b.w; } int Union_Find(int x) {return x == per[x] ? x: per[x] = Union_Find(per[x]); } void kruskal() {sort(p,p+m,cmp);for(int i=0; i<=n; i++)//初始化{G[i].clear();G[i].push_back(i);per[i]=i;}int sum=0,k=0;//sum是最小生成樹的值for(int i=0; i<m; i++){if(k==n-1) break;//n個點,最多n-1條邊int x1=Union_Find(p[i].u),x2=Union_Find(p[i].v);if(x1!=x2){k++;p[i].vis=1;//這條邊已經(jīng)用過了sum+=p[i].w;int len_x1=G[x1].size();int len_x2=G[x2].size();for(int j=0; j<len_x1; j++)//更新兩點之間距離的最大值for(int k=0; k<len_x2; k++)maxd[G[x1][j]][G[x2][k]]=maxd[G[x2][k]][G[x1][j]]=p[i].w;//因為后面的邊會越來越大,所以這里可以直接等于當前邊的長度per[x1]=x2;int tem[110];for(int j=0; j<len_x2; j++)//現(xiàn)在已經(jīng)屬于一棵樹了,那么我們就將點添加到相應的集合中tem[j]=G[x2][j];for(int j=0; j<len_x1; j++)G[x2].push_back(G[x1][j]);for(int j=0; j<len_x2; j++)G[x1].push_back(tem[j]);}}int cisum=INF;//次小生成樹的權值for(int i=0; i<m; i++)if(!p[i].vis)cisum=min(cisum,sum+p[i].w-maxd[p[i].u][p[i].v]);if(cisum>sum)printf("%d\n",sum);elseprintf("Not Unique!\n"); } int main() {int T;scanf("%d\n",&T);while(T--){scanf("%d%d",&n,&m);for(int i=0; i<m; i++){scanf("%d%d%d",&p[i].u,&p[i].v,&p[i].w);p[i].vis = false;}kruskal();}return 0; }

    總結(jié)

    以上是生活随笔為你收集整理的POJ1679 Luogu4180 次小生成树的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。

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