#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(int i = 1 ; i <= v ; i++) resp = max({resp, dp[u][i-1] + soma[u], dp[u][i]});
for(auto k : grafo[u])
{
ll d1[110];
if(k == pai) continue;
for(int i = 1 ; i <= v ; i++)
{
d1[i] = dp[u][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 i = 1 ; i <= v ; i++)
{
ll val = max(dp[u][i-1] + soma[u] - vet[k], dp[u][i]);
if(dp[k][i] < val)
{
dp2[k][i] = dp[k][i];
dp[k][i] = val;
}
else dp2[k][i] = max(dp2[k][i], 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];
}
}
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(1, 0);
//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:92: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 |
2680 KB |
Output is correct |
3 |
Correct |
3 ms |
2680 KB |
Output is correct |
4 |
Correct |
3 ms |
2680 KB |
Output is correct |
5 |
Correct |
3 ms |
2680 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 |
3 ms |
2680 KB |
Output is correct |
4 |
Correct |
3 ms |
2680 KB |
Output is correct |
5 |
Correct |
3 ms |
2680 KB |
Output is correct |
6 |
Correct |
4 ms |
2680 KB |
Output is correct |
7 |
Correct |
8 ms |
4984 KB |
Output is correct |
8 |
Correct |
7 ms |
4984 KB |
Output is correct |
9 |
Correct |
7 ms |
4472 KB |
Output is correct |
10 |
Correct |
8 ms |
4472 KB |
Output is correct |
11 |
Correct |
7 ms |
4472 KB |
Output is correct |
12 |
Correct |
7 ms |
4472 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
586 ms |
228176 KB |
Output is correct |
2 |
Correct |
592 ms |
227940 KB |
Output is correct |
3 |
Correct |
413 ms |
179564 KB |
Output is correct |
4 |
Correct |
411 ms |
179344 KB |
Output is correct |
5 |
Correct |
699 ms |
179348 KB |
Output is correct |
6 |
Correct |
685 ms |
179372 KB |
Output is correct |
7 |
Correct |
706 ms |
179376 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 |
3 ms |
2680 KB |
Output is correct |
4 |
Correct |
3 ms |
2680 KB |
Output is correct |
5 |
Correct |
3 ms |
2680 KB |
Output is correct |
6 |
Correct |
4 ms |
2680 KB |
Output is correct |
7 |
Correct |
8 ms |
4984 KB |
Output is correct |
8 |
Correct |
7 ms |
4984 KB |
Output is correct |
9 |
Correct |
7 ms |
4472 KB |
Output is correct |
10 |
Correct |
8 ms |
4472 KB |
Output is correct |
11 |
Correct |
7 ms |
4472 KB |
Output is correct |
12 |
Correct |
7 ms |
4472 KB |
Output is correct |
13 |
Correct |
586 ms |
228176 KB |
Output is correct |
14 |
Correct |
592 ms |
227940 KB |
Output is correct |
15 |
Correct |
413 ms |
179564 KB |
Output is correct |
16 |
Correct |
411 ms |
179344 KB |
Output is correct |
17 |
Correct |
699 ms |
179348 KB |
Output is correct |
18 |
Correct |
685 ms |
179372 KB |
Output is correct |
19 |
Correct |
706 ms |
179376 KB |
Output is correct |
20 |
Correct |
734 ms |
181500 KB |
Output is correct |
21 |
Correct |
217 ms |
9208 KB |
Output is correct |
22 |
Correct |
806 ms |
181624 KB |
Output is correct |
23 |
Correct |
411 ms |
181496 KB |
Output is correct |
24 |
Correct |
687 ms |
181384 KB |
Output is correct |
25 |
Correct |
396 ms |
181312 KB |
Output is correct |
26 |
Correct |
593 ms |
230648 KB |
Output is correct |
27 |
Correct |
586 ms |
230712 KB |
Output is correct |