[NOIP2015]運輸計劃

題目鏈接 : CodeVS 4632

題目大意
在一棵 N 節點樹上,有 M 個運輸計劃( 從aibi ) ,N,M300000
問:把哪一條樹邊的權值變爲0,可以使所有運輸計劃的最大距離最小,輸出這個最大距離的最小值。

分析
0. 首先要會LCA和樹上差分。
1. 顯然,這道題要求樹上兩點之間的距離,所以要寫LCA( 我是用數剖寫的LCA ),現在問題就是怎麼選取變爲0的樹邊。
2. 對於一個某一個答案 X ,若 X 可以作爲答案,那麼所有比 X 小的答案可以實現,滿足二分條件,二分答案求解。
3. 對於某一個 X ,把所有距離大於 X 的運輸計劃進行樹上差分。若找到一條邊,滿足 MaxdistvalX 而且所有距離大於 X 運輸計劃都經過這條邊,則返回 true ,否則,返回 false
4. 最後二分出的答案最大值即爲問題的解。

PS. 不知道用數剖LCA合適不合適

上代碼

#include <cstdio> 
#include <cstring>
#include <algorithm>
using namespace std ;

const int N = 3e5 + 5 ;

int n, m, maxn ;

// vector比較慢,可能T,所以選擇手寫鄰接表
int head[ N ], llen ;
struct node_lib {
    int to, wigh, next ;
    inline void add( int a , int b , int c ) {
        to = b, wigh = c ;
        next = head[ a ], head[ a ] = llen ++ ;
    }
} lib[ N << 1 ] ;

// 數剖模板(沒有加線段樹)
int fa[ N ], size[ N ], son[ N ], deep[ N ], dist[ N ] ;
int top[ N ], pass[ N ], plen ; // 注意這個pass數組,下面有用
void dfs1( int a , int b ) {
    fa[ a ] = b, size[ a ] = 1, deep[ a ] = deep[ b ] + 1 ;
    for ( int p = head[ a ] ; p != -1 ; p = lib[ p ].next ) {
        int node = lib[ p ].to, cost = lib[ p ].wigh ;
        if ( node == b )    continue ;
        dist[ node ] = dist[ a ] + cost ;
        dfs1( node , a ), size[ a ] += size[ node ] ;
        if ( size[ node ] > size[ son[ a ] ] )  son[ a ] = node ;
    }
}
void dfs2( int a , int b ) {
    top[ a ] = b, pass[ ++ plen ] = a ;
    if ( son[ a ] ) dfs2( son[ a ] , b ) ;
    for ( int p = head[ a ] ; p != -1 ; p = lib[ p ].next ) {
        int node = lib[ p ].to ;
        if ( node == fa[ a ] || node == son[ a ] )  continue ;
        dfs2( node , node ) ;
    }
}

// 數剖LCA
int calc_lca( int a , int b ) {
    int f1 = top[ a ], f2 = top[ b ] ;
    while ( f1 != f2 ) {
        if ( deep[ f1 ] < deep[ f2 ] )  
            swap( f1 , f2 ), swap( a , b ) ;
        a = fa[ f1 ], f1 = top[ a ] ;
    }
    return deep[ a ] < deep[ b ] ? a : b ;
}

struct node_tran { // 記錄所有的計劃
    int st, to, lca ;
    int wigh ;
} tran[ N ] ;
void init() {
    int a, b, c ;
    llen = 0 ;
    memset( head , 0xff , sizeof( head ) ) ;
    scanf( "%d %d", &n, &m ) ;
    for ( int i = 1 ; i < n ; i ++ ) {
        scanf( "%d %d %d", &a, &b, &c ) ;
        lib[ llen ].add( a , b , c ) ;
        lib[ llen ].add( b , a , c ) ;
    }
    dfs1( 1 , 0 ) ;
    dfs2( 1 , 1 ) ;
    for ( int i = 1 ; i <= m ; i ++ ) { // LCA求樹上距離
        scanf( "%d %d", &a, &b ), c = calc_lca( a , b ) ;
        tran[ i ].st = a, tran[ i ].to = b, tran[ i ].lca = c ;
        tran[ i ].wigh = dist[ a ] + dist[ b ] - ( dist[ c ] << 1 ) ;
        maxn = max( maxn , tran[ i ].wigh ) ;
    }
}

int cover[ N ] ;
bool judge( int a ) {
    int num = 0 ;
    memset( cover , 0 , sizeof( cover ) ) ;
    for ( int i = 1 ; i <= m ; i ++ )
        if ( tran[ i ].wigh > a ) { // 把所有的大與a的計劃進行樹上差分
            cover[ tran[ i ].st ] ++, cover[ tran[ i ].to ] ++ ;
            cover[ tran[ i ].lca ] -= 2, num ++ ;
        }
    for ( int i = n ; i >= 1 ; i -- ) // 按照pass數組(dfs遍歷)的順序更新,從葉子到根
        cover[ fa[ pass[ i ] ] ] += cover[ pass[ i ] ] ;
    for ( int i = 1 ; i <= n ; i ++ )
        if ( cover[ i ] == num && dist[ i ] - dist[ fa[ i ] ] >= maxn - a ) return true ;
    return false ;
}
int figure() {
    int l = 0, r = maxn ;
    int mid = 0, ans = 0 ;
    while ( l <= r ) { // 二分求解
        mid = ( l + r ) >> 1 ;
        if ( judge( mid ) )
            r = mid - 1, ans = mid ;
        else
            l = mid + 1 ;
    }
    return ans ;
}
int main() {
    init() ;
    printf( "%d\n", figure() ) ;
    return 0 ;
}

以上

發佈了33 篇原創文章 · 獲贊 2 · 訪問量 1萬+
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章