Advertisement
a53

tairos

a53
Mar 14th, 2019
145
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.27 KB | None | 0 0
  1. /// autor Radu Muntean
  2. #include <fstream>
  3. #include <assert.h>
  4.  
  5. using namespace std;
  6.  
  7. ifstream in ("tairos.in");
  8. ofstream out ("tairos.out");
  9.  
  10. const int N_MAX = 100, D_MAX = 10000, R = 1000000007;
  11.  
  12. int N, D;
  13. int fst[N_MAX + 1], val[2 * N_MAX + 1], nxt[2 * N_MAX + 1];
  14. int leafs[N_MAX + 1];
  15. int not_leafs[N_MAX + 1];
  16.  
  17. // Vom folosi vectorul sol, unde sol[i] va reprezenta numarul de foste frunze aflate la distanta i de radacina modulo R.
  18. long long sol[N_MAX * D_MAX + 1];
  19.  
  20. void add_edge(int x, int y) {
  21. // "Static int" spune ca variabila 'contor' va fi initializata doar la primul
  22. // apel al functie 'add_edge()', iar, in rest, se va prelua valoarea lui 'contor'
  23. // lasata de apelul precedent al functiei noastre 'add_edge()'.
  24. static int contor = 0;
  25. val[++contor] = y;
  26. nxt[contor] = fst[x];
  27. fst[x] = contor;
  28. }
  29.  
  30. int no_nodes_visited = 0;
  31.  
  32. void dfs(int node, int dad, int depth) {
  33. no_nodes_visited ++;
  34. bool is_leaf = true;
  35. for (int p = fst[node]; p != 0; p = nxt[p]) {
  36. if (val[p] == dad) {
  37. continue;
  38. }
  39. is_leaf = false;
  40. dfs(val[p], node, depth + 1);
  41. }
  42. if (is_leaf) {
  43. leafs[depth] ++;
  44. } else {
  45. not_leafs[depth] ++;
  46. }
  47. }
  48.  
  49. int main() {
  50. int a, b;
  51. in >> N >> D;
  52. assert (N > 1 && N <= 100);
  53. assert (D > 0 && D <= 10000);
  54.  
  55. for (int i = 0; i < N - 1; i++) {
  56. in >> a >> b;
  57. assert (a > 0 && a <= N);
  58. assert (b > 0 && b <= N);
  59. add_edge(a, b);
  60. add_edge(b, a);
  61. }
  62. // Facem un DFS ca sa gasim numarul de frunze de la fiecare nivel de adancime in arbore.
  63. dfs(1, 0, 0);
  64. assert (no_nodes_visited == N);
  65.  
  66. // Avand o singura radacina, sol[0] = 1;
  67. sol[0] = 1;
  68. long long solution = 0;
  69. // TODO(Radu) scos long long.
  70.  
  71. for (int depth = 0; depth <= D; depth++) {
  72. sol[depth] %= R;
  73. if (D - depth < N) {
  74. solution += sol[depth] * not_leafs[D - depth];
  75. solution %= R;
  76. }
  77. for (int forward = 1; forward <= N; forward++) {
  78. sol[depth + forward] += sol[depth] * leafs[forward];
  79. // TODO(Radu) pus modulo si aici la calibratul testelor.
  80. }
  81. }
  82.  
  83. out << solution;
  84. return 0;
  85. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement