-
Notifications
You must be signed in to change notification settings - Fork 2
/
1018. Binary Apple Tree.c
96 lines (72 loc) · 1.79 KB
/
1018. Binary Apple Tree.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
#include <stdio.h>
#define NMAX 101
int Tree[NMAX][NMAX];
int B[NMAX][NMAX];
int Branches[NMAX];
int N, Q;
void traverse(int root, int parent)
{
int i;
for (i = 1; i <= N; i++)
if (Tree[root][i] != -1 && i != parent)
{
traverse(i, root);
Branches[root] += Branches[i] + 1;
}
int q;
for (q = 0; q <= N - Q; q++)
{
int max = 0;
int k;
int left = 0;
for (i = 1; i <= N; i++)
if (Tree[root][i] != -1 && i != parent)
{
left = i;
break;
}
int right = 0;
for (i = left + 1; i <= N; i++)
if (Tree[root][i] != -1 && i != parent)
{
right = i;
break;
}
for (k = 0; k <= q; k++)
{
int Cleft, Cright;
if (k == Branches[left] + 1 || left == 0)
Cleft = 0;
else
Cleft = Tree[root][left] + B[left][k];
if (q - k == Branches[right] + 1 || right == 0)
Cright = 0;
else
Cright = Tree[root][right] + B[right][q - k];
if (k > Branches[left] + 1 || q - k > Branches[right] + 1)
{
Cleft = Cright = 0;
}
if (Cleft + Cright > max)
max = Cleft + Cright;
}
B[root][q] = max;
}
}
int main(void)
{
int i, j;
scanf("%d %d", &N, &Q);
for (i = 1; i <= N; i++)
for (j = 1; j <= N; j++)
Tree[i][j] = -1;
for (i = 0; i < N - 1; i++)
{
int n1, n2, a;
scanf("%d %d %d", &n1, &n2, &a);
Tree[n1][n2] = Tree[n2][n1] = a;
}
traverse(1, -1);
printf("%d",B[1][N - 1 - Q]);
return 0;
}