답안 #29790

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
29790 2017-07-20T17:36:35 Z samir_droubi Election Campaign (JOI15_election_campaign) C++14
100 / 100
396 ms 32828 KB
#include <bits/stdc++.h>
using namespace std;
int n,m;
const int mxn=(1e5)+5;

vector<int>tr[mxn];
int p[mxn][20];
int dep[mxn];
int T;
int in[mxn];
int out[mxn];
void dfs(int v,int pa)
{
    ++T;
    in[v] = T;
    for(int i = 0 ; i < tr[v].size() ; ++i )
    {
        int u = tr[v][i];
        if(u == pa)continue;
        
        dep[u] = dep[v] + 1;
        p[u][0] = v;

        dfs( u, v);
    }
    out[v] = T;
}
int mxlog = 0;
void init()
{
    while( ( 1<<mxlog ) <= n ) ++mxlog;
    for(int j = 1 ; j < mxlog ; ++j)
        for(int i = 1; i <= n ; ++i ) 
            p[ i ][ j ] = p[ p[ i ][ j - 1 ] ][ j - 1 ];
}
int lca(int x,int y)
{
    if( dep[x] > dep[y] ) swap( x, y);
    int dif = dep[y] - dep[x];
    for(int i = 0 ; i < mxlog ; ++i )
    {
        if( ( 1<<i ) & dif )
            y=p[y][i];
    }
    if( x == y )return x;
    for(int i = mxlog - 1 ; i >= 0 ; --i )
        if( p[x][i] != p[y][i] )
        {
            x = p[x][i];
            y = p[y][i];
        }
    return p[x][0];
}

int lz[mxn*4];
int sum[mxn];
void pros(int p,int l,int r)
{
    if(l!=r)
    {
        lz[ p * 2 ] += lz[p];
        lz[ p * 2 + 1 ] += lz[p];
    }
    else sum[l] += lz[ p ];
    lz[ p ]=0;
}
void update(int p,int l,int r,int i,int j,int x)
{
    pros( p , l , r );
    if( r < i || l > j)return;
    if( l >= i && r <= j)
    {
        lz[p] += x;
        pros( p , l , r );
        return ;
    }
    int md = ( l + r ) / 2;
    update( p * 2 , l , md , i , j , x );
    update( p * 2 + 1 , md + 1 , r , i ,j , x );

}
void get(int p,int l,int r,int i)
{
    pros( p , l , r );
    if( r < i || l > i )return;
    if( l == r ) return;
    int md = ( l + r ) / 2;
    if( i <= md )get( p * 2 , l , md , i);
    else get( p * 2 + 1 , md + 1 , r , i);
}

int dp[mxn];
int dpsm[mxn];
int ans=0;
vector< pair< int , pair< int , int > > >q[mxn];
void dfs1(int v,int pa)
{
    int sm=0;
    for(int i = 0 ; i < tr[v].size() ; ++i )
    {
        int u = tr[v][i];
        if( u == pa )continue;

        dfs1(u,v);
        sm += dp[u];

        dp[v] = max( dp[v] , dp[u] );
    }
    dpsm[v] = sm;
    
    for(int i = 0 ; i < tr[v].size() ; ++i )
    {
        int u = tr[v][i];
        if( u == pa )continue;
        update( 1 , 1 , n , in[u] , out[u] , sm - dp[u] );
    }

    for(int i = 0 ; i < q[v].size() ; ++i)
    {
        int c = q[v][i].first;
        int a = q[v][i].second.first;
        int b = q[v][i].second.second;
        
        int vl = 0;
        if( a == v)
        {

            get( 1 , 1 , n , in[b] );

            vl += sum[ in[b] ] + dpsm[ b ];
            
        }
        else if ( b == v)
        {

            get( 1 , 1 , n , in[a] );

            vl += sum[ in[a] ] + dpsm[ a ];
            
        }
        else
        {

            get( 1 , 1 , n , in[b] );

            vl += sum[ in[b] ] ;

            get( 1 , 1 , n , in[a] );

            vl += sum[ in[a] ] ;

            vl += dpsm[a] + dpsm[b];

            vl -= sm;
        }
        dp[v] = max( dp[v] , vl + c ); 
    }
    dp[v] = max( dp[v] , dpsm[v] );
    ans = max( ans , dp[v] );
}
int main()
{
    scanf("%d",&n);
    for(int i = 0 ; i < n - 1 ; ++i ) 
    {
        int x , y ;
        scanf("%d%d",&x,&y);
        tr[x].push_back(y);
        tr[y].push_back(x);
    }

    dfs(1,1);
    init();

    scanf("%d",&m);
    for(int i = 0; i < m ; ++i )
    {
        int x,y,c;
        scanf("%d%d%d",&x,&y,&c);
        int node = lca( x , y );
        q[node].push_back( { c , { x , y } } ); 
    }

    dfs1(1,1);
    printf("%d\n",ans);

    return 0;
}

Compilation message

election_campaign.cpp: In function 'void dfs(int, int)':
election_campaign.cpp:16:23: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
     for(int i = 0 ; i < tr[v].size() ; ++i )
                       ^
election_campaign.cpp: In function 'void dfs1(int, int)':
election_campaign.cpp:99:23: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
     for(int i = 0 ; i < tr[v].size() ; ++i )
                       ^
election_campaign.cpp:111:23: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
     for(int i = 0 ; i < tr[v].size() ; ++i )
                       ^
election_campaign.cpp:118:23: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
     for(int i = 0 ; i < q[v].size() ; ++i)
                       ^
election_campaign.cpp: In function 'int main()':
election_campaign.cpp:163:19: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
     scanf("%d",&n);
                   ^
election_campaign.cpp:167:28: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
         scanf("%d%d",&x,&y);
                            ^
election_campaign.cpp:175:19: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
     scanf("%d",&m);
                   ^
election_campaign.cpp:179:33: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
         scanf("%d%d%d",&x,&y,&c);
                                 ^
# 결과 실행 시간 메모리 Grader output
1 Correct 0 ms 18428 KB Output is correct
2 Correct 0 ms 18428 KB Output is correct
3 Correct 3 ms 18428 KB Output is correct
4 Correct 0 ms 18428 KB Output is correct
5 Correct 176 ms 21728 KB Output is correct
6 Correct 66 ms 30844 KB Output is correct
7 Correct 206 ms 27604 KB Output is correct
8 Correct 113 ms 22248 KB Output is correct
9 Correct 159 ms 25748 KB Output is correct
10 Correct 119 ms 22188 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 3 ms 18428 KB Output is correct
2 Correct 0 ms 18428 KB Output is correct
3 Correct 0 ms 18428 KB Output is correct
4 Correct 209 ms 32824 KB Output is correct
5 Correct 186 ms 32828 KB Output is correct
6 Correct 193 ms 32824 KB Output is correct
7 Correct 219 ms 32828 KB Output is correct
8 Correct 199 ms 32828 KB Output is correct
9 Correct 183 ms 32828 KB Output is correct
10 Correct 216 ms 32828 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 3 ms 18428 KB Output is correct
2 Correct 0 ms 18428 KB Output is correct
3 Correct 0 ms 18428 KB Output is correct
4 Correct 209 ms 32824 KB Output is correct
5 Correct 186 ms 32828 KB Output is correct
6 Correct 193 ms 32824 KB Output is correct
7 Correct 219 ms 32828 KB Output is correct
8 Correct 199 ms 32828 KB Output is correct
9 Correct 183 ms 32828 KB Output is correct
10 Correct 216 ms 32828 KB Output is correct
11 Correct 16 ms 18956 KB Output is correct
12 Correct 236 ms 32824 KB Output is correct
13 Correct 213 ms 32824 KB Output is correct
14 Correct 193 ms 32824 KB Output is correct
15 Correct 246 ms 32824 KB Output is correct
16 Correct 213 ms 32828 KB Output is correct
17 Correct 196 ms 32824 KB Output is correct
18 Correct 233 ms 32824 KB Output is correct
19 Correct 203 ms 32828 KB Output is correct
20 Correct 236 ms 32824 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 299 ms 23468 KB Output is correct
2 Correct 193 ms 32824 KB Output is correct
3 Correct 379 ms 29232 KB Output is correct
4 Correct 216 ms 24188 KB Output is correct
5 Correct 326 ms 28660 KB Output is correct
6 Correct 216 ms 24524 KB Output is correct
7 Correct 353 ms 28284 KB Output is correct
8 Correct 259 ms 23444 KB Output is correct
9 Correct 183 ms 32824 KB Output is correct
10 Correct 396 ms 27380 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 0 ms 18428 KB Output is correct
2 Correct 0 ms 18428 KB Output is correct
3 Correct 3 ms 18428 KB Output is correct
4 Correct 0 ms 18428 KB Output is correct
5 Correct 176 ms 21728 KB Output is correct
6 Correct 66 ms 30844 KB Output is correct
7 Correct 206 ms 27604 KB Output is correct
8 Correct 113 ms 22248 KB Output is correct
9 Correct 159 ms 25748 KB Output is correct
10 Correct 119 ms 22188 KB Output is correct
11 Correct 0 ms 18560 KB Output is correct
12 Correct 0 ms 18560 KB Output is correct
13 Correct 0 ms 18560 KB Output is correct
14 Correct 0 ms 18560 KB Output is correct
15 Correct 3 ms 18560 KB Output is correct
16 Correct 0 ms 18560 KB Output is correct
17 Correct 3 ms 18564 KB Output is correct
18 Correct 6 ms 18560 KB Output is correct
19 Correct 0 ms 18560 KB Output is correct
20 Correct 0 ms 18560 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 0 ms 18428 KB Output is correct
2 Correct 0 ms 18428 KB Output is correct
3 Correct 3 ms 18428 KB Output is correct
4 Correct 0 ms 18428 KB Output is correct
5 Correct 176 ms 21728 KB Output is correct
6 Correct 66 ms 30844 KB Output is correct
7 Correct 206 ms 27604 KB Output is correct
8 Correct 113 ms 22248 KB Output is correct
9 Correct 159 ms 25748 KB Output is correct
10 Correct 119 ms 22188 KB Output is correct
11 Correct 3 ms 18428 KB Output is correct
12 Correct 0 ms 18428 KB Output is correct
13 Correct 0 ms 18428 KB Output is correct
14 Correct 209 ms 32824 KB Output is correct
15 Correct 186 ms 32828 KB Output is correct
16 Correct 193 ms 32824 KB Output is correct
17 Correct 219 ms 32828 KB Output is correct
18 Correct 199 ms 32828 KB Output is correct
19 Correct 183 ms 32828 KB Output is correct
20 Correct 216 ms 32828 KB Output is correct
21 Correct 16 ms 18956 KB Output is correct
22 Correct 236 ms 32824 KB Output is correct
23 Correct 213 ms 32824 KB Output is correct
24 Correct 193 ms 32824 KB Output is correct
25 Correct 246 ms 32824 KB Output is correct
26 Correct 213 ms 32828 KB Output is correct
27 Correct 196 ms 32824 KB Output is correct
28 Correct 233 ms 32824 KB Output is correct
29 Correct 203 ms 32828 KB Output is correct
30 Correct 236 ms 32824 KB Output is correct
31 Correct 299 ms 23468 KB Output is correct
32 Correct 193 ms 32824 KB Output is correct
33 Correct 379 ms 29232 KB Output is correct
34 Correct 216 ms 24188 KB Output is correct
35 Correct 326 ms 28660 KB Output is correct
36 Correct 216 ms 24524 KB Output is correct
37 Correct 353 ms 28284 KB Output is correct
38 Correct 259 ms 23444 KB Output is correct
39 Correct 183 ms 32824 KB Output is correct
40 Correct 396 ms 27380 KB Output is correct
41 Correct 0 ms 18560 KB Output is correct
42 Correct 0 ms 18560 KB Output is correct
43 Correct 0 ms 18560 KB Output is correct
44 Correct 0 ms 18560 KB Output is correct
45 Correct 3 ms 18560 KB Output is correct
46 Correct 0 ms 18560 KB Output is correct
47 Correct 3 ms 18564 KB Output is correct
48 Correct 6 ms 18560 KB Output is correct
49 Correct 0 ms 18560 KB Output is correct
50 Correct 0 ms 18560 KB Output is correct
51 Correct 289 ms 23464 KB Output is correct
52 Correct 243 ms 32824 KB Output is correct
53 Correct 376 ms 27536 KB Output is correct
54 Correct 209 ms 23992 KB Output is correct
55 Correct 309 ms 23696 KB Output is correct
56 Correct 229 ms 32824 KB Output is correct
57 Correct 333 ms 28072 KB Output is correct
58 Correct 159 ms 24020 KB Output is correct
59 Correct 316 ms 23528 KB Output is correct
60 Correct 236 ms 32828 KB Output is correct
61 Correct 313 ms 28220 KB Output is correct
62 Correct 219 ms 24616 KB Output is correct
63 Correct 296 ms 23768 KB Output is correct
64 Correct 196 ms 32824 KB Output is correct
65 Correct 373 ms 28400 KB Output is correct
66 Correct 226 ms 24088 KB Output is correct
67 Correct 306 ms 24200 KB Output is correct
68 Correct 229 ms 32824 KB Output is correct
69 Correct 323 ms 26576 KB Output is correct
70 Correct 246 ms 24288 KB Output is correct