生成树,发现自己写了好多生
#include <stdio.h>
#include <stdlib.h>
int city[505];
void make ( int n ) {
for ( int i=0; i<n; i++ ) { city[i] = -1; } }
int find ( int a ) {
if ( city[a] < 0 ) { return a; } int root = find ( city[a] ); city[a] = root;
return root; }
void un ( int a, int b ) {
int ra = find ( a ); int rb = find ( b );
if ( city[ra] < city[rb] ) { city[ra] += city[rb]; city[rb] = ra; } else { city[rb] += city[ra]; city[ra] = rb; } }
void init ( int n ) { for ( int i=0; i<n; i++ ) { city[i] = 0; } }
typedef struct { int b; int e; int len; }type; type seg[200000];
int cmp ( const void *a, const void *b ) { return ( ( type * )a )->len - ( ( type * )b )->len; }
int main () { int t; int n; int m; int l; while ( scanf ( "%d", &t ) != EOF ) { while ( t -- ) { scanf ( "%d", &n ); m = 0; for ( int i=0; i<n; i++ ) { for ( int j=0; j<n; j++ ) { scanf ( "%d", &l ); if ( j > i ) { seg[m].b = i; seg[m].e = j; seg[m].len = l; m ++; } } } qsort ( seg, m, sizeof ( type ), cmp ); int max = -1; make ( n ); for ( i=0; i<m; i++ ) { if ( find ( seg[i].b ) != find ( seg[i].e ) ) { if ( max < seg[i].len ) { max = seg[i].len; } un ( seg[i].b, seg[i].e ); } } printf ( "%d\n", max ); } } return 0; }
成树啊
|
|
| 日 | 一 | 二 | 三 | 四 | 五 | 六 |
---|
30 | 31 | 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | 10 | 11 | 12 | 13 | 14 | 15 | 16 | 17 | 18 | 19 | 20 | 21 | 22 | 23 | 24 | 25 | 26 | 27 | 28 | 29 | 30 | 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | 10 |
|
公告
决定从线程开始!!
常用链接
留言簿(6)
随笔档案
搜索
最新评论
阅读排行榜
评论排行榜
|
|