2012年10月17日 星期三

11747 - Heavy Cycle Edges


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 3
0 1 1
1 2 2
2 0 3
4 5
0 1 1
1 2 2
2 3 3
3 1 4
0 2 0
3 1
0 1 1
0 0

Sample Output

3
2 4
forest

Zachary Friggstad
not
Used with an auxiliary verb or “be” to form the negative
Tip: Didn't want this definition pop-up? Try setting a trigger key in Extension Options.


minimum spanning tree - kruskal
#include<stdio.h>
#include<cstring>
#include<vector>
#include<algorithm>
#include<queue>
#define REP(i, b, n) for (int i = b; i < n; i++)
#define rep(i, n) REP(i, 0, n)
#define DBG 0
#define MAXM 25000
#define MAXN 1000
using namespace std;

int N,M,e;
int v1[MAXM],v2[MAXM],w[MAXM],p[MAXN];
struct Node{
Node(int a,int b,int c):v1(a),v2(b),dis(c){}
bool operator <(const Node b)const {return dis>b.dis;}
int v1,v2,dis;
};
void addedge(int a,int b,int c){
v1[e]=a,v2[e]=b,w[e]=c,e++;
}
int find(int v){
if(p[v]==v)return v;
return p[v]=find(p[v]);
}
void unit(int from,int to){
p[find(from)]=p[find(to)];
}
void ans(){
int a,b,c,d=0,sum=0;
vector<int>res;
priority_queue<Node>q;
rep(i,e)q.push(Node(v1[i],v2[i],w[i]));
rep(i,N)p[i]=i;
while(q.size()){
a=q.top().v1,b=q.top().v2,c=q.top().dis,q.pop();
if(DBG)printf("a b %d %d\n",a,b);
if(find(a)!=find(b))unit(a,b),sum+=c,d++;
else res.push_back(c);
}
sort(res.begin(),res.end());
bool ll=0;
if(res.size())rep(i,res.size()){if(ll)printf(" ");ll=1;printf("%d",res[i]);}
else printf("forest");
printf("\n");
}
int main(){
int a,b,c;
while(scanf("%d%d",&N,&M)==2){
e=0;
if(N==0&&M==0)break;
rep(i,M)scanf("%d%d%d",&a,&b,&c),addedge(a,b,c);
ans();
}
return 0;
}

沒有留言:

張貼留言