(beginer) 最小生成树 UVA 11747 Heavy Cycle Edges
来源:互联网 发布:鬼影 电影 知乎 编辑:程序博客网 时间:2024/05/16 04:40
Problem F: Heavy Cycle Edges
Given an undirected graph with edge weights, a minimum spanning tree is a subset of edges of minimum total weight such that any two nodes are connected by some path containing only these edges. A popular algorithm for finding the minimum spanning tree T in a graph proceeds as follows:
- let T be initially empty
- consider the edges e1, ..., em in increasing order of weight
- add ei to T if the endpoints of ei are not connected by a path in T
An alternative algorithm is the following:
- let T be initially the set of all edges
- while there is some cycle C in T
- remove edge e from T where e has the heaviest weight in C
Your task is to implement a function related to this algorithm. Given an undirected graph G with edge weights, your task is to output all edges that are the heaviest edge in some cycle of G.
Input Format
The first input of each case begins with integers n and m with 1 ≤ n ≤ 1,000 and 0 ≤ m ≤ 25,000 where n is the number of nodes and m is the number of edges in the graph. Following this are m lines containing three integers u, v, and w describing a weight w edge connecting nodes u and v where 0 ≤ u, v < n and 0 ≤ w < 231. Input is terminated with a line containing n = m = 0; this case should not be processed. You may assume no two edges have the same weight and no two nodes are directly connected by more than one edge.
Output Format
Output for an input case consists of a single line containing the weights of all edges that are the heaviest edge in some cycle of the input graph. These weights should appear in increasing order and consecutive weights should be separated by a space. If there are no cycles in the graph then output the text forest
instead of numbers.
Sample Input
3 30 1 11 2 22 0 34 50 1 11 2 22 3 33 1 40 2 03 10 1 10 0
Sample Output
32 4forest
#include<iostream>#include<stdio.h>#include<string.h>#include<cstring>#include<algorithm>using namespace std;const int maxn = 1010;const int maxm = 25010;int n , m;int p[maxn];int find(int x){if (x==p[x]) return x;return p[x] = find(p[x]);}struct Edge{Edge() { }Edge(int uu,int vv,int ww):u(uu) , v(vv) , w(ww) { }int u , v , w;}edge[maxm];inline bool operator<(const Edge&e1,const Edge&e2){return e1.w < e2.w;}void input(){for (int i = 0 ; i < m ; ++i){int u , v , w;scanf("%d%d%d",&u,&v,&w);edge[i] = Edge(u,v,w);}sort(edge,edge+m);}void solve(){bool flag = true;for (int i = 0 ; i < n ; ++i) p[i] = i;for (int i = 0 ; i < m ; ++i){int u = find(edge[i].u);int v = find(edge[i].v);if (u==v) {if (flag) printf("%d",edge[i].w);else printf(" %d",edge[i].w);flag = false;continue;}p[u] = v;}if (flag) printf("forest");cout << endl;}int main(){while (scanf("%d%d",&n,&m)==2,n+m){input();solve();}}
- (beginer) 最小生成树 UVA 11747 Heavy Cycle Edges
- UVa 11747 Heavy Cycle Edges(最小生成树)
- uva 11747 - Heavy Cycle Edges(生成树)
- UVa 11747 - Heavy Cycle Edges
- uva 11747 Heavy Cycle Edges
- (beginer) 最小生成树 UVA 544 Heavy Cargo
- UVA 11747 - Heavy Cycle Edges(MST)
- UVA 11747 Heavy Cycle Edges(MST)
- (beginer) 最小生成树 UVA 1395 Slim Span
- UVa 544 Heavy Cargo( 最小生成树的variant)
- (beginer) 最小生成树 UVA 10600 ACM Contest and Blackout
- uva 544 - Heavy Cargo(生成树)
- poj 1797 Heavy Transportation 最小生成树 最大生成树
- POJ1797 Heavy Transportation (最短路径/最小生成树kruskal)
- Heavy Cargo(弗洛伊德——最小生成树)
- POJ 1797 Heavy Transportation(最小生成树或最短路)
- POJ-1797Heavy Transportation (最小生成树问题)
- (beginer) 最小权匹配 UVA 1045 - The Great Wall Game
- (intermediate) 最短路(第k短路) UVA 10740 - Not the Best
- (advanced) 最短路 UVA 12227 Wormholes
- (beginer) 最小生成树 UVA 1395 Slim Span
- CSDN感谢你!
- (beginer) 最小生成树 UVA 10600 ACM Contest and Blackout
- (beginer) 最小生成树 UVA 11747 Heavy Cycle Edges
- (beginer) 最小生成树 UVA 544 Heavy Cargo
- (intermediate) 最小生成树 UVA 10369 - Arctic Network
- (intermediate)最小生成树 UVA 12275 - Sensor network
- (intermediate) 最小生成树 UVA 1504 Genghis Khan the Conqueror
- (advanced) UVA 最小生成树(限制度数) UVA 1537 Picnic Planning
- (beginer) 最小权匹配 UVA 1045 - The Great Wall Game
- (beginer) 二分图(最大独立集) UVA 12168 Cat vs. Dog
- (intermediate) 二分图(边着色) UVA 10615 Rooks