Synchronization State (tree structure) of [ZJOI2007] - explanations

Synchronization State (tree structure) of [ZJOI2007] - explanations

Why write DP? , It is not just being water problem


Face questions

Description
small Q learning soldered on an electronic circuit board technology training courses. A circuit board by a number of elements, we might call a node, and its digital 1,2,3 .... For reference. Each node is connected to the circuit board by a plurality of disjoint conductor, and for any two nodes of the circuit board, and there exist only one path (path means for connecting two conductors sequence).

In the presence of a particular element on a circuit board called a "trigger." When operating excitation, generate an excitation current, passed through a wire connected to each of the nodes it. And the intermediate node after receiving the excitation current, obtain information, and the energization to spread it and connected to the node has not received the exciting current. Finally, intense current will reach some "terminal node" - node after receiving the excitation current is no longer forwarded.

Propagating the excitation current conductor can be time consuming, and E for each edge, by the time it takes the excitation current is T
, and the receiving node forwards the excitation current may be considered to be instantaneous. The board now requires each "termination node" while the obtained excitation circuit - i.e. the synchronization holding state. Since the structure does not comply with current temporal synchronization requirements, it needs by changing the configuration of the connecting line. Currently there is a small prop QQ, once the prop, so that the excitation current can be increased by one unit time through a connection strip conductor. Q Will the small synchronous state how many times the minimum use of props available so that all the "end node" when?

The INPUT
root of a tree +

Output

An integer representing the lowest number of

in.1
3
1
1 2 1
1 3 3

out.1
2

Data range and Conventions

Data for 100%, n <= 1e5, t <= 1e6

Thinking

The main idea

Since the boundary value can not be reduced by, we consider the bottom-up processing for each node i, i fa node connected to the non-constant as long sides, or in any case can not be satisfied. As long as we find max value of their son for each node, and update other son, the difference between the sum of the update is the answer.

Details:
Note dis open long long
to continue with dis [] updated, not only the right side of this point are attached w [I], because the son node to a leaf node corresponding distance is not the same

error code

void  dfs( int u , int fa ){
    ll sum = 0LL , ma = 0LL , cnt = 0LL ;
    for( int i = head[ u ] ; i ; i = nex[ i ] ){
        if( to[ i ] == fa )continue ;
        dfs( to[ i ] , u ) ;
        sum += (ll)w[ i ] , ma = max( (ll)w[ i ] , ma ) , cnt++ ;
    }
    ans += ma*cnt - sum ;
}

Correct wording

void  dfs( int u , int fa ){
    ll sum = 0LL , ma = 0LL ;
    for( int i = head[ u ] ; i ; i = nex[ i ] ){
        if( to[ i ] == fa )continue ;
        dfs( to[ i ] , u ) ;
        ma = max( dis[ to[i] ] + w[ i ] , ma ) ;//统计最大值
    }
    dis[ u ] = ma ; //更新dis[ u ]
    for( int i = head[ u ] ; i ; i = nex[ i ] ){
        if( to[ i ] == fa )continue ;
        ans += dis[ u ] - w[ i ] - dis[ to[ i ] ] ;  //统计贡献
    }
}

The complete code

#include<bits/stdc++.h>
using namespace std ;
#define ll long long
const int MAXN = 500005 ;
inline int read(){
    int s=0 ; char g=getchar() ; while(g>'9'||g<'0')g=getchar() ; 
    while(g>='0'&&g<='9')s=s*10+g-'0',g=getchar() ; return s ;
}
int N , root , to[ MAXN*2 ] , nex[ MAXN*2 ] , head[ MAXN ] , w[ MAXN*2 ] , tot = 1;
ll ans = 0LL , dis[ MAXN ];
void add( int x , int y , int z){
    to[ ++tot ] = y , nex[ tot ] = head[ x ] , w[ tot ] = z , head[ x ] = tot ;
} 
void  dfs( int u , int fa ){
    ll sum = 0LL , ma = 0LL ;
    for( int i = head[ u ] ; i ; i = nex[ i ] ){
        if( to[ i ] == fa )continue ;
        dfs( to[ i ] , u ) ;
        ma = max( dis[ to[i] ] + w[ i ] , ma ) ;
    }
    dis[ u ] = ma ; 
    for( int i = head[ u ] ; i ; i = nex[ i ] ){
        if( to[ i ] == fa )continue ;
        ans += dis[ u ] - w[ i ] - dis[ to[ i ] ] ;  
    }
}
int main(){
    N = read() , root = read() ; int m1 , m2 , m3 ;
    for( int i = 1 ;  i < N ; ++i ){
        m1 = read() , m2 = read() , m3 = read() ;
        add( m1 , m2 , m3 ) , add( m2 , m1 , m3 ) ;
    }
    dfs( root , root ) ;
    cout<<ans ;
    return 0 ;
}

If insufficient, please indicate Gangster

Guess you like

Origin www.cnblogs.com/ssw02/p/11431951.html