BZOJ-2152: 聪聪可可(点分治)

题目:http://www.lydsy.com/JudgeOnline/problem.php?id=2152

树的点分治,搞个数组存mod 3=0,1,2的数的个数,然后直接搞就可以了。。

代码:

#include <cstdio>
#include <algorithm>
#include <cstring>
 
using namespace std ;
 
#define travel( v ) for ( edge *p = head[ v ] ; p ; p = p -> next )
#define AddEdge( s , t , d ) Add( s , t , d ) , Add( t , s , d )
#define check( ch ) ( ch >= '0' && ch <= '9' )
 
const int inf = 0x7fffffff ;
const int maxn = 20010 ;
 
void getint( int &t ) {
    int ch ; for ( ch = getchar(  ) ; ! check( ch ) ; ch = getchar(  ) ) ;
    t = ch - '0' ;
    for ( ch = getchar(  ) ; check( ch ) ; ch = getchar(  ) ) {
        t *= 10 ;
        t += ch - '0' ;
    }
}
 
int o[ 20 ] ;
 
void putint( int t ) {
    if ( t ) {
        o[ 0 ] = 0 ;
        for ( ; t ; t /= 10 ) o[ ++ o[ 0 ] ] = t % 10 ;
        while ( o[ 0 ] -- ) putchar( '0' + o[ o[ 0 ] + 1 ] ) ;
    } else putchar( '0' ) ;
}
 
struct edge {
    edge *next ;
    int t , d ;
} *head[ maxn ] ;
 
void Add( int s , int t , int d ) {
    edge *p = new( edge ) ;
    p -> t = t , p -> d = d , p -> next = head[ s ] ;
    head[ s ] = p ;
}
 
int gcd( int x , int y ) {
    if ( x < y ) swap( x , y ) ;
    for ( ; y ; ) {
        int k = y ; 
        y = x % y ;
        x = k ;
    }
    return x ;
}
 
int n , ans = 0 , cnt[ 3 ] ;
bool del[ maxn ] ;
 
int rt , roof , size[ maxn ] , depth[ maxn ] , rec ;
 
void dfs0( int v , int u ) {
    size[ v ] = 1 ;
    travel( v ) if ( p -> t != u && del[ p -> t ] ) {
        dfs0( p -> t , v ) ;
        size[ v ] += size[ p -> t ] ;
    }
}
 
void chose( int v , int u ) {
    int ret = size[ rt ] - size[ v ] ;
    travel( v ) if ( p -> t != u && del[ p -> t ] ) {
        ret = max( ret , size[ p -> t ] ) ;
        chose( p -> t , v ) ;
    }
    if ( rec > ret ) {
        rec = ret , roof = v ;
    }
}
 
void dfs1( int v , int u ) {
    travel( v ) if ( p -> t != u && del[ p -> t ] ) {
        depth[ p -> t ] = depth[ v ] + p -> d ;
        dfs1( p -> t , v ) ;
    }
}
 
int b[ maxn ] , bn ;
 
void dfs2( int v , int u ) {
    b[ ++ bn ] = v ;
    travel( v ) if ( p -> t != u && del[ p -> t ] ) dfs2( p -> t , v ) ;
}
 
void divide( int v ) {
    dfs0( v , 0 ) ;
    rt = v , roof = 0 ;
    rec = inf ;
    chose( v , 0 ) ;
    depth[ roof ] = 0 ;
    dfs1( roof , 0 ) ;
    memset( cnt , 0 , sizeof( cnt ) ) ;
    cnt[ 0 ] ++ , ++ ans ;
    travel( roof ) if ( del[ p -> t ] ) {
        bn = 0 ;
        dfs2( p -> t , roof ) ;
        for ( int i = 0 ; i ++ < bn ; ) {
            int t = depth[ b[ i ] ] % 3 ;
            ans += ( cnt[ ( 3 - t ) % 3 ] * 2 ) ;
        }
        for ( int i = 0 ; i ++ < bn ; ) cnt[ depth[ b[ i ] ] % 3 ] ++ ;
    }
    del[ roof ] = false ;
    travel( roof ) if ( del[ p -> t ] ) divide( p -> t ) ;
}
 
int main(  ) {
    getint( n ) ;
    for ( int i = 1 ; i < n ; ++ i ) {
        int s , t , d ; getint( s ) , getint( t ) , getint( d ) ;
        AddEdge( s , t , d ) ;
    }
    memset( del , true , sizeof( del ) ) ;
    divide( 1 ) ;
    int ret = gcd( ans , n * n ) ;
    putint( ans / ret ) ;
    putchar( '/' ) ;
    putint( ( n * n ) / ret ) ;
    return 0 ;
}
    原文作者:AmadeusChan
    原文地址: https://www.jianshu.com/p/c0df69147f10
    本文转自网络文章,转载此文章仅为分享知识,如有侵权,请联系博主进行删除。
点赞