卡了一道题,想今天怎么都得过一道题,就先拿着开刀吧,比较简单的
#include <cstdio>
#include <algorithm>
#include <cstring>
using namespace std;
typedef long long ll;
struct edge {
int u, v;
ll w;
bool is;
}e[25010];
int n, m, fa[1010];
bool cmp( edge a, edge b ) { return a.w < b.w; }
int find ( int x ) { return fa[x] == x ? x : fa[x] = find(fa[x]); }
int main()
{
while ( scanf("%d%d", &n, &m) != EOF && !(!n && !m) ) {
bool f = false;
for ( int i = 0; i < m; e[i].is = 0, ++i ) scanf("%d%d%lld", &e[i].u, &e[i].v, &e[i].w);
sort( e, e + m, cmp );
for ( int i = 0; i < n; ++i ) fa[i] = i;
for ( int i = 0; i < m; ++i ) {
int x = e[i].u, y = e[i].v;
int a = find(x), b = find(y);
if ( a != b ) {
fa[a] = b;
e[i].is = true;
}
}
for ( int i = 0; i < m; ++i ) {
if ( !e[i].is ) {
if ( f ) printf(" ");
f = true;
printf("%lld", e[i].w);
}
}
if ( !f ) printf("forest");
printf("\n");
}
}