题目链接:merge
set练习题
#include <stdio.h>
#include <string.h>
#include <set>
#include <algorithm>
using namespace std ;
#define clr( a , x ) memset ( a , x , sizeof a )
const int MAXN = 300005 ;
set < int > s[MAXN] ;
set < int > :: iterator it ;
int p[MAXN] ;
int n , m ;
int find ( int x ) {
int o = x , tmp ;
while ( o != p[o] ) o = p[o] ;
while ( x != p[x] ) {
tmp = p[x] ;
p[x] = o ;
x = tmp ;
}
return o ;
}
void solve () {
int x , y ;
scanf ( "%d" , &n ) ;
for ( int i = 1 ; i <= n ; ++ i ) {
p[i] = i ;
s[i].clear () ;
s[i].insert ( i << 1 ) ;
}
for ( int i = 1 ; i < n ; ++ i ) {
scanf ( "%d%d" , &x , &y ) ;
x = find ( x ) ;
y = find ( y ) ;
if ( s[x].size () > s[y].size () ) swap ( x , y ) ;
p[x] = y ;
for ( it = s[x].begin () ; it != s[x].end () ; ++ it ) {
s[y].insert ( *it ) ;
}
int l = *( s[y].begin () ) ;
int r = *( --s[y].end () ) ;
int m = ( l + r ) >> 1 ;
it = s[y].upper_bound ( m ) ;
int b = *it ;
int a = *( -- it ) ;
int t = ( a + b ) / 2 ;
int ans = max ( t - l , r - t ) ;
if ( it != s[y].begin () ) {
-- it ;
int z = *it ;
++ it ;
t = ( a + z ) / 2 ;
ans = min ( ans , max ( t - l , r - t ) ) ;
}
++ it ;
++ it ;
if ( it != s[y].end () ) {
int z = *it ;
t = ( b + z ) / 2 ;
ans = min ( ans , max ( t - l , r - t ) ) ;
}
printf ( "%.1f\n" , ans / 2.0 ) ;
}
}
int main () {
int T ;
scanf ( "%d" , &T ) ;
for ( int i = 1 ; i <= T ; ++ i ) {
solve () ;
}
return 0 ;
}