-
Notifications
You must be signed in to change notification settings - Fork 59
/
Copy pathkdtree.cpp
63 lines (61 loc) · 1.47 KB
/
kdtree.cpp
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
# include <cstdio>
# include <vector>
# include <algorithm>
# define NR 200005
using namespace std;
vector <int> v[NR],vect[NR];
int i,j,n,m,x,y,sol,K,o;
int P[NR];
void DFS (int k, int tata, int nivel)
{
int i,act;
if (v[k].size()==1) P[k]=0;
else
{
for (i=0; i<v[k].size(); ++i)
{
if (v[k][i]!=tata)
{
DFS(v[k][i], k, nivel+1);
vect[nivel].push_back(P[v[k][i]]);
}
}
sort (vect[nivel].begin(), vect[nivel].end());
if (k==o) act=v[k].size()-1;
else act=v[k].size()-2;
if (act==0)
{
if (vect[nivel][act]+1<=K) P[k]=vect[nivel][act]+1;
else ++sol,P[k]=0;
}
else
{
if (vect[nivel][act]+vect[nivel][act-1]+2>K)
{
while (act && vect[nivel][act]+vect[nivel][act-1]+2>K)
++sol, P[k]=vect[nivel][act-1]+1, --act;
if (P[k]>K) ++sol, P[k]=0;
}
else P[k]=vect[nivel][act]+1;
}
vect[nivel].clear();
}
}
int main ()
{
freopen ("kdtree.in", "r", stdin);
freopen ("kdtree.out", "w", stdout);
scanf ("%d%d", &n, &K);
for (i=1; i<n; ++i)
{
scanf ("%d%d", &x, &y);
v[x].push_back(y);
v[y].push_back(x);
}
for (o=1; o<=n; ++o)
{
if (v[o].size()!=1) {DFS(o,0,1); break;}
}
printf ("%d\n", sol);
return 0;
}