# | 제출 시각 | 아이디 | 문제 | 언어 | 결과 | 실행 시간 | 메모리 |
---|---|---|---|---|---|---|---|
164412 | 2019-11-20T13:33:29 Z | Lawliet | Chase (CEOI17_chase) | C++14 | 1683 ms | 363112 KB |
#include <bits/stdc++.h> using namespace std; typedef long long int lli; const int MAXK = 110; const int MAXN = 100010; int n, k; int v[MAXN]; int maxDP[MAXN][MAXK][2]; lli ans; lli sum[MAXN]; lli dp[MAXN][MAXK]; vector< int > adj[MAXN]; void DFSCalculate(int cur, int p) { for(int i = 0 ; i < adj[cur].size() ; i++) { int viz = adj[cur][ i ]; if( viz == p ) continue; sum[ cur ] += v[ viz ]; DFSCalculate( viz , cur ); } for(int j = 1 ; j <= k ; j++) { for(int i = 0 ; i < adj[cur].size() ; i++) { int viz = adj[cur][ i ]; if( viz == p ) continue; if( dp[viz][j] > dp[ maxDP[cur][j][1] ][ j ] ) maxDP[ cur ][ j ][1] = viz; if( dp[ maxDP[cur][j][1] ][ j ] > dp[ maxDP[cur][j][0] ][ j ]) swap( maxDP[ cur ][ j ][0] , maxDP[ cur ][ j ][1] ); } dp[ cur ][ j ] = dp[ maxDP[cur][j - 1][0] ][ j - 1 ] + sum[ cur ]; dp[ cur ][ j ] = max( dp[ cur ][ j ] , dp[ maxDP[cur][j][0] ][ j ] ); } } void DFSRotate(int cur, int p) { ans = max( ans , dp[cur][k] ); for(int i = 0 ; i < adj[cur].size() ; i++) { int viz = adj[cur][ i ]; if( viz == p ) continue; vector< lli > oldDPCur; vector< lli > oldDPViz; vector< int > oldMaxDPCur[2]; vector< int > oldMaxDPViz[2]; lli oldSumCur = sum[ cur ]; lli oldSumViz = sum[ viz ]; for(int j = 0 ; j <= k ; j++) { oldDPCur.push_back( dp[cur][j] ); oldDPViz.push_back( dp[viz][j] ); for(int p = 0 ; p < 2 ; p++) { oldMaxDPCur[p].push_back( maxDP[cur][j][p] ); oldMaxDPViz[p].push_back( maxDP[viz][j][p] ); } } sum[ cur ] -= v[ viz ]; sum[ viz ] += v[ cur ]; for(int j = 1 ; j <= k ; j++) { lli newDPCur = 0; if( maxDP[ cur ][ j - 1 ][0] == viz ) newDPCur = dp[ maxDP[cur][j - 1][1] ][ j - 1 ] + sum[ cur ]; else newDPCur = dp[ maxDP[cur][j - 1][0] ][ j - 1 ] + sum[ cur ]; if( maxDP[ cur ][ j ][0] == viz ) newDPCur = max( newDPCur , dp[ maxDP[cur][j][1] ][ j ] ); else newDPCur = max( newDPCur , dp[ maxDP[cur][j][0] ][ j ] ); dp[ cur ][ j ] = newDPCur; } for(int j = 1 ; j <= k ; j++) { if( dp[cur][j] > dp[ maxDP[viz][j][1] ][ j ] ) maxDP[ viz ][ j ][1] = cur; if( dp[ maxDP[viz][j][1] ][ j ] > dp[ maxDP[viz][j][0] ][ j ]) swap( maxDP[ viz ][ j ][0] , maxDP[ viz ][ j ][1] ); dp[ viz ][ j ] = max( dp[ viz ][ j ] , dp[ maxDP[viz][j][0] ][ j ] ); dp[ viz ][ j ] = max( dp[ viz ][ j ] , dp[ maxDP[viz][j - 1][0] ][ j - 1 ] + sum[ viz ]); } DFSRotate( viz , cur ); sum[ cur ] = oldSumCur; sum[ viz ] = oldSumViz; for(int j = 0 ; j <= k ; j++) { dp[cur][j] = oldDPCur[j]; dp[viz][j] = oldDPViz[j]; for(int p = 0 ; p < 2 ; p++) { maxDP[cur][j][p] = oldMaxDPCur[p][j]; maxDP[viz][j][p] = oldMaxDPViz[p][j]; } } } } int main() { scanf("%d %d",&n,&k); for(int i = 1 ; i <= n ; i++) scanf("%d",&v[i]); for(int i = 1 ; i < n ; i++) { int U, V; scanf("%d %d",&U,&V); adj[ U ].push_back( V ); adj[ V ].push_back( U ); } DFSCalculate( 1 , 0 ); DFSRotate( 1 , 0 ); printf("%lld\n",ans); }
Compilation message
# | 결과 | 실행 시간 | 메모리 | Grader output |
---|---|---|---|---|
1 | Correct | 4 ms | 2680 KB | Output is correct |
2 | Correct | 4 ms | 2680 KB | Output is correct |
3 | Correct | 9 ms | 2708 KB | Output is correct |
4 | Correct | 4 ms | 2680 KB | Output is correct |
5 | Correct | 5 ms | 2728 KB | Output is correct |
6 | Correct | 4 ms | 2680 KB | Output is correct |
# | 결과 | 실행 시간 | 메모리 | Grader output |
---|---|---|---|---|
1 | Correct | 4 ms | 2680 KB | Output is correct |
2 | Correct | 4 ms | 2680 KB | Output is correct |
3 | Correct | 9 ms | 2708 KB | Output is correct |
4 | Correct | 4 ms | 2680 KB | Output is correct |
5 | Correct | 5 ms | 2728 KB | Output is correct |
6 | Correct | 4 ms | 2680 KB | Output is correct |
7 | Correct | 28 ms | 6520 KB | Output is correct |
8 | Correct | 8 ms | 4856 KB | Output is correct |
9 | Correct | 8 ms | 4472 KB | Output is correct |
10 | Correct | 15 ms | 4472 KB | Output is correct |
11 | Correct | 10 ms | 4472 KB | Output is correct |
12 | Correct | 9 ms | 4472 KB | Output is correct |
# | 결과 | 실행 시간 | 메모리 | Grader output |
---|---|---|---|---|
1 | Correct | 1555 ms | 359480 KB | Output is correct |
2 | Correct | 1513 ms | 358660 KB | Output is correct |
3 | Correct | 1149 ms | 180264 KB | Output is correct |
4 | Correct | 418 ms | 180332 KB | Output is correct |
5 | Correct | 1291 ms | 180572 KB | Output is correct |
6 | Correct | 1264 ms | 180472 KB | Output is correct |
7 | Correct | 1229 ms | 180732 KB | Output is correct |
# | 결과 | 실행 시간 | 메모리 | Grader output |
---|---|---|---|---|
1 | Correct | 4 ms | 2680 KB | Output is correct |
2 | Correct | 4 ms | 2680 KB | Output is correct |
3 | Correct | 9 ms | 2708 KB | Output is correct |
4 | Correct | 4 ms | 2680 KB | Output is correct |
5 | Correct | 5 ms | 2728 KB | Output is correct |
6 | Correct | 4 ms | 2680 KB | Output is correct |
7 | Correct | 28 ms | 6520 KB | Output is correct |
8 | Correct | 8 ms | 4856 KB | Output is correct |
9 | Correct | 8 ms | 4472 KB | Output is correct |
10 | Correct | 15 ms | 4472 KB | Output is correct |
11 | Correct | 10 ms | 4472 KB | Output is correct |
12 | Correct | 9 ms | 4472 KB | Output is correct |
13 | Correct | 1555 ms | 359480 KB | Output is correct |
14 | Correct | 1513 ms | 358660 KB | Output is correct |
15 | Correct | 1149 ms | 180264 KB | Output is correct |
16 | Correct | 418 ms | 180332 KB | Output is correct |
17 | Correct | 1291 ms | 180572 KB | Output is correct |
18 | Correct | 1264 ms | 180472 KB | Output is correct |
19 | Correct | 1229 ms | 180732 KB | Output is correct |
20 | Correct | 1233 ms | 181472 KB | Output is correct |
21 | Correct | 406 ms | 181496 KB | Output is correct |
22 | Correct | 1330 ms | 181664 KB | Output is correct |
23 | Correct | 422 ms | 181532 KB | Output is correct |
24 | Correct | 1229 ms | 181724 KB | Output is correct |
25 | Correct | 1102 ms | 181588 KB | Output is correct |
26 | Correct | 1683 ms | 363064 KB | Output is correct |
27 | Correct | 1543 ms | 363112 KB | Output is correct |