国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 學院 > 開發設計 > 正文

The Unique MST POJ - 1679 題解

2019-11-08 02:32:04
字體:
來源:轉載
供稿:網友

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: 1. V’ = V. 2. 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!


給你一張圖,問你能不能構造出兩棵不同的最小生成樹,方法是先構造出一個最小生成樹,然后枚舉該生成樹的某一條邊不能用,再次構造生成樹,比較生成樹權值。

#include<iostream>#include<string.h>#include<stdio.h>#include<string>#include<vector>#include<algorithm>using namespace std;int n,m;struct edge{ int beg, endd, len; edge(){} edge(int a, int b, int c){ beg = a, endd = b, len = c; }}e[5005];bool cmp(edge a,edge b){ return a.len < b.len;}int f[105];int F(int x){ if (f[x] == x)return x; return f[x] = F(f[x]);}vector<int> ansn;int main(){ int x, y,z,t; scanf("%d", &t); while (t--){ ansn.clear(); scanf("%d%d", &n, &m); for (int i = 0; i < m; i++){ scanf("%d%d%d", &x, &y, &z); e[i] = edge(x, y, z); } sort(e, e + m, cmp); int ans = 0; for (int i = 1; i <= n; i++){ f[i] = i; } for (int i = 0; i < m; i++){ int a = e[i].beg, b = e[i].endd, c = e[i].len; if (F(a) != F(b)){ ansn.push_back(i); ans += c; f[F(a)] = F(b); } } bool same = 0; int total = ansn.size(); for (int i = 0; i < total; i++){ int cntans = 0,cntto=0; for (int k = 1; k <= n; k++){ f[k] = k; } for (int j = 0; j < m; j++){ if (j != ansn[i]){ int a = e[j].beg, b = e[j].endd, c = e[j].len; if (F(a) != F(b)){ cntans += c; cntto++; f[F(a)] = F(b); } } } if (cntto == total&&cntans == ans){ same = 1; break; } } if (same){ printf("Not Unique!/n"); } else{ printf("%d/n", ans); } } return 0;}
上一篇:搜索旋轉數組

下一篇:PAT 1082

發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表
主站蜘蛛池模板: 珠海市| 潞城市| 呼和浩特市| 方山县| 古浪县| 湖南省| 阜平县| 兴和县| 东至县| 朝阳市| 大同县| 潼关县| 岳阳县| 平陆县| 五莲县| 凌云县| 泸定县| 惠水县| 芒康县| 开阳县| 大邑县| 东明县| 东乌珠穆沁旗| 抚顺市| 巫山县| 两当县| 虞城县| 赤峰市| 吉林省| 正安县| 揭东县| 班戈县| 旺苍县| 仁化县| 海门市| 翁牛特旗| 巴中市| 元朗区| 云阳县| 依安县| 台江县|