#include <bits/stdc++.h>
#define pb push_back
using namespace std;
typedef long long int ll;
const int maxn = 1e5+10;
vector < int > grafo[maxn];
int n, v, vet[maxn];
ll soma[maxn], dp[maxn][110], dp2[maxn][110], resp;
void dfs(int u, int pai)
{
for(int i = 0 ; i < grafo[u].size() ; i++)
{
int k = grafo[u][i];
if(k == pai) continue;
dfs(k, u);
for(int j = 1 ; j <= v ; j++)
{
ll val = max(dp[k][j-1] + soma[k] - vet[u], dp[k][j]);
if(dp[u][j] < val)
{
dp2[u][j] = dp[u][j];
dp[u][j] = val;
}
else dp2[u][j] = max(dp2[u][j], val);
}
}
}
void rotate(int u, int pai)
{
for(auto k : grafo[u])
{
ll d1[110][2], d2[110][2];
if(k == pai) continue;
for(int i = 1 ; i <= v ; i++)
{
d1[i][0] = dp[u][i];
d2[i][0] = dp[k][i];
d1[i][1] = dp2[u][i];
d2[i][1] = dp2[k][i];
if(dp[u][i] == dp[k][i] or dp[u][i] == dp[k][i-1] + soma[k] - vet[u]) dp[u][i] = dp2[u][i];
}
for(int j = 1 ; j <= v ; j++) dp[k][j] = 0;
for(auto x : grafo[k])
{
for(int j = 1 ; j <= v ; j++)
{
ll val = max(dp[x][j-1] + soma[x] - vet[k], dp[x][j]);
if(dp[k][j] < val)
{
dp2[k][j] = dp[k][j];
dp[k][j] = val;
}
else dp2[k][j] = max(dp2[k][j], val);
}
}
dp[0][v] = max(dp[k][v-1] + soma[k], dp[k][v]);
resp = max(resp, dp[0][v]);
//cout << u << ' ' << k << ' ' << dp[k][v] << endl;
rotate(k, u);
for(int i = 1 ; i <= v ; i++)
{
dp[u][i] = d1[i][0];
dp[k][i] = d2[i][0];
dp2[u][i] = d1[i][1];
dp2[k][i] = d2[i][1];
}
}
}
int main()
{
cin >> n >> v;
for(int i = 1 ; i <= n ; i++) cin >> vet[i];
for(int i = 0 ; i < n-1 ; i++)
{
int a, b;
cin >> a >> b;
grafo[a].pb(b);
grafo[b].pb(a);
}
for(int i = 1 ; i <= n ; i++)
{
for(int j = 0 ; j < grafo[i].size() ; j++)
{
soma[i] += vet[grafo[i][j]];
}
}
grafo[0].pb(1);
dfs(0, 0);
resp = dp[0][v];
//cout << resp << endl;
rotate(1, 0);
cout << resp << endl;
}
Compilation message
chase.cpp: In function 'void dfs(int, int)':
chase.cpp:15:20: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
for(int i = 0 ; i < grafo[u].size() ; i++)
~~^~~~~~~~~~~~~~~~~
chase.cpp: In function 'int main()':
chase.cpp:106:21: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
for(int j = 0 ; j < grafo[i].size() ; j++)
~~^~~~~~~~~~~~~~~~~
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
4 ms |
2680 KB |
Output is correct |
2 |
Correct |
4 ms |
2808 KB |
Output is correct |
3 |
Correct |
4 ms |
2680 KB |
Output is correct |
4 |
Correct |
4 ms |
2680 KB |
Output is correct |
5 |
Incorrect |
4 ms |
2680 KB |
Output isn't correct |
6 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
4 ms |
2680 KB |
Output is correct |
2 |
Correct |
4 ms |
2808 KB |
Output is correct |
3 |
Correct |
4 ms |
2680 KB |
Output is correct |
4 |
Correct |
4 ms |
2680 KB |
Output is correct |
5 |
Incorrect |
4 ms |
2680 KB |
Output isn't correct |
6 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
803 ms |
360176 KB |
Output is correct |
2 |
Correct |
800 ms |
359108 KB |
Output is correct |
3 |
Correct |
476 ms |
181744 KB |
Output is correct |
4 |
Correct |
423 ms |
181504 KB |
Output is correct |
5 |
Correct |
655 ms |
181756 KB |
Output is correct |
6 |
Correct |
645 ms |
181552 KB |
Output is correct |
7 |
Correct |
743 ms |
181620 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
4 ms |
2680 KB |
Output is correct |
2 |
Correct |
4 ms |
2808 KB |
Output is correct |
3 |
Correct |
4 ms |
2680 KB |
Output is correct |
4 |
Correct |
4 ms |
2680 KB |
Output is correct |
5 |
Incorrect |
4 ms |
2680 KB |
Output isn't correct |
6 |
Halted |
0 ms |
0 KB |
- |