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
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
#include<cstdio>
#include<vector>
#include<map>
#include<utility>
#include<queue>
#include<algorithm>
#include<set>

typedef std::map<int, int> edges;
typedef std::map<int, edges> graph;
typedef std::map<int, int> distance;

int n, k;
graph g;
std::vector<distance> distances;
int * visited;
long long * pows;
long long M = 1000000007;

void generate_pows() {
  pows = new long long[n + 2];
  pows[0] = 1;
  for (int i = 1; i < n + 2; i++) {
    pows[i] = (pows[i-1] * n) % M;
  }
}

void input() {
  scanf("%d %d", &n, &k);
  int a, b, p;
  generate_pows();
  visited = new int[n + 2];
  for (int i = 0; i < n; i++) {
    visited[i] = 0;
  }
  for (int i = 0; i < n - 1; i++) {
    scanf("%d %d %d", &a, &b, &p);
    if (g.find(a) == g.end()) {
      g[a] = edges();
    }
    if (g.find(b) == g.end()) {
      g[b] = edges();
    }
    g[a][b] = p;
    g[b][a] = p;
  }
}

void print(edges e) {
  for (edges::iterator it = e.begin(); it != e.end(); it++) {
    printf("(%d %d) ", it -> first, it -> second);
  }
  printf("\n");
}

void print(graph g) {
  for (graph::iterator it = g.begin(); it != g.end(); it++) {
    printf("%d: ", it -> first);
    print(it -> second);
  }
}

long long pow(int p) {
  // if (p == 0) {
  //   return 1;
  // }
  // return n * pow(p - 1);
  return pows[p];
}

long long decode(distance d) {
  long long res = 0;
  for (distance::iterator it = d.begin(); it != d.end(); it++) {
    res = (res + (it -> second) * pow(it -> first)) % M;
  }
  return res;
}

void traverse(int i, distance d, int mark) {
  for (edges::iterator it = g[i].begin(); it != g[i].end(); it++) {
     // printf("%d %d %d\n", i, it -> first, visited[it -> first]);
    if (visited[it -> first] != mark) {
      visited[it -> first] = mark;
      distance dd(d);
      dd[it -> second]++;
      // printf("%d %d %d %lld\n", mark - 1, i, it -> first, decode(dd));
      if (it -> first >= mark) {
        distances.push_back(dd);
      }
      traverse(it -> first, dd, mark);
    }
  }
}

void traverse() {
  for (int i = 0; i < n; i++) {
    distance d;
    visited[i] = i + 1;
    traverse(i, d, i + 1);
  }
}

void print_d(distance d) {
  long long res = 0;
  for (distance::iterator it = d.begin(); it != d.end(); it++) {
    printf("(%d %d) ", it -> first, it -> second);
  }
  printf("\n");
}

bool cmp(distance & a, distance & b) {
  bool result = false;
  std::set<int> keys;
  for (distance::iterator it = a.begin(); it != a.end(); it++) {
    keys.insert(it -> first);
  }
  for (distance::iterator it = b.begin(); it != b.end(); it++) {
    keys.insert(it -> first);
  }

  for (std::set<int>::reverse_iterator it = keys.rbegin(); it != keys.rend(); it++) {
    int i = *it;
    if (a.find(i) != a.end()) {
      if (b.find(i) != b.end()) {
        if (a[i] < b[i]) {
          result = true;
          // printf("1\n");
          break;
        }
        if (a[i] > b[i]) {
          result = false;
          // printf("2\n");
          break;
        }
      } else if (a[i] > 0) {
        result =  false;
        // printf("3 %d\n", i);
        break;
      }
    } else if (b.find(i) != b.end()) {
      if (b[i] > 0) {
        result = true;
        // printf("4\n");
        break;
      }
    }
    // if (a[i] < b[i]) {
    //   printf("%d < %d\n", a[i], b[i]);
    //   result = true;
    //   break;
    // }
    // if (b[i] < a[i]) {
    //   printf("%d < %d\n", b[i], a[i]);
    //   result = false;
    //   break;
    // }
  }
  // if (result != (decode(a) < decode(b))) {
  //   print(a);
  //   print(b);
  //   printf("%d\n", result);
  // }
  return result;
  // return decode(a) < decode(b);
}

long long solve() {
  std::sort(distances.begin(), distances.end(), cmp);
  // for (std::vector<distance>::iterator it = distances.begin(); it != distances.end(); it++) {
  //   printf("%lld\n", decode(*it));
  //   //print_d(*it);
  // }
  return decode(distances[k - 1]);
}

int main() {
  input();
  // print(g);
  traverse();
  printf("%lld\n", solve());
  return 0;
}