fork(4) download
  1. var
  2. i, j, k, n, d, u, v: longint;
  3. c: int64;
  4. a: array[1..5000, 1..5000] of longint;
  5.  
  6. function min(a, b: longint): longint;
  7. begin
  8. if a > b then a := b;
  9. min := a;
  10. end;
  11.  
  12.  
  13. begin{
  14. assign(input, 'capitals.in'); reset(input);
  15. assign(output, 'capitals.out'); rewrite(output);}
  16. read(n, d);
  17. for i := 1 to n do
  18. for j := 1 to n do a[i][j] := 1000000000;
  19.  
  20. for i := 1 to n - 1 do
  21. begin
  22. read(u, v);
  23. a[u][v] := 1;
  24. a[v][u] := 1;
  25. end;
  26.  
  27. for k := 1 to n do
  28. for i := 1 to n do
  29. for j := 1 to n do
  30. if i<>j then a[i][j] := min(a[i][j], a[i][k] + a[k][j]);
  31. {
  32.   for i := 1 to n do
  33.   begin
  34.   for j := 1 to n do write(a[i][j], ' ');
  35.   writeln;
  36.   end; }
  37. for k := 1 to n do
  38. for i := 1 to n do
  39. for j := 1 to n do
  40. if (a[i][j] = d) and (a[i][k] = d) and (a[j][k] = d) then inc(c);
  41. writeln(c / 6);
  42.  
  43. end.
Success #stdin #stdout 0.02s 97920KB
stdin
5 2
1 4
3 4
5 2
5 4
stdout
 1.0000000000000000E+0000