【HDU】5604 merge【set练习题】

3 篇文章 0 订阅

题目链接: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 ;
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值